AndroidRuntimeException “Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag”

会有一股神秘感。 提交于 2019-11-30 23:52:30

Replace getApplicationContext() with this. Most likely, you should do that everywhere in your code that you have getApplicationContext() -- only use getApplicationContext() when you specifically need the Application object.

Old thread, but thought this easy solution might help someone. Instead of passing context around, just get it from view. For eg: view.getContext()

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

I have added:

parent.getApplicationContext()

Instead of just:

getApplicationContext()

Whole line is:

retval=LayoutInflater.from(parent.getApplicationContext()).inflate(R.layout.layout_anuncio, null);

The solution of Warren Lankie Van Tonder is almost the good:

-You should avoid using Activity context and choose Application Context instead, in purpose to prevent memory leaks. This blog of an Android developer explains that http://android-developers.blogspot.be/2009/01/avoiding-memory-leaks.html

-But in the case of Activity context is the only solution for your code (maybe for calling another activity from outside an activity) and regarding the link above, you have to release the static reference in each onPause() with

AppGlobals.setAppContext(null);

In return, set the static field in onResume and not onCreate.

Thanks to CommonsWare i came up with this solution which worked best.

I added a new class

package com.test.test;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

public class AppGlobals extends Activity {
    private static Context appContext;

    public static Context getAppContext(){
        return appContext;
    }

    public static void setAppContext(Context context){
        appContext = context;
    }

    public static void StartActivity(Intent intent){
        appContext.startActivity(intent);
    }
}

All i needed to do then was add the below code on each activity onCreate that i navigated to.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    AppGlobals.setAppContext(this);
}

This allowed the new intent to start as if it working with normal application flow and this way i didn't have to set a Flag for the intent.

Calling the start activity method is as easy as:

Intent totalTimerIntent = new Intent(AppGlobals.getAppContext(), TotalTimer.class);
AppGlobals.StartActivity(totalTimerIntent);

Hope this helps someone as it has helped me.

Thank you CommonsWare.

Murtaza Ashraf
CustomAdapter mAdapter = new CustomAdapter( getApplicationContext(), yourlist);

or

Context mContext = getAppliactionContext();

CustomAdapter mAdapter = new CustomAdapter( mContext, yourlist);

change to below

CustomAdapter mAdapter = new CustomAdapter( this, yourlist);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!