问题描述
- Unfortunately,Xxx has stoppe 这是我的logcat
- 07-15 02:03:35.355: I/Process(1664): Sending signal. PID: 1664 SIG: 9
07-15 02:03:36.955: D/AndroidRuntime(1703): Shutting down VM
07-15 02:03:36.955: W/dalvikvm(1703): threadid=1: thread exiting with uncaught exception (group=0xb1a8fba8)
07-15 02:03:36.995: E/AndroidRuntime(1703): FATAL EXCEPTION: main
07-15 02:03:36.995: E/AndroidRuntime(1703): Process: com.example.android_notification PID: 1703
07-15 02:03:36.995: E/AndroidRuntime(1703): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android_notification/com.example.android_notification.MainActivity}: java.lang.NullPointerException
07-15 02:03:36.995: E/AndroidRuntime(1703): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
07-15 02:03:36.995: E/AndroidRuntime(1703): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-15 02:03:36.995: E/AndroidRuntime(1703): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-15 02:03:36.995: E/AndroidRuntime(1703): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-15 02:03:36.995: E/AndroidRuntime(1703): at android.os.Handler.dispatchMessage(Handler.java:102)
07-15 02:03:36.995: E/AndroidRuntime(1703): at android.os.Looper.loop(Looper.java:136)
07-15 02:03:36.995: E/AndroidRuntime(1703): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-15 02:03:36.995: E/AndroidRuntime(1703): at java.lang.reflect.Method.invokeNative(Native Method)
07-15 02:03:36.995: E/AndroidRuntime(1703): at java.lang.reflect.Method.invoke(Method.java:515)
07-15 02:03:36.995: E/AndroidRuntime(1703): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-15 02:03:36.995: E/AndroidRuntime(1703): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-15 02:03:36.995: E/AndroidRuntime(1703): at dalvik.system.NativeStart.main(Native Method)
07-15 02:03:36.995: E/AndroidRuntime(1703): Caused by: java.lang.NullPointerException
07-15 02:03:36.995: E/AndroidRuntime(1703): at com.example.android_notification.MainActivity.onCreate(MainActivity.java:29)
07-15 02:03:36.995: E/AndroidRuntime(1703): at android.app.Activity.performCreate(Activity.java:5231)
07-15 02:03:36.995: E/AndroidRuntime(1703): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-15 02:03:36.995: E/AndroidRuntime(1703): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
07-15 02:03:36.995: E/AndroidRuntime(1703): ... 11 more
07-15 02:03:40.435: I/Process(1703): Sending signal. PID: 1703 SIG: 9
解决方案
ComponentInfo{com.example.android_notification/com.example.android_notification.MainActivity}: java.lang.NullPointerExceptionmainActivity中报空指针了。跟踪一下onCreate中的代码,看什么报空指针了。
解决方案二:
NullPointerException,重点查一下Active,和各 Active上 的控件的初始化。
可能是控件是 ID 不对,导致控件变量为 NULL。
解决方案三:
07-15 02:03:36.995: E/AndroidRuntime(1703): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android_notification/com.example.android_notification.MainActivity}: java.lang.NullPointerException
你的MainActivity里面有对象报空指针异常。设置断点排除一下错误。
解决方案四:
public class MainActivity extends ActionBarActivity {
boolean is_send = false;
Notification mynotifiNotification;
NotificationManager notificationManager;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) { // TODO Auto-generated method stub if(!is_send){ Intent i = new Intent(MainActivity.thisNotificationActivity.class); PendingIntent pi = PendingIntent.getActivity(MainActivity.this0i0); mynotifiNotification = new Notification(); mynotifiNotification.icon = R.drawable.image1; mynotifiNotification.tickerText = getResources().getString(R.string.notification); mynotifiNotification.defaults = Notification.DEFAULT_SOUND; mynotifiNotification.setLatestEventInfo(MainActivity.this示例""单击查看"" pi); notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); notificationManager.notify(0mynotifiNotification); is_send = true; btn.setText(""关闭消息提示""); }else{ notificationManager.cancelAll(); is_send = false; btn.setText(R.string.btn); } } }); 这是我的mainActivity