无法从onPostExecute启动活动

时间:2015-03-25 18:30:49

标签: android android-activity android-asynctask

我有AsyncTask我向服务器请求并下载一些数据。根据收到的数据,我在Activity A开始Activity BonPostExecute()

问题是不起作用。没有错误,只是活动无法启动。似乎代码根本没有在那里执行。

private class LoadingAsyncTask extends AsyncTask<Boolean, Void, Boolean> {

    protected Void doInBackground(Boolean... params) {
        if (InternetConnection.isConnected(ActivityLoading.this)) {
            .....load data
        } else {
            Toast.makeText(ActivityLoading.this, getString(R.string.no_internet_connection), Toast.LENGTH_SHORT).show();
        }

        isDone = true;
        return isDone;            
    }


    protected void onPostExecute(Void isDone) {
        super.onPostExecute(isDone);
        //if work is done
        if (isDone) {
            if (dwnlData.equals("success")) {
            getApplicationContext().startActivity(new Intent(ActivityLoading.this, ActivityNavigation.class));
        } else {
            getApplicationContext().startActivity(new Intent(ActivityLoading.this, ActivityAuthorization.class));
        }
    }
}

我在onCreate中调用了AsyncTask:

public class ActivityLoading extends ActionBarActivity {
static boolean isDone = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_loading);

    new LoadingAsyncTask().execute();

}

3 个答案:

答案 0 :(得分:2)

您是否尝试将isDone设为布尔值?

编辑 - 您可以让doInBackground不返回任何内容,并停止使用onPostExecute中的参数。

答案 1 :(得分:2)

将参数数据类型void更改为boolean

protected void onPostExecute(boolean isDone) {
    super.onPostExecute(isDone);
    //if work is done
    if (isDone) {
        if (dwnlData.equals("success")) {
        getApplicationContext().startActivity(new Intent(ActivityLoading.this, ActivityNavigation.class));
    } else {
        getApplicationContext().startActivity(new Intent(ActivityLoading.this, ActivityAuthorization.class));
    }
}

答案 2 :(得分:1)

我建议在if {}块中使用Log.d()或Toast()来验证您的条件是否为真