【问题标题】:Calling an Activity from asyncTask doInBackground从 asyncTask doInBackground 调用 Activity
【发布时间】:2016-01-29 04:09:17
【问题描述】:

你好,我从一个单独的 asyncTask class 中调用 base activity class,当我在 一台装有 android 4.4.2 的设备上运行应用程序时,它工作正常,但是当我在具有 的手机上运行它时>android 5.0 然后在此时从 asyncTask 应用程序调用活动崩溃。请问有人有解决办法吗?

public abstract class AbstractGetNameTask extends AsyncTask<Void, Void,Void>  
{
private static final String TAG = "TokenInfoTask";
protected ActivityLogin mActivity;
public static String GOOGLE_USER_DATA="No_data";
protected String mScope;
protected String mEmail;
protected int mRequestCode;
private ProgressDialog pDialog;
 private Context mContext;



AbstractGetNameTask(ActivityLogin activity, String email, String      
scope,Context context) {
    this.mActivity = activity;
    this.mScope = scope;
    this.mEmail = email;
    this.mContext=context;

}   

@Override
protected void onPreExecute() {
    // TODO Auto-generated method stub
    pDialog = new ProgressDialog(mContext);
    pDialog.setTitle("Loading....");
    pDialog.setMessage("Please wait...");
    pDialog.setCancelable(false);
    pDialog.setMax(100);
    pDialog.show();
    super.onPreExecute();
}




@Override
protected Void doInBackground(Void... params) {
try {
fetchNameFromProfileServer();   // here in this method i start new activity

    } catch (IOException ex) {
        onError("Following Error occured, please try again. "
                + ex.getMessage(), ex);
    } catch (JSONException e) {
        onError("Bad response: " + e.getMessage(), e);
    }
    return null;
}
}

我的安卓清单有

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />

但它从不打扰我其他应用程序

【问题讨论】:

  • 你的 logcat 在哪里??
  • 我告诉过你,在调试模式下它工作正常......但在安装 apk 时它不起作用
  • 把你捕捉到的日志放在try-catch块中。
  • 应用安装后仍会记录到 logcat。
  • 另外,从后台执行开始活动也不是一个好主意,请在 postExecute 上执行

标签: android android-intent android-asynctask


【解决方案1】:

您应该从您的onPostExecute 开始活动。您只需要从fetchNameFromProfileServer() 返回结果,而不是从那里开始活动并将结果传递给onPostExecute,然后开始活动。

【讨论】:

  • 你能把代码贴在这里吗?你是怎么做到的?
  • 我真正的问题是它适用于较低版本的 android 说 4.4.2 在一部手机上但不适用于具有 android 5.0 的手机。我有两个设备
  • 在我的清单中 -
  • 你不应该使用targetSdkVersion 21。如果你必须在android 5.0中使用它?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多