0

I have an application that has an Asynctask for web Api Requests and and there are many request for many tabs.

When user scrolls very fast between tabs many request lunches! but when user closes the activity, some ongoing do in background causes app crashes.

I tried this: in my doinBackground I always check if fragment isAdded()

 @Override
 protected Void doInBackground(Void... Params) {
 Bundle bundle = getArguments();
 String url = ServiceHandler.serverEnum.serverRootPath + ServiceHandler.serverEnum.Me.toString();


 if(isAdded()){
            response = StaticUtils.generateReguest(mContext, url, headerParams, bodyParams, URLParams, null, ServiceHandler.serviceMethod.get.toString());
        }

    return null;
}

I also do this when Asynctask runs from Activity: onStop of activity I canceled the ran Asyctask then onPostExecute of it check if not canceled do some UI stuff. but problem is I get crash in doInBackground.

this is the line that crash happens in creating respond in StaticUtils.generateReguest:

if (status != 200 && status != 401 && status != 404 && status != 0) {
            Intent intent = new Intent(context, ErrorActivity.class); //error point to this line
            intent.putExtra(ErrorActivity.REASON, "SERVER");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            (context).startActivity(intent);
        }

and this is log cat that lead to above line:

   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference

I think it may because of context. cause when I generate a request I pass Activity context to Requestgenerator and some time as you see in code it lead to show error activity. when user closese the activity context changed so Request generator can not open `ErrorActivity. what is best practice to handle this? I googled and could not find a good solution?

Mahdi
  • 6,139
  • 9
  • 57
  • 109

3 Answers3

0

I think context is null. why don't have a null check for context.

      if(context != null){
          Intent intent = new Intent(context, ErrorActivity.class); 
          intent.putExtra(ErrorActivity.REASON, "SERVER");
          intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          (context).startActivity(intent);
      }
Manasa
  • 11
  • 2
0

Your context is null, I am not sure of how you are passing the Context. But I would recommend this way,

public class YourActivity extends Application{
    Context appContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_two);
        appContext=getApplicationContext();
MyAsynchClass mAsynch= new MyAsynchClass(appContext);
mAsynch.execute();
    }

And then use that appContext from your constructor.

If the above does not work then use this,

From this users answer,

<application android:name="com.xyz.MyApplication">

</application>

public class MyApplication extends Application {

    private static Context context;

    public void onCreate() {
        super.onCreate();
        MyApplication.context = getApplicationContext();
    }

    public static Context getAppContext() {
        return MyApplication.context;
    } }
Community
  • 1
  • 1
oldcode
  • 1,669
  • 3
  • 22
  • 41
  • I do some thing like this. read the question once again. in mid of Asynctask activity changes – Mahdi Jan 18 '17 at 09:41
0

1) Put a debugger point at that place and check which variable is getting null. Looks like its context which is null, but still the check it once.

2) If this doesnot work, use the same function ie

if (status != 200 && status != 401 && status != 404 && status != 0) {
    Intent intent = new Intent(context, ErrorActivity.class);
        intent.putExtra(ErrorActivity.REASON, "SERVER");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

in post exceute. I am not sure but since do in background work in background thread and not on UI thread it is not able to process the UI processings.

Shuddh
  • 1,920
  • 2
  • 19
  • 30