当我想恢复最小化通知时,android force关闭

时间:2012-10-29 07:58:32

标签: android android-intent android-notifications

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

2 个答案:

答案 0 :(得分:0)

您正在请求WelcomeActivity1第105行处的空白对象。这就是您的应用程序崩溃的原因。

此外:没有必要将您的应用程序“缩小”到通知区域,因为Android本身处理应用程序之间的切换。如果用户已登录,您只需在将LoginActivity发送到下一个finish()后通过调用startIntent方法关闭Activity

答案 1 :(得分:0)

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

public boolean onKeyDown(int keyCode, KeyEvent event){
        if(keyCode == KeyEvent.KEYCODE_BACK){
            //showMinimized();
            moveTaskToBack(true);
        }           
        return super.onKeyDown(keyCode, event);
    }