Reputation: 7739
I have that code:
public class GeolocationActivity extends ActionBarActivity implements SensorEventListener {
{
//...
private void setUpCompass()
{
isCompass = true;
setContentView(R.layout.subactivity_compass);
//...
}
private void setUpMain()
{
//...
btn2 = (Button)findViewById(R.id.geoloc_compass);
btn2.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// here I want to call method setUpCompass();
}
});
//...
}
}
I want to call setUpCompass()
method from onClick()
function in setUpMain()
.
I tried:
Context c = getBaseContext();
((GeolocationActivity)c).setUpCompass();
but I get:
java.lang.ClassCastException: android.app.ContextImpl cannot be cast to com.myname.myapp.GeolocationActivity
What am I doing wrong?
Upvotes: 0
Views: 786
Reputation: 44158
Instead simply use GeolocationActivity.this
.
Example:
GeolocationActivity.this.setUpCompass();
Upvotes: 1