Asynctask结果在activty关闭后导致崩溃

时间:2017-01-18 08:50:31

标签: android android-asynctask crash request android-context

我的应用程序对于网络Api请求有Asynctask,并且有许多标签请求。

当用户在标签之间滚动非常快时,许多请求午餐!但是当用户关闭活动时,一些正在进行的活动会导致应用程序崩溃。

我试过这个: 在我的doinBackground中,我总是检查片段是否已添加()

 @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;
}

当Asynctask从Activity运行时,我也会这样做: onStop of activity我取消了运行Asyctask然后onPostExecute检查是否取消了做一些UI的东西。但问题是我在doInBackground崩溃了。

这是在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);
        }

这是导致上面一行的log cat:

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

我认为这可能是因为背景。因为当我生成一个请求时,我将Activity上下文传递给Requestgenerator,并且在代码中看到它会导致显示错误活动。当用户关闭活动上下文发生变化时,请求生成器无法打开`ErrorActivity。处理这个问题的最佳做法是什么?我用谷歌搜索,找不到一个好的解决方案?

3 个答案:

答案 0 :(得分:0)

我认为上下文为空。 为什么不对上下文进行空检查。

      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);
      }

答案 1 :(得分:0)

您的context为空,我不确定您是如何传递上下文的。但我会这样推荐,

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();
    }

然后使用构造函数中的appContext

如果上述方法不起作用,请使用此

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;
    } }

答案 2 :(得分:0)

1)在该位置放置调试器点并检查哪个变量为空。看起来它的上下文为null,但仍然检查一次。

2)如果这不起作用,请使用相同的功能,即

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);
    }

在帖子中。我不确定,但是因为在后台线程的后台工作而不是UI线程,它无法处理UI处理。