首先,我知道這個問題已被問及答覆很多次。如果有任何解決方案對我有用,我都不會轉貼。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頁。
使用'ADB logcat',DDMS,或在Eclipse中DDMS角度來考察logcat的,並期待在與相關的堆棧跟蹤你的「程序意外崩潰」消息。 – CommonsWare
添加了堆棧跟蹤。 – user1022888