【发布时间】:2010-12-24 19:27:05
【问题描述】:
当我注释掉 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
【问题讨论】:
-
“Logcat 显示一堆红色,但我不知道如何阅读...”在这里发布堆栈跟踪,我们可以帮助您阅读。
-
你为什么要-1这个?史诗般的失败