美好的一天,我最近很难处理 android 通知,所以这是我的问题。我的应用程序所做的是用户将登录,如果成功,它将显示主页。当用户按下后退按钮(从主页)时,它将最小化应用程序并创建通知,而不是返回登录页面。我的问题是,当我按下正在运行的通知(从通知选项卡恢复应用程序)时,它会产生错误。这是我的一些代码
public class WelcomeActivity1 extends ListActivity{
private NotificationManager mNM;
Intent notificationIntent;
protected void onCreate(Bundle savedInstanceState) {
mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationIntent = new Intent(WelcomeActivity1.this, WelcomeActivity1.class);
}
public boolean onKeyDown(int keyCode, KeyEvent event){
if(keyCode == KeyEvent.KEYCODE_BACK){
showMinimized();
moveTaskToBack(true);
}
return super.onKeyDown(keyCode, event);
}
public void showMinimized(){
CharSequence text = "Zylun Intranet is Minimized";
Notification notification = new Notification(R.drawable.ic_launcher, text, System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(WelcomeActivity1.this, 0, new Intent(this,WelcomeActivity1.class), 0);
notification.setLatestEventInfo(getApplicationContext(), "Zylun Intranet Main", "Running", contentIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
mNM.notify(1,notification);
}
}
如果有人能告诉我我的代码有什么问题,我将不胜感激。谢谢!
当登录凭据正确时 http://i.imgur.com/0WROj.jpg?1
当用户按下后退按钮时最小化应用程序(从主页) http://i.imgur.com/31yNl.jpg?1
这是我的错误:
10-29 07:45:01.196: E/AndroidRuntime(733): FATAL EXCEPTION: main
10-29 07:45:01.196: E/AndroidRuntime(733): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.intranetzylun1/com.app.intranetzylun1.WelcomeActivity1}: java.lang.NullPointerException
10-29 07:45:01.196: E/AndroidRuntime(733): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
10-29 07:45:01.196: E/AndroidRuntime(733): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-29 07:45:01.196: E/AndroidRuntime(733): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-29 07:45:01.196: E/AndroidRuntime(733): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-29 07:45:01.196: E/AndroidRuntime(733): at android.os.Handler.dispatchMessage(Handler.java:99)
10-29 07:45:01.196: E/AndroidRuntime(733): at android.os.Looper.loop(Looper.java:123)
10-29 07:45:01.196: E/AndroidRuntime(733): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-29 07:45:01.196: E/AndroidRuntime(733): at java.lang.reflect.Method.invokeNative(Native Method)
10-29 07:45:01.196: E/AndroidRuntime(733): at java.lang.reflect.Method.invoke(Method.java:521)
10-29 07:45:01.196: E/AndroidRuntime(733): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-29 07:45:01.196: E/AndroidRuntime(733): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-29 07:45:01.196: E/AndroidRuntime(733): at dalvik.system.NativeStart.main(Native Method)
10-29 07:45:01.196: E/AndroidRuntime(733): Caused by: java.lang.NullPointerException
10-29 07:45:01.196: E/AndroidRuntime(733): at com.app.intranetzylun1.WelcomeActivity1.onCreate(WelcomeActivity1.java:105)
10-29 07:45:01.196: E/AndroidRuntime(733): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-29 07:45:01.196: E/AndroidRuntime(733): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)