2

美好的一天,我最近很难处理 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)
4

2 回答 2

0

您在. _ WelcomeActivity1这就是您的应用程序崩溃的原因。

另外:没有必要将您的应用程序“缩小”到通知区域,因为 Android 本身会处理应用程序之间的切换。如果用户已登录,您可以在发送到 next之后LoginActivity通过调用该方法简单地关闭。finish()startIntentActivity

于 2012-10-29T08:21:01.707 回答
0

改为使用此方法,因此当用户按下后退按钮时,它将显示主页,当用户想要返回应用程序时,他/她只需按住菜单键,将显示最近应用程序的菜单正在使用。

public boolean onKeyDown(int keyCode, KeyEvent event){
        if(keyCode == KeyEvent.KEYCODE_BACK){
            //showMinimized();
            moveTaskToBack(true);
        }           
        return super.onKeyDown(keyCode, event);
    }
于 2012-11-01T02:53:39.900 回答