Calling startActivity() from outside of an Activity context requires the FLAG_A

10-21 01:05:37.121: I/TestRunner(1224): ----- begin exception -----
10-21 01:05:37.131: I/TestRunner(1224): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
10-21 01:05:37.131: I/TestRunner(1224): 	at android.app.ContextImpl.startActivity(ContextImpl.java:847)
10-21 01:05:37.131: I/TestRunner(1224): 	at android.content.ContextWrapper.startActivity(ContextWrapper.java:276)
10-21 01:05:37.131: I/TestRunner(1224): 	at com.hello.jifenshenqi.common.Utils.startApp(Utils.java:55)
10-21 01:05:37.131: I/TestRunner(1224): 	at com.hello.jifenshenqi.test.DemoTest.test_1_taobao(DemoTest.java:50)
10-21 01:05:37.131: I/TestRunner(1224): 	at java.lang.reflect.Method.invokeNative(Native Method)
10-21 01:05:37.131: I/TestRunner(1224): 	at java.lang.reflect.Method.invoke(Method.java:511)
10-21 01:05:37.131: I/TestRunner(1224): 	at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
10-21 01:05:37.131: I/TestRunner(1224): 	at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
10-21 01:05:37.131: I/TestRunner(1224): 	at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:186)
10-21 01:05:37.131: I/TestRunner(1224): 	at junit.framework.TestCase.runBare(TestCase.java:127)
10-21 01:05:37.131: I/TestRunner(1224): 	at junit.framework.TestResult$1.protect(TestResult.java:106)
10-21 01:05:37.131: I/TestRunner(1224): 	at junit.framework.TestResult.runProtected(TestResult.java:124)
10-21 01:05:37.131: I/TestRunner(1224): 	at junit.framework.TestResult.run(TestResult.java:109)
10-21 01:05:37.131: I/TestRunner(1224): 	at junit.framework.TestCase.run(TestCase.java:118)
10-21 01:05:37.131: I/TestRunner(1224): 	at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
10-21 01:05:37.131: I/TestRunner(1224): 	at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
10-21 01:05:37.131: I/TestRunner(1224): 	at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:537)
10-21 01:05:37.131: I/TestRunner(1224): 	at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1551)
10-21 01:05:37.131: I/TestRunner(1224): ----- end exception -----

 

原因分析:

Context中有一个startActivity方法,Activity继承自Context,重载了startActivity方法。如果使用 Activity的startActivity方法,不会有任何限制,而如果使用Context的startActivity方法的话,就需要开启一个新的task,遇到上面那个异常的,都是因为使用了Context的startActivity方法。解决办法是,加一个flag。intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

 

解决办法:

Intent intent = new Intent();
intent.setClass(mContext, PhotoView.class);
Bundle bundle = new Bundle();
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
mContext.startActivity(intent);

 

你可能感兴趣的:(Calling startActivity() from outside of an Activity context requires the FLAG_A)