5

onPause()我创建了一个应用程序,它在按下后退按钮时调用。

如下方式..

protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();

        // Notification show in status bar

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this).setSmallIcon(R.drawable.icon).setContentTitle("TNLRadio")
                .setContentText("");
        // Creates an explicit intent for an Activity in your app
        // Intent resultIntent = new Intent(this, MainActivity.class);
        Intent resultIntent = this.getIntent();

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        // Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack(MainActivity.class);
        // Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
                PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        int mId = 0;
        // mId allows you to update the notification later on.
        mNotificationManager.notify(mId, mBuilder.build());

        this.unregisterReceiver(UIStateManager.getInsatance());
        // unregister call listener
        UIStateManager.getInsatance().onPause(this);
    }

单击后退按钮时,我想做同样的事情...在onBackPress(). 我在onPause()方法内部调用onBackPress()方法。

然后我得到如下错误:

08-28 13:45:34.600: E/AndroidRuntime(14153): FATAL EXCEPTION: main
08-28 13:45:34.600: E/AndroidRuntime(14153): java.lang.IllegalArgumentException: Receiver not registered: com.ironone.streaming.evt.UIStateManager@4153d788
08-28 13:45:34.600: E/AndroidRuntime(14153):    at android.app.LoadedApk.forgetReceiverDispatcher(LoadedApk.java:628)
08-28 13:45:34.600: E/AndroidRuntime(14153):    at android.app.ContextImpl.unregisterReceiver(ContextImpl.java:1130)
08-28 13:45:34.600: E/AndroidRuntime(14153):    at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:354)
08-28 13:45:34.600: E/AndroidRuntime(14153):    at com.ironone.streaming.MainActivity.onPause(MainActivity.java:567)
08-28 13:45:34.600: E/AndroidRuntime(14153):    at com.ironone.streaming.MainActivity.onBackPressed(MainActivity.java:866)
08-28 13:45:34.600: E/AndroidRuntime(14153):    at android.app.Activity.onKeyUp(Activity.java:2099)
08-28 13:45:34.600: E/AndroidRuntime(14153):    at android.view.KeyEvent.dispatch(KeyEvent.java:2633)
08-28 13:45:34.600: E/AndroidRuntime(14153):    at android.app.Activity.dispatchKeyEvent(Activity.java:2329)
08-28 13:45:34.600: E/AndroidRuntime(14153):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1957)
08-28 13:45:34.600: E/AndroidRuntime(14153):    at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3546)
08-28 13:45:34.600: E/AndroidRuntime(14153):    at android.view.ViewRootImpl.handleFinishedEvent(ViewRootImpl.java:3519)
08-28 13:45:34.600: E/AndroidRuntime(14153):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2603)
08-28 13:45:34.600: E/AndroidRuntime(14153):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-28 13:45:34.600: E/AndroidRuntime(14153):    at android.os.Looper.loop(Looper.java:137)
08-28 13:45:34.600: E/AndroidRuntime(14153):    at android.app.ActivityThread.main(ActivityThread.java:4507)
08-28 13:45:34.600: E/AndroidRuntime(14153):    at java.lang.reflect.Method.invokeNative(Native Method)
08-28 13:45:34.600: E/AndroidRuntime(14153):    at java.lang.reflect.Method.invoke(Method.java:511)
08-28 13:45:34.600: E/AndroidRuntime(14153):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
08-28 13:45:34.600: E/AndroidRuntime(14153):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
08-28 13:45:34.600: E/AndroidRuntime(14153):    at dalvik.system.NativeStart.main(Native Method)

请尽快帮助我

4

3 回答 3

4

首先,彻底删除onBackPressed(). 然后,尝试将以下代码插入onKeyDown()如下。(如果您还没有这样的方法。只需复制以下代码)

public boolean onKeyDown(int keyCode, KeyEvent event) {

    if(keyCode == KeyEvent.KEYCODE_BACK)
    {
            moveTaskToBack(true);
            return true;
    }

    // your other related codes
}
于 2013-08-28T09:25:13.717 回答
0

请仔细阅读错误日志,异常原因如下:

08-28 13:45:34.600: E/AndroidRuntime(14153):    at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:354)
08-28 13:45:34.600: E/AndroidRuntime(14153):    at com.ironone.streaming.MainActivity.onPause(MainActivity.java:567)
08-28 13:45:34.600: E/AndroidRuntime(14153):    at com.ironone.streaming.MainActivity.onBackPressed(MainActivity.java:866)

因此,根本原因在于您MainActivity.java文件中的第 567 行,似乎UIStateManager.getInsatance()接收器已被取消注册(在您的代码中的某处)。

这是一个解决同样问题的帖子。

于 2013-08-28T08:51:07.387 回答
0

你的错误就行了"onBackPressed(MainActivity.java:866)"。使用Ctrl+L转到它,请onBackpressed()在第 866 行提供代码。

此外,您的错误是“java.lang.IllegalArgumentException: Receiver not registered: "所以您要么过早地注销了您的听众,要么没有正确设置听众。

请尝试在粘贴错误日志时对其进行分页。我个人这样

编辑:感谢@trinimon 为您编辑代码:) 和@RiteshGune pour avoir éditer le mien :D

于 2013-08-28T08:27:07.370 回答