我最近从python转移到Android,我被困在这里。
这是我的类声明,用于为Alert Dialog创建一个接受必要参数的通用函数public static AlertDialog.Builder getAlertDialog(String strArray[], String strTitle, Activity v) { return new AlertDialog.Builder(v) .setTitle(strTitle).setItems(strArray, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }); }
但我无法通过这段代码调用此函数,这给了我一个错误
getAlertDialog(strArray, strTitle, MakeCall.class).show();
错误是
the method getAlertDialog(String[], String, Activity) in the type MakeCallAlertDialog is not applicable for the arguments (String[], String, Class)
任何人都可以告诉我如何正确地得到这个? 先谢谢你。
如果你只想传递对你的Activity使用的引用: MakeCall.this (或者只是this 。)
像这样打电话:
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); } }); }
/ **************************** / SANI HYNE(delickate)
我想你想通过this 。 如果这不起作用,请使用MakeCall.this 。
getAlertDialog(strArray, strTitle, this).show();
只需像新的YourActivity()一样创建一个活动对象/实例。
public static void Redirect(Context context,Activity page) { ..... //code context.startActivity(new Intent(context,page.getClass())); ((Activity) context).finish(); }
并使用此方法作为
Redirect(Registration.this, new YourActivity());
你需要这个实例。 使用this或SampleActivity.this 。
在Java中,您编写的每个类也将附加一个Class类。 Class类将由类加载器等使用。
正如其他人所说,你应该使用MakeCall.this而不是MakeCall.class因为MakeCall.this将指向它自己是一个Activity而MakeCall.class将指向MakeCall的附加Class类。
这对我有用:
private void switchActivity(Class cls){ Intent intent = new Intent(HomeActivity.this, cls); startActivity(intent); }
像这样调用函数: switchActivity(DestinationActivity.class)