安卓运行时异常 "Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag"

标签 android runtime-error

我在一个 ListView 中创建了多个布局,但是当我点击时我得到一个 AndroidRuntimeException“从 Activity 上下文外部调用 startActivity() 需要 FLAG_ACTIVITY_NEW_TASK 标志。这真的是你想要的吗?”

我正在添加

Intent.FLAG_ACTIVITY_NEW_TASK

我的 Intent ,但我得到了同样的信息! =(

      @Override
            public View getView(int position, View convertView, ViewGroup parent) {

                    retval=LayoutInflater.from(getApplicationContext()).inflate(R.layout.layout_anuncio, null);
                    ImageView image=(ImageView) retval.findViewById(R.id.imageAD);
                    LoadAds loadAds= new CargaAnuncios();
                    clickUrl = LoadAds.cargaImagenAnuncio(image, mContext, GlobalInfo.ANUNCIO_CARRIL_PORTADA);
                    image.setOnClickListener(new OnClickListener(){

                        @Override
                        public void onClick(View view) {
                            Bundle bundle=new Bundle();
                            bundle.putString("url", clickUrl);
                            Intent intent =new Intent(mContext,CustomWebView.class);
                            intent.putExtras(bundle);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            mContext.startActivity(intent);
                        }

                    });
            return retval;
        }

最佳答案

getApplicationContext() 替换为 this。最有可能的是,您应该在代码中的任何地方执行 getApplicationContext() - 只有在您特别需要 Application 时才使用 getApplicationContext() > 对象。

关于安卓运行时异常 "Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13978190/

相关文章:

c++ - Boost图库分布式广度优​​先搜索MPI_Unpack运行时报错

installation - 来自virtualenv中 'pip list'的声明错误。

android - 如何使用 Python 将 Android 发送的图像存储在 Google Cloud Storage 上

java - 无法正确返回 arraylist 对象

android - 集成 Facebook-android-sdk-3.0 抛出 Resources$NotFoundException

android - 无法调度 DDM block 46454154 : no handler defined - Eclipse - Android SDK

ios - 架构 arm64 的 undefined symbol : "_OBJC_CLASS_$_GANTracker

android - 将辅助功能事件传递给后台线程获取空值

android - Algolia NoClassDefFoundError jayway/jsonpath/Predicate

java - 无法反序列化对象。无法将类型 java.util.Date 的值转换为字符串