2011-11-01 127 views
0

首先,我知道这个问题已被问及答复很多次。如果有任何解决方案对我有用,我都不会转贴。Android - 从状态栏切换到活动

我有10个活动,我创建并切换到 开始闹钟之前的活动。

然后,我从上次活动中创造的alarmManager(用户与之进行交互),用下面的代码:

 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); 

因为我想报警创造我切换到主活动,报警后在后台运行。该的PendingIntent传递给alarmManager是下列服务:

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(即创建的第二个活动)的通知。一切都运行起来,直到用户点击通知。然后,程序崩溃,出现一个非描述性的“程序意外崩溃”,并将我返回到页面10之前的页面(而不是page1,我设置了pendingIntent)。

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

解决方案我曾尝试: - 设置各种的意图的标志,包括CLEAR_TASK,CLEAR_TOP,REORDER_ACTIVITY等 的-Tried是一定要完成()所有活动之前,我尝试从活动 重新启动它们 - 试图把代码发送到服务中的不同位置(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是无法找到第1页。

+0

使用'ADB logcat',DDMS,或在Eclipse中DDMS角度来考察logcat的,并期待在与相关的堆栈跟踪你的“程序意外崩溃”消息。 – CommonsWare

+0

添加了堆栈跟踪。 – user1022888

回答

0

你必须在org.scrumptious.slugmood.Page1onCreate()一个NullPointerException,在Page1.java源文件,行36