启动activity:
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);
报错: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 intent = new Intent(this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);-----重要
startActivity(intent);
说明:启动的activity需要在AndroidManifest.xml中配置此activity的theme
<activity
android:name=".MainActivity"
android:theme="@style/AppTheme"/>