天天看點

Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activit

在Context中有一個startActivity()方法,在activity中使用startActivity()方法時是沒有任何問題的,這是因為activity繼承自Context,并且重載了startActivity()方法。但是在Context中直接使用startActivity()方法時會報:

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?
           

解決的方法是在intent中加一個flag即可。

mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);