java - Android如何将Activity.class作为函数的参数传递

标签 java android

我最近从 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>)

我怎样才能正确得到这个?

最佳答案

这样调用:

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);
        }
    });
}

关于java - Android如何将Activity.class作为函数的参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15229324/

相关文章:

java - 如何消费Spring Cloud Stream中默认死信队列的消息?

android - 无法在 Android 中将应用程序包转换为 APK

android - 如果放置在工具栏上方,则水平进度条不可见(适用于 Android 5)

java - 无法访问类 com.sun.org.apache.xerces.internal.impl.dv.util.Base64

java - Android: ListView 未显示

java - Spring Boot 3.0/Security 6.0 迁移 - SecurityFilterChain 中的 "EL1057E: No bean resolver registered in the context to resolve access to bean..."

android - 后退按钮不起作用

java - 为什么我的异常处理错误会导致无限循环?

android - 如何在Android(2016)中与社交网络(Whatsapp,Facebook)共享音频?

android - 在android中设置WebView缓存的过期时间