Android Reup:通知(NPE发布)

Android Reup:通知(NPE发布),android,nullpointerexception,Android,Nullpointerexception,我重新编写了通知类的代码。每次我运行它时,它都会向我抛出一个空指针异常。下面是代码: 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 a

我重新编写了通知类的代码。每次我运行它时,它都会向我抛出一个空指针异常。下面是代码:

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.widget.RemoteViews;

public class kickStart extends Activity {
NotificationManager nm;
Context context = this;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Notification notification = new Notification();
    RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.note);
    contentView.setImageViewResource(R.id.icon, R.drawable.ic_launcher);
    contentView.setTextViewText(R.id.title, "Custom notification");
    contentView.setTextViewText(R.id.text, "This is a custom layout");
    notification.contentView = contentView;
    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    notification.contentIntent = contentIntent;
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    int CUSTOM_VIEW_ID = 10;
    nm.notify(CUSTOM_VIEW_ID, notification);
}
}
现在问一个问题:为什么我会得到NPE。。。我该如何修复它

这是航海日志

07-21 17:58:33.360: W/dalvikvm(6511): threadid=1: thread exiting with uncaught exception (group=0x40018560)
07-21 17:58:33.360: E/AndroidRuntime(6511): FATAL EXCEPTION: main
07-21 17:58:33.360: E/AndroidRuntime(6511): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.this/com.example.this.kickStart}: java.lang.NullPointerException
07-21 17:58:33.360: E/AndroidRuntime(6511):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
07-21 17:58:33.360: E/AndroidRuntime(6511):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-21 17:58:33.360: E/AndroidRuntime(6511):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-21 17:58:33.360: E/AndroidRuntime(6511):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-21 17:58:33.360: E/AndroidRuntime(6511):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-21 17:58:33.360: E/AndroidRuntime(6511):     at android.os.Looper.loop(Looper.java:130)
07-21 17:58:33.360: E/AndroidRuntime(6511):     at android.app.ActivityThread.main(ActivityThread.java:3683)
07-21 17:58:33.360: E/AndroidRuntime(6511):     at java.lang.reflect.Method.invokeNative(Native Method)
07-21 17:58:33.360: E/AndroidRuntime(6511):     at java.lang.reflect.Method.invoke(Method.java:507)
07-21 17:58:33.360: E/AndroidRuntime(6511):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
07-21 17:58:33.360: E/AndroidRuntime(6511):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
07-21 17:58:33.360: E/AndroidRuntime(6511):     at dalvik.system.NativeStart.main(Native Method)
07-21 17:58:33.360: E/AndroidRuntime(6511): Caused by: java.lang.NullPointerException
07-21 17:58:33.360: E/AndroidRuntime(6511):     at com.example.this.kickStart.onCreate(kickStart.java:29)
07-21 17:58:33.360: E/AndroidRuntime(6511):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-21 17:58:33.360: E/AndroidRuntime(6511):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
07-21 17:58:33.360: E/AndroidRuntime(6511):     ... 11 more

您尚未初始化
nm

nm.notify(CUSTOM_VIEW_ID, notification);
您应该添加:

nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
在尝试使用
nm
之前。希望有帮助

来自LogCat

以下两条线供将来参考:

Caused by: java.lang.NullPointerException
    at com.example.this.kickStart.onCreate(kickStart.java:29)

告诉我们NPE在kickStart.java的第29行,特别是kickStart.onCreate()。使用这些信息,您应该能够比我们任何人更快地找到您的NPE,因为我们没有行号:)

如果你的应用程序崩溃,你应该始终发布你的日志。好的,给我一秒钟时间来获取它。另外,出于任何原因重新发布问题()在堆栈溢出中是不受欢迎的,它讨论了适当的方法来引起对旧问题的注意。好的,现在所有这些都得到了解决,它没有给我任何东西。即使发射器已明确定义,也不能发射。哦,主要问题解决了。谢谢你,伙计!