【问题标题】:Android - Switching to activity from status barAndroid - 从状态栏切换到活动
【发布时间】:2011-12-19 03:51:57
【问题描述】:

首先,我知道这个问题已经被问过很多次了。如果这些解决方案中的任何一个对我有用,我就不会转发。

我创建了 10 个活动并切换到了 启动警报。

然后我使用以下代码从最后一个活动(用户与之交互)中创建警报管理器:

        nextButton.setOnClickListener(new Button.OnClickListener(){ 
            @Override
            //Code adapted from example at android-er.blogspot.com
            public void onClick(View arg0) {
                //Declare the intent to start a new service
                Intent myIntent = new Intent(Page10.this, AlarmService.class);
                pendingIntent = PendingIntent.getService(Page10.this, 0, myIntent, 0);

                //Create the alarm manager and connect it to ALARM_SERVICE
                AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

                //Use the calendar to time the action of the alarm
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(System.currentTimeMillis());
                calendar.add(Calendar.SECOND, 10);

                //Set the alarm to buzz after the time defined in calendar
                alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
                Toast.makeText(Page10.this, "Start Alarm", Toast.LENGTH_LONG).show();

                //Start the main activity, the alarm service is now running in the background
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_HOME);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);

创建警报后,我切换到主要活动,因为我希望警报在后台运行。传递给 alarmManager 的 pendingIntent 是以下服务:

public class AlarmService extends Service {
    @Override
    public void onCreate() {
        //Get a notification manager
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager SlugNotificationManager = (NotificationManager) getSystemService(ns);

        //Instantiate the notification
        int icon = R.drawable.slugmoodtextless;
        CharSequence tickerText = "Survey Ready";
        long when = System.currentTimeMillis();
        Notification notification = new Notification(icon, tickerText, when);

        //Set notification messages and PendingIntent
        Context context = getApplicationContext();
        CharSequence contentTitle = "Slugmood";
        CharSequence contentText = "Survey Ready!";
        Intent notificationIntent = new Intent(this, Page1.class);
        notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

        //Pass notification to notification manager
        final int HELLO_ID = 1;
        SlugNotificationManager.notify(HELLO_ID, notification);

        Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG).show();
    }

为了简洁起见,我没有包含其他覆盖的方法,因为我的服务不使用它们。所以我希望我的服务在警报响起时向状态栏发送通知,然后用户可以单击通知返回到 page1(创建的第二个活动)。一切正常,直到用户单击通知。然后程序因无法描述的“程序意外崩溃”而崩溃,并将我返回到 page10 之前的页面(不是 page1,我在其中设置了 pendingIntent)。

现在,我最初只是希望服务立即启动 page1 活动。当我简单地从服务启动 page1 活动时,使用通知系统是我解决完全相同的崩溃的解决方案。

我尝试过的解决方案: - 在意图上设置各种标志,包括 CLEAR_TASK、CLEAR_TOP、REORDER_ACTIVITY 等 - 在我尝试从活动中重新启动它们之前,尝试确保完成()所有活动 -尝试将代码发送到服务中的各个位置(onCreate、onStart 等) -尝试恢复活动而不是重新启动它们。 - 以及 StackOverflow 上提到的关于从服务启动活动、使用状态栏等几乎所有其他内容。

所以我很难过。如果有人知道如何处理这个问题,我将非常感谢您的帮助。

编辑:这是完整的堆栈跟踪:

11-01 04:58:21.111: ERROR/Zygote(32): setreuid() failed. errno: 2
11-01 04:58:28.332: ERROR/Zygote(32): setreuid() failed. errno: 17
11-01 04:58:29.412: ERROR/BatteryService(58): usbOnlinePath not found
11-01 04:58:29.412: ERROR/BatteryService(58): batteryVoltagePath not found
11-01 04:58:29.412: ERROR/BatteryService(58): batteryTemperaturePath not found
11-01 04:58:29.431: ERROR/SurfaceFlinger(58): Couldn't open /sys/power/wait_for_fb_sleep or /sys/power/wait_for_fb_wake
11-01 04:58:35.131: ERROR/EventHub(58): could not get driver version for /dev/input/mouse0, Not a typewriter
11-01 04:58:35.131: ERROR/EventHub(58): could not get driver version for /dev/input/mice, Not a typewriter
11-01 04:58:35.382: ERROR/System(58): Failure starting core service
11-01 04:58:35.382: ERROR/System(58): java.lang.SecurityException
11-01 04:58:35.382: ERROR/System(58):     at android.os.BinderProxy.transact(Native Method)
11-01 04:58:35.382: ERROR/System(58):     at android.os.ServiceManagerProxy.addService(ServiceManagerNative.java:146)
11-01 04:58:35.382: ERROR/System(58):     at android.os.ServiceManager.addService(ServiceManager.java:72)
11-01 04:58:35.382: ERROR/System(58):     at com.android.server.ServerThread.run(SystemServer.java:184)
11-01 04:58:36.081: ERROR/SoundPool(58): error loading /system/media/audio/ui/Effect_Tick.ogg
11-01 04:58:36.081: ERROR/SoundPool(58): error loading /system/media/audio/ui/KeypressStandard.ogg
11-01 04:58:36.081: ERROR/SoundPool(58): error loading /system/media/audio/ui/KeypressSpacebar.ogg
11-01 04:58:36.091: ERROR/SoundPool(58): error loading /system/media/audio/ui/KeypressDelete.ogg
11-01 04:58:36.091: ERROR/SoundPool(58): error loading /system/media/audio/ui/KeypressReturn.ogg
11-01 04:58:37.572: ERROR/ThrottleService(58): Could not open GPS configuration file /etc/gps.conf
11-01 04:58:38.641: ERROR/logwrapper(147): executing /system/bin/tc failed: No such file or directory
11-01 04:58:38.691: ERROR/logwrapper(148): executing /system/bin/tc failed: No such file or directory
11-01 04:58:38.702: ERROR/logwrapper(149): executing /system/bin/tc failed: No such file or directory
11-01 04:58:46.651: ERROR/HierarchicalStateMachine(58): TetherMaster - unhandledMessage: msg.what=3
11-01 05:00:22.672: ERROR/AndroidRuntime(274): FATAL EXCEPTION: main
11-01 05:00:22.672: ERROR/AndroidRuntime(274): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.scrumptious.slugmood/org.scrumptious.slugmood.Page1}: java.lang.NullPointerException
11-01 05:00:22.672: ERROR/AndroidRuntime(274):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-01 05:00:22.672: ERROR/AndroidRuntime(274):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-01 05:00:22.672: ERROR/AndroidRuntime(274):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-01 05:00:22.672: ERROR/AndroidRuntime(274):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-01 05:00:22.672: ERROR/AndroidRuntime(274):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-01 05:00:22.672: ERROR/AndroidRuntime(274):     at android.os.Looper.loop(Looper.java:123)
11-01 05:00:22.672: ERROR/AndroidRuntime(274):     at android.app.ActivityThread.main(ActivityThread.java:4627)
11-01 05:00:22.672: ERROR/AndroidRuntime(274):     at java.lang.reflect.Method.invokeNative(Native Method)
11-01 05:00:22.672: ERROR/AndroidRuntime(274):     at java.lang.reflect.Method.invoke(Method.java:521)
11-01 05:00:22.672: ERROR/AndroidRuntime(274):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-01 05:00:22.672: ERROR/AndroidRuntime(274):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-01 05:00:22.672: ERROR/AndroidRuntime(274):     at dalvik.system.NativeStart.main(Native Method)
11-01 05:00:22.672: ERROR/AndroidRuntime(274): Caused by: java.lang.NullPointerException
11-01 05:00:22.672: ERROR/AndroidRuntime(274):     at org.scrumptious.slugmood.Page1.onCreate(Page1.java:36)
11-01 05:00:22.672: ERROR/AndroidRuntime(274):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-01 05:00:22.672: ERROR/AndroidRuntime(274):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-01 05:00:22.672: ERROR/AndroidRuntime(274):     ... 11 more
11-01 05:05:15.972: ERROR/AndroidRuntime(286): FATAL EXCEPTION: main
11-01 05:05:15.972: ERROR/AndroidRuntime(286): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.scrumptious.slugmood/org.scrumptious.slugmood.Page1}: java.lang.NullPointerException
11-01 05:05:15.972: ERROR/AndroidRuntime(286):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-01 05:05:15.972: ERROR/AndroidRuntime(286):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-01 05:05:15.972: ERROR/AndroidRuntime(286):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-01 05:05:15.972: ERROR/AndroidRuntime(286):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-01 05:05:15.972: ERROR/AndroidRuntime(286):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-01 05:05:15.972: ERROR/AndroidRuntime(286):     at android.os.Looper.loop(Looper.java:123)
11-01 05:05:15.972: ERROR/AndroidRuntime(286):     at android.app.ActivityThread.main(ActivityThread.java:4627)
11-01 05:05:15.972: ERROR/AndroidRuntime(286):     at java.lang.reflect.Method.invokeNative(Native Method)
11-01 05:05:15.972: ERROR/AndroidRuntime(286):     at java.lang.reflect.Method.invoke(Method.java:521)
11-01 05:05:15.972: ERROR/AndroidRuntime(286):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-01 05:05:15.972: ERROR/AndroidRuntime(286):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-01 05:05:15.972: ERROR/AndroidRuntime(286):     at dalvik.system.NativeStart.main(Native Method)
11-01 05:05:15.972: ERROR/AndroidRuntime(286): Caused by: java.lang.NullPointerException
11-01 05:05:15.972: ERROR/AndroidRuntime(286):     at org.scrumptious.slugmood.Page1.onCreate(Page1.java:36)
11-01 05:05:15.972: ERROR/AndroidRuntime(286):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-01 05:05:15.972: ERROR/AndroidRuntime(286):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-01 05:05:15.972: ERROR/AndroidRuntime(286):     ... 11 more

我仍然不完全确定我的问题是什么,尽管 Android 似乎无法找到 page1。

【问题讨论】:

  • 在 Eclipse 中使用 adb logcat、DDMS 或 DDMS 透视图检查 LogCat 并查看与“程序意外崩溃”消息相关的堆栈跟踪。

标签: android service android-activity notifications alarmmanager


【解决方案1】:

org.scrumptious.slugmood.Page1onCreate() 中有一个NullPointerException,在Page1.java 源文件的第36 行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-09
    • 1970-01-01
    • 1970-01-01
    • 2018-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多