I recently moved to Android from python and am stuck here.
This is my class declaration to create a common function for an Alert Dialog which accepts necessary parameter
If you just want to pass a reference to your Activity use: MakeCall.this
(or maybe just this
.)
You need the instance. Use this
or SampleActivity.this
.
In Java, each class you write will also have a Class
class attached to it. The Class
class will be used by the classloader etc.
As others have said you should use MakeCall.this
instead of MakeCall.class
because MakeCall.this
will point to itself which is an Activity whilst MakeCall.class
will point to MakeCall's attached Class
class.
This works for me:
private void switchActivity(Class cls){
Intent intent = new Intent(HomeActivity.this, cls);
startActivity(intent);
}
Call the function like this: switchActivity(DestinationActivity.class)
I think you want to pass this
. If this doesn't work, use MakeCall.this
.
getAlertDialog(strArray, strTitle, this).show();
ButtonClickBySani(R.id.btnsehrabandi, sehrabandiActivity.class);
private void ButtonClickBySani(int ButtonId, final Class<? extends Activity> ActivityToOpen)
{
Button btn;
// Locate the button in activity_main.xml
btn = (Button) findViewById(ButtonId);
// Capture button clicks
btn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
startActivity(new Intent(getBaseContext(), ActivityToOpen));
// Start NewActivity.class
//Intent myIntent = new Intent(getBaseContext(), ActivityToOpen);
// startActivity(myIntent);
}
});
}