问题描述
我修改了code到我的通知类。它每次去运行它时随地吐痰一个空指针异常看着我。所以,再一次这里是code:
进口android.app.Activity;
进口android.app.Notification;
进口android.app.NotificationManager;
进口android.app.PendingIntent;
进口android.content.Context;
进口android.content.Intent;
进口android.os.Bundle;
进口android.widget.RemoteViews;公共类Kickstart的延伸活动{
NotificationManager纳米;
上下文的背景下=这;
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
通知通知=新的通知();
RemoteViews内容查看=新的RemoteViews(getPackageName(),R.layout.note);
contentView.setImageViewResource(R.id.icon,R.drawable.ic_launcher);
contentView.setTextViewText(R.id.title,自定义通知);
contentView.setTextViewText(R.id.text,这是一个自定义布局);
notification.contentView =内容查看;
意图notificationIntent =新意图(这一点,MainActivity.class);
的PendingIntent contentIntent = PendingIntent.getActivity(在此,0,notificationIntent,0);
notification.contentIntent = contentIntent;
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
INT CUSTOM_VIEW_ID = 10;
nm.notify(CUSTOM_VIEW_ID,通知);
}
}
现在的问题:为什么我会得到NPE ......,我如何解决它。
这里是logcat的
07-21 17:58:33.360:W / dalvikvm(6511):主题ID = 1:螺纹未捕获的异常(组= 0x40018560)退出
07-21 17:58:33.360:E / AndroidRuntime(6511):致命异常:主要
07-21 17:58:33.360:E / AndroidRuntime(6511):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.this / com.example.this.kickStart}:显示java.lang.NullPointerException
07-21 17:58:33.360:E / AndroidRuntime(6511):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
07-21 17:58:33.360:E / AndroidRuntime(6511):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-21 17:58:33.360:E / AndroidRuntime(6511):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)
07-21 17:58:33.360:E / AndroidRuntime(6511):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931)
07-21 17:58:33.360:E / AndroidRuntime(6511):在android.os.Handler.dispatchMessage(Handler.java:99)
07-21 17:58:33.360:E / AndroidRuntime(6511):在android.os.Looper.loop(Looper.java:130)
07-21 17:58:33.360:E / AndroidRuntime(6511):在android.app.ActivityThread.main(ActivityThread.java:3683)
07-21 17:58:33.360:E / AndroidRuntime(6511):在java.lang.reflect.Method.invokeNative(本机方法)
07-21 17:58:33.360:E / AndroidRuntime(6511):在java.lang.reflect.Method.invoke(Method.java:507)
07-21 17:58:33.360:E / AndroidRuntime(6511):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:864)
07-21 17:58:33.360:E / AndroidRuntime(6511):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
07-21 17:58:33.360:E / AndroidRuntime(6511):在dalvik.system.NativeStart.main(本机方法)
07-21 17:58:33.360:E / AndroidRuntime(6511):因:显示java.lang.NullPointerException
07-21 17:58:33.360:E / AndroidRuntime(6511):在com.example.this.kickStart.onCreate(kickStart.java:29)
07-21 17:58:33.360:E / AndroidRuntime(6511):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-21 17:58:33.360:E / AndroidRuntime(6511):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
07-21 17:58:33.360:E / AndroidRuntime(6511):11 ...更多
您还没有初始化纳米
:
nm.notify(CUSTOM_VIEW_ID,通知);
您应该加上:
纳米=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
试图用纳米
之前。希望帮助!
从LogCat中
以备将来参考以下两行:
产生的原因:显示java.lang.NullPointerException
在com.example.this.kickStart.onCreate(kickStart.java:29)
告诉我们,NPE是在kickStart.java 29行,特别是kickStart.onCreate()。利用这些信息,你应该能找到你的NPE比我们更快,因为我们没有行号。 :)
I reworked the code to my notification class. Its spitting an Null pointer Exception at me every time I go to run it. So once more here is the code:
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);
}
}
Now for the question: Why am I getting NPE... And how do I fix it.
HERE IS THE LOGCAT
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
You haven't initialized nm
:
nm.notify(CUSTOM_VIEW_ID, notification);
You should add:
nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
before trying to use nm
. Hope that helps!
From LogCat
For future reference these two lines:
Caused by: java.lang.NullPointerException
at com.example.this.kickStart.onCreate(kickStart.java:29)
Tell us that the NPE is on line 29 in kickStart.java, specifically kickStart.onCreate(). Using this information you should be able to find you NPEs faster than any of us, since we don't have line numbers. :)
这篇关于REUP:通知(NPE问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!