Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 未能将按钮链接到意图_Java_Android - Fatal编程技术网

Java 未能将按钮链接到意图

Java 未能将按钮链接到意图,java,android,Java,Android,当我尝试此代码时,它读取用户单击的内容并将其与按钮名称进行比较,它似乎只适用于一个数组,而不是第二个数组。如果有人知道为什么,请帮助我 case R.id.new_button: final CharSequence[] items = {"N", "E", "M", "G"}; AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitl

当我尝试此代码时,它读取用户单击的内容并将其与按钮名称进行比较,它似乎只适用于一个数组,而不是第二个数组。如果有人知道为什么,请帮助我

        case R.id.new_button:
        final CharSequence[] items = {"N", "E", "M", "G"};

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

            if ("N".equals(items[0]))
            {Intent intent = new Intent();
            Intent i0 = new Intent(B.this, Test1.class);

            startActivity(i0);}
            else if ("M".equals(items[2]))
            {Intent intent = new Intent();
            Intent i2 = new Intent(Brain.this, Test2.class);

            startActivity(i2);;}


        }

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

编辑对不起,我看到了错误的东西让我重新检查一下您的代码

正如您现在的代码一样,您永远不会碰到if语句的第二部分,因为“N”始终等于items[0]。所以else永远不会被执行

这应该起作用:

if ("N".equals(items[item]))
{
    Intent intent = new Intent();
    Intent i0 = new Intent(Brain.this, CropImageActivity.class);
    startActivity(i0);
}
else if ("M".equals(items[item]))
{
    Intent intent = new Intent();
    Intent i2 = new Intent(Brain.this, Test2.class);
    startActivity(i2);
}

为什么要在builder.setItems()上调用show?