启动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"/>
Android启动Activity添加FLAG_ACTIVITY_NEW_TASK标志
博客主要讲述在Android中启动Activity时遇到的问题。直接启动Activity会报错,提示从非Activity上下文调用startActivity()需要FLAG_ACTIVITY_NEW_TASK标志。正确方式是在Intent中添加该标志,同时启动的Activity需在AndroidManifest.xml中配置theme。
1625

被折叠的 条评论
为什么被折叠?



