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?
Context中有一个startActivity方法,Activity继承自Context,重载了startActivity方法。如果使用 Activity的startActivity方法,不会有任何限制,而如果使用Context的startActivity方法的话,就需要开启一个新的task,遇到上面那个异常的,都是因为使用了Context的startActivity方法。解决办法是,加一个flag。
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
以上转自:http://www.cnblogs.com/rioder/archive/2011/11/02/2233584.html
以下为Context源码中的startActivity:
/**
* Launch a new activity. You will not receive any information about when
* the activity exits.
*
* <p>Note that if this method is being called from outside of an
* {@link android.app.Activity} Context, then the Intent must include
* the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag. This is because,
* without being started from an existing Activity, there is no existing
* task in which to place the new activity and thus it needs to be placed
* in its own separate task.
*
* <p>This method throws {@link ActivityNotFoundException}
* if there was no Activity found to run the given Intent.
*
* @param intent The description of the activity to start.
*
* @throws ActivityNotFoundException
*
* @see PackageManager#resolveActivity
*/
public abstract void startActivity(Intent intent);
以上解释了为什么使用Context的startActivity方法(比如在Service中或者BroadcastReceiver中启动Activity)为什么需要添加flag:FLAG_ACTIVITY_NEW_TASK
分享到:
相关推荐
W/System.err: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? W/System.err: at...
android.util.Base64类
Tomcat内存溢出的解决方法(java.util.concurrent.ExecutionException:java.lang.OutOfMemoryError),内附解决方案!
予org.jasig.cas.client.util.CommonUtils 加入 public static void disableSSLVerification(){ try { // Create a trust manager that does not validate certificate chains TrustManager[] ...
应用.activity 此包中包含:页面用到的Activity类 com.qqq.android.应用.handler 此包中包含:解析服务器端返回信息的Handler类 com.qqq.android.应用.bean 此包中包含:程序中要用到的JavaBean类 com.qqq.android....
Exception in thread “main“ java.util.InputMismatchException
android.util.Log is the most usable library of the Android. But, when the app released on the market, some important information is clearly forgotten by the developer. All logs are disabled by Debug...
android.support.v4.util.Pools使用的Demo,详情见博客:http://blog.csdn.net/u012810020/article/details/52915922
Android Base64Jar包及Java完整源码 包含:android android.util.Base64 类, BASE64编码、解码算法;包含该类的完整Jar包。 可以直接导入Jar包或者引用类及类中相关方法。 很不错的工具类。
真实的Android Base64的jar。下载既可以使用。在Java项目内亦可以。android-util-0.6.0.jar
java.util.stream.Stream:Stream 代码示例
发现问题 早上起来报错误,Jenkins打包到tomcat服务器,死活启动不起来,一些定时任务也没跑成功。 报错如下: org.apache.catalina.startup.ContextConfig.beforeStart ... at java.util.zip.ZipFile.(ZipFi
主要介绍了java.util.concurrent.ExecutionException 问题解决方法的相关资料,需要的朋友可以参考下
java.util.ConcurrentModificationException 异常问题详解1
C# Emgu.CV人脸检测与识别,MCvAvgComp[][] facesDetected = currentFrame.DetectHaarCascade(Face, 1.2, 10, Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(20, 20));
com/sun/mail/util/LineInputStream错误 原因: MyEclipse6.5的javaee.jar中的mail包与JavaMail包有冲突。 解决: 在MyEclipse目录下(D:\Program Files\MyEclipse 6.5\myeclipse\eclipse\plugins\...
2009-8-16 10:18:39 org.directwebremoting.util.CommonsLoggingOutput info 信息: DWR Version 2.0.5 starting. 2009-8-16 10:18:39 org.directwebremoting.util.CommonsLoggingOutput info 信息: - Servlet Engine...