我现在从Java / Android开始,我有这个错误......
按照this教程(根据我的需要修改),我想连接到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("");
}
});
更具体地说,在按钮和按钮清晰的SetOnClickListener中,因为如果我评论那些行和虚空,它就可以工作......只是不做任何事......
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();
}
});
答案 0 :(得分:1)
检查XML布局。它真的叫做btnClear吗?
在我看来,一旦你尝试协助onClickListener你有一个空指针,因为它没有在布局上找到视图。