1

我现在开始使用 Java / Android,我有这个错误......

按照教程(根据我的需要修改),我想连接到 rumonet.pt/feeder/index_android.php 输出一个 json 数组。

但是该代码给了我一个错误

  button = (Button) findViewById(R.id.btnFetch);
        buttonClear = (Button) findViewById(R.id.btnClear);
        textView = (TextView) findViewById(R.id.txtView);
        button.setOnClickListener(new Button.OnClickListener()
        {
            public void onClick(View v)
            {
               // examineJSONFile();
            }
        });
        buttonClear.setOnClickListener(new Button.OnClickListener()
        {
            public void onClick(View v)
            {
                //textView.setText("");
            }
        });

更具体地说,在 button 和 buttonclear SetOnClickListener 中,因为如果我评论那些行和 void,它会起作用......只是什么都不做......

Any1知道为什么,或者有更好的教程吗?=) 谢谢 =)

编辑

11-09 14:16:31.836: D/AndroidRuntime(9939): Shutting down VM
11-09 14:16:31.836: W/dalvikvm(9939): threadid=1: thread exiting with uncaught exception (group=0x2b542210)
11-09 14:16:31.836: E/AndroidRuntime(9939): FATAL EXCEPTION: main
11-09 14:16:31.836: E/AndroidRuntime(9939): java.lang.RuntimeException: Unable to start activity ComponentInfo{noticias.rumonet.pt/noticias.rumonet.pt.MainActivity}: java.lang.NullPointerException
11-09 14:16:31.836: E/AndroidRuntime(9939):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
11-09 14:16:31.836: E/AndroidRuntime(9939):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
11-09 14:16:31.836: E/AndroidRuntime(9939):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
11-09 14:16:31.836: E/AndroidRuntime(9939):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
11-09 14:16:31.836: E/AndroidRuntime(9939):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-09 14:16:31.836: E/AndroidRuntime(9939):     at android.os.Looper.loop(Looper.java:137)
11-09 14:16:31.836: E/AndroidRuntime(9939):     at android.app.ActivityThread.main(ActivityThread.java:4441)
11-09 14:16:31.836: E/AndroidRuntime(9939):     at java.lang.reflect.Method.invokeNative(Native Method)
11-09 14:16:31.836: E/AndroidRuntime(9939):     at java.lang.reflect.Method.invoke(Method.java:511)
11-09 14:16:31.836: E/AndroidRuntime(9939):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-09 14:16:31.836: E/AndroidRuntime(9939):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-09 14:16:31.836: E/AndroidRuntime(9939):     at dalvik.system.NativeStart.main(Native Method)
11-09 14:16:31.836: E/AndroidRuntime(9939): Caused by: java.lang.NullPointerException
11-09 14:16:31.836: E/AndroidRuntime(9939):     at noticias.rumonet.pt.MainActivity.onCreate(MainActivity.java:85)
11-09 14:16:31.836: E/AndroidRuntime(9939):     at android.app.Activity.performCreate(Activity.java:4465)
11-09 14:16:31.836: E/AndroidRuntime(9939):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
11-09 14:16:31.836: E/AndroidRuntime(9939):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
11-09 14:16:31.836: E/AndroidRuntime(9939):     ... 11 more







   button.setOnClickListener(new Button.OnClickListener()
                {
                    public void onClick(View v)
                    {
                       // examineJSONFile();
                    }
                });
4

1 回答 1

1

检查 XML 布局。真的叫btnClear吗??

在我看来,一旦您尝试分配 onClickListener,您就有一个空指针,因为它在布局上找不到视图。

于 2012-11-09T14:32:23.150 回答