设置OnClickListener时出现NullPointerException

时间:2013-08-26 19:55:09

标签: android android-layout button nullpointerexception

由于某些未知原因我得到了FATAL EXCEPTION:main - java.lang.NullPointerException就行了:

x_button.setOnClickListener(new View.OnClickListener() {

但我不确定为什么会这样。按钮在那里 - 所以我不确定为什么在这一点上会出现空指针 - 但事实确实如此。

非常感谢任何输入。

SOURCE SNIPPET:

@Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub

            super.onPostExecute(result);
            // Show updated screen if table was successfully updated
            // Or alert indicating settings are not updated
            if (result.equals("success")) {

                setContentView(R.layout.completion);

            } else
                setContentView(R.layout.error);

            Button x_button = (Button) findViewById(R.id.x_button);
            x_button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finishAll(v);
                }
            });
        }

LOGCAT:

08-26 15:49:42.419: W/System.err(5998): java.io.IOException: Stream is closed
08-26 15:49:42.429: W/System.err(5998):     at java.util.zip.GZIPInputStream.read(GZIPInputStream.java:161)
08-26 15:49:42.429: W/System.err(5998):     at java.io.DataInputStream.read(DataInputStream.java:95)
08-26 15:49:42.429: W/System.err(5998):     at java.io.InputStreamReader.read(InputStreamReader.java:255)
08-26 15:49:42.429: W/System.err(5998):     at java.io.BufferedReader.fillBuf(BufferedReader.java:128)
08-26 15:49:42.429: W/System.err(5998):     at java.io.BufferedReader.readLine(BufferedReader.java:357)
08-26 15:49:42.429: W/System.err(5998):     at com.tracfone.straighttalk.networktasklibrary.NetworkTask.doInBackground(NetworkTask.java:37)
08-26 15:49:42.429: W/System.err(5998):     at com.tracfone.straighttalk.networktasklibrary.NetworkTask.doInBackground(NetworkTask.java:1)
08-26 15:49:42.429: W/System.err(5998):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-26 15:49:42.429: W/System.err(5998):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
08-26 15:49:42.429: W/System.err(5998):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-26 15:49:42.429: W/System.err(5998):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-26 15:49:42.429: W/System.err(5998):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-26 15:49:42.429: W/System.err(5998):     at java.lang.Thread.run(Thread.java:1019)
08-26 15:49:46.299: W/dalvikvm(5998): threadid=1: thread exiting with uncaught exception (group=0x4016e560)
08-26 15:49:46.299: E/AndroidRuntime(5998): FATAL EXCEPTION: main
08-26 15:49:46.299: E/AndroidRuntime(5998): java.lang.NullPointerException
08-26 15:49:46.299: E/AndroidRuntime(5998):     at com.project.new.datasettings.ConfigFinalActivity$TableUpdateRequestTask.onPostExecute(ConfigFinalActivity.java:552)
08-26 15:49:46.299: E/AndroidRuntime(5998):     at com.project.new.datasettings.ConfigFinalActivity$TableUpdateRequestTask.onPostExecute(ConfigFinalActivity.java:1)
08-26 15:49:46.299: E/AndroidRuntime(5998):     at android.os.AsyncTask.finish(AsyncTask.java:417)
08-26 15:49:46.299: E/AndroidRuntime(5998):     at android.os.AsyncTask.access$300(AsyncTask.java:127)
08-26 15:49:46.299: E/AndroidRuntime(5998):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
08-26 15:49:46.299: E/AndroidRuntime(5998):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-26 15:49:46.299: E/AndroidRuntime(5998):     at android.os.Looper.loop(Looper.java:130)
08-26 15:49:46.299: E/AndroidRuntime(5998):     at android.app.ActivityThread.main(ActivityThread.java:3821)
08-26 15:49:46.299: E/AndroidRuntime(5998):     at java.lang.reflect.Method.invokeNative(Native Method)
08-26 15:49:46.299: E/AndroidRuntime(5998):     at java.lang.reflect.Method.invoke(Method.java:507)
08-26 15:49:46.299: E/AndroidRuntime(5998):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
08-26 15:49:46.299: E/AndroidRuntime(5998):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:633)
08-26 15:49:46.299: E/AndroidRuntime(5998):     at dalvik.system.NativeStart.main(Native Method)

完整来源:

https://docs.google.com/document/d/1ap5qknNcn9RaJhewc9CarQja2uS4Xjgtzw4eNzXzLKY/edit?usp=sharing

1 个答案:

答案 0 :(得分:2)

您的NullpointerException发生是因为Button“x_button”为null,这意味着findViewById(...)无法找到具有您提供的ID的Button。

我认为有两个可能的原因:

  • “R.id.x_button”既不是你的两个布局文件“完成”和“错误”,而是在其他一些布局文件中。这将导致不会出现编译器错误,但会在运行代码时导致Nullpointer。
  • “R.id.x_button”就在其中一个中,但你在setContentView(...)中使用另一个,具体取决于你的“成功”。

推荐

  

检查布局文件“错误”和“完成”,然后查看两者   包含ID为“R.id.x_button”的Button,如果没有,请更改您的代码   或者将Button添加到布局文件中。

如果您的按钮仅作为示例存在于“完成”中,则需要将代码更改为:

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub

        super.onPostExecute(result);
        // Show updated screen if table was successfully updated
        // Or alert indicating settings are not updated
        if (result.equals("success")) {

            setContentView(R.layout.completion);

            Button x_button = (Button) findViewById(R.id.x_button);
            x_button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finishAll(v);
                }
             });
        } else
            setContentView(R.layout.error);
    }

这将避免NullPointer,因为只有在加载实际包含Button的布局时才会初始化Button。