2

For some unknown reason I'm getting FATAL EXCEPTION: main - java.lang.NullPointerException on the line:

x_button.setOnClickListener(new View.OnClickListener() {

but I'm not sure why this is happening. The button is there - so I'm not sure why a null pointer would occur at this point - but it is.

Any input is greatly appreciated.

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)

FULL SOURCE:

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

4

1 回答 1

2

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

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

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

我会推荐

检查您的布局文件“错误”和“完成”,看看它们是否都包含一个 ID 为“R.id.x_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。

于 2013-08-26T20:18:59.173 回答