当我注释掉 calc1.setOnClickListener(buttonListener);
时它运行良好,当我允许它运行时,它使我的脚本崩溃。我怎样才能弄清楚发生了什么? Logcat 显示一堆红色,但我不确定如何阅读...
package com.example.contactwidget;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ContactWidget extends Activity {
private static final int HELLO_ID = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button calc1 = (Button) findViewById(R.id.calc_button_1);
OnClickListener buttonListener = new View.OnClickListener() {
public void onClick(View v) {
}
};
calc1.setOnClickListener(buttonListener);
setContentView(R.layout.main);
}
}
更新
堆栈跟踪...
E/AndroidRuntime( 339): FATAL EXCEPTION: main
E/AndroidRuntime( 339): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.contactwidget/com.example.contactwidget.ContactWidget}: java.lang.NullPointerException
E/AndroidRuntime( 339): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
E/AndroidRuntime( 339): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
E/AndroidRuntime( 339): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
E/AndroidRuntime( 339): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
E/AndroidRuntime( 339): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 339): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 339): at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime( 339): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 339): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 339): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime( 339): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime( 339): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 339): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 339): at com.example.contactwidget.ContactWidget.onCreate(ContactWidget.java:41)
E/AndroidRuntime( 339): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime( 339): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
E/AndroidRuntime( 339): ... 11 more
最佳答案
ccheneson's answer对于您的具体情况可能是正确的。
根据您的堆栈跟踪,您正在运行 NullPointerException。一种更容易追踪这些异常的好方法是为所有未捕获的 NullPointerExceptions 添加 Java 异常断点。 (我还建议中断 ArrayIndexOutOfBoundsException、IllegalArgumentException 和 IllegalStateException)。
要执行此操作,请转到调试透视图(窗口 -> 打开透视图 -> 调试或其他,然后是调试),找到断点 Pane (默认情况下它与右上角的变量 Pane 结合在一起),然后单击 J !图标。下次您遇到此异常时,您将能够准确地看到它发生的位置,而不必筛选 RuntimeException 输出。
关于android - 如何在 Eclipse 中调试 Android/Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4528014/