2012-11-25 47 views
2

我有延伸BroadCastReceiever和的onReceive方法的類如下:從類與Android設備意圖接收

public class Alarm extends BroadcastReceiver 
{ 
    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
     String message = intent.getStringExtra("message2"); 
     Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); 
     Intent intent1 = new Intent(context, AlarmShow.class); 
     context.startActivity(intent1); 

    } 
} 

但每次崩潰,它運行的onReceive方法,而不是開始在意向活動的。

我試過幾乎所有東西。

任何幫助,將不勝感激。

感謝

編輯:logcat的錯誤:

11-25 18:24:00.511: D/AndroidRuntime(2158): Shutting down VM 
11-25 18:24:00.511: W/dalvikvm(2158): threadid=1: thread exiting with uncaught exception (group=0x40015560) 
11-25 18:24:00.521: E/AndroidRuntime(2158): FATAL EXCEPTION: main 
11-25 18:24:00.521: E/AndroidRuntime(2158): java.lang.RuntimeException: Unable to start receiver com.example.androidalarm.Alarm: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? 
11-25 18:24:00.521: E/AndroidRuntime(2158):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:1805) 
11-25 18:24:00.521: E/AndroidRuntime(2158):  at android.app.ActivityThread.access$2400(ActivityThread.java:117) 
11-25 18:24:00.521: E/AndroidRuntime(2158):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:981) 
11-25 18:24:00.521: E/AndroidRuntime(2158):  at android.os.Handler.dispatchMessage(Handler.java:99) 
11-25 18:24:00.521: E/AndroidRuntime(2158):  at android.os.Looper.loop(Looper.java:123) 
11-25 18:24:00.521: E/AndroidRuntime(2158):  at android.app.ActivityThread.main(ActivityThread.java:3683) 
11-25 18:24:00.521: E/AndroidRuntime(2158):  at java.lang.reflect.Method.invokeNative(Native Method) 
11-25 18:24:00.521: E/AndroidRuntime(2158):  at java.lang.reflect.Method.invoke(Method.java:507) 
11-25 18:24:00.521: E/AndroidRuntime(2158):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
11-25 18:24:00.521: E/AndroidRuntime(2158):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
11-25 18:24:00.521: E/AndroidRuntime(2158):  at dalvik.system.NativeStart.main(Native Method) 
11-25 18:24:00.521: E/AndroidRuntime(2158): Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? 
11-25 18:24:00.521: E/AndroidRuntime(2158):  at android.app.ContextImpl.startActivity(ContextImpl.java:621) 
11-25 18:24:00.521: E/AndroidRuntime(2158):  at android.content.ContextWrapper.startActivity(ContextWrapper.java:258) 
11-25 18:24:00.521: E/AndroidRuntime(2158):  at android.content.ContextWrapper.startActivity(ContextWrapper.java:258) 
11-25 18:24:00.521: E/AndroidRuntime(2158):  at com.example.androidalarm.Alarm.onReceive(Alarm.java:20) 
11-25 18:24:00.521: E/AndroidRuntime(2158):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:1794) 
11-25 18:24:00.521: E/AndroidRuntime(2158):  ... 10 more 
11-25 18:24:02.611: I/Process(2158): Sending signal. PID: 2158 SIG: 9 
+0

發佈您的logcat錯誤,也許'message'是'null' ... – Sam

回答

1

測試信息不爲空,否則將有一個NullPointerException。 此外,如果你不使用這個下活動,您需要:

intent.addFlag(Intent.FLAG_ACTIVITY_NEW_TASK); 

或者你也會有暴跌。

+0

謝謝!有用! – user1851960

+0

不客氣:) – jaumard