Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.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_Nullpointerexception - Fatal编程技术网

Java 在对话框上查找按钮时出现空指针异常

Java 在对话框上查找按钮时出现空指针异常,java,android,nullpointerexception,Java,Android,Nullpointerexception,这在我的onCreate()中 这是布局go_pro: <LinearLayout android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_width="wrap_content"> <Button android:id="@+id/still_free" android:text="Keep the ads" android:lay

这在我的onCreate()中

这是布局go_pro:

 <LinearLayout android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_width="wrap_content">
     <Button android:id="@+id/still_free" android:text="Keep the ads" android:layout_width="wrap_content" android:layout_height="wrap_content" ></Button>
     <Button android:id="@+id/full_version" android:text="Go PRO" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
 </LinearLayout>
编辑:这太奇怪了!我在xml中有两个相同的按钮,id不同,在创建时有两个相同的按钮,但其中只有一个出现空指针异常

 Button button1 = (Button) dialog.findViewById(R.id.full_version);
            button1.setOnClickListener(new OnClickListener() {
            @Override
                public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("market://details?id=com.paulmaidment.games.flagsoftheworld"));
                startActivity(intent);
                   dialog.cancel();//TODO OPEN PAID APP HERE!!!
                }
            });

            Button free = (Button)findViewById(R.id.stay_free);
            free.setOnClickListener(new OnClickListener() { //EXCEPTION ONLY HERE!
                @Override
                public void onClick(View v) {
                    dialog.cancel();
                }
            });

当应该在活动上调用findViewById时,您正在对话框上调用findViewById。改变

        Button free = (Button) dialog.findViewById(R.id.still_free);

应该修复它。

尝试移动代码

dialog.setContentView(R.layout.go_pro);
        Button free = (Button) dialog.findViewById(R.id.still_free);
        free.setOnClickListener(new OnClickListener() { //EXCEPTION HERE!
        @Override
            public void onClick(View v) {
               dialog.cancel();
            }
        });

要在xml文件中重新创建对话类的方法,id仍然是免费的,并且在代码中访问保持免费。。。就这样

编辑:
嗯,在你的编辑中,你保持了自由,但在最初的帖子中,它仍然是自由的,所以我假设你只是更改了id来测试是否是由它引起的?

你是否检查了
对话框。findViewById(R.id.still\u free)返回的somtheing与null不同吗?请确保清理并重新生成项目。它会重新索引你的id。不是真的。他正在对话框中调用findViewById,因为他正在对话框中搜索按钮。所以代码是正确的。(如果我正确理解他想要做什么)当我们定制任何dailog时,我们根据需要制作xml,我认为他使用的是xml,而xml部分有一个按钮,
        Button free = (Button) dialog.findViewById(R.id.still_free);
        Button free = (Button) findViewById(R.id.still_free);
dialog.setContentView(R.layout.go_pro);
        Button free = (Button) dialog.findViewById(R.id.still_free);
        free.setOnClickListener(new OnClickListener() { //EXCEPTION HERE!
        @Override
            public void onClick(View v) {
               dialog.cancel();
            }
        });