java安卓如何传递活动。类作为函数的参数
我最近从Python转到Android,我被困在这里
这是我的类声明,用于为接受必要参数的警报对话框创建公共函数:
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<TestActivity>)
我怎样才能正确地得到这个
# 1 楼答案
你需要实例。使用
this
或SampleActivity.this
# 2 楼答案
我想你想通过考试。如果此不起作用,请使用
MakeCall.this
# 3 楼答案
这对我很有用:
像这样调用函数:
switchActivity(DestinationActivity.class)
# 4 楼答案
只需创建一个活动对象/实例,如新建YourActivity()
并将此方法用作
# 5 楼答案
这样称呼:
定义:
# 6 楼答案
如果您只想传递对活动的引用,请使用:
MakeCall.this
(或者只使用this
)