链接按钮以启动活动

时间:2012-03-06 18:55:51

标签: android

我试图链接数组[1]来启动一个活动,但它在这一行显示我的错误.Intent i1 = new Intent(this,Difficulty.class);如果有人看到我出错的地方请帮助我

错误读取“构造函数Intent(new DialogInterface.OnClickListener(){},Class)未定义”

        final CharSequence[] items = {"Red", "Green", "Blue"};

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Pick a color");
        builder.setItems(items, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {

            if ("Green".equals(items[1]))
            {Intent intent = new Intent();
            Intent i1 = new Intent(this, Diff.class);
            startActivity(i1);;}

        }
        }).show();
        AlertDialog alert = builder.create();

2 个答案:

答案 0 :(得分:2)

更改

Intent i1 = new Intent(this, Diff.class);

Intent i1 = new Intent(TheNameOfYourActivity.this, diff.class);

并将“TheNameOfYourActivity”替换为其中发生的活动的名称。

答案 1 :(得分:1)

由于您在对话框中启动了onclick侦听器的意图, 将引用对话框实例而不是您的活动类(我假设您正在调用代码来自)。

要解决此问题,您需要创建一个包含对您的活动的引用的变量,并使用该变量代替 this

相关问题