Android:ProgressBar导致AsyncTask出现故障

Android:ProgressBar导致AsyncTask出现故障,android,android-asynctask,android-progressbar,Android,Android Asynctask,Android Progressbar,在我的活动中,我必须使用HttpPost获得一个大的JSON~600KB。这需要一些时间来下载和分配内存,所以我在一个异步任务中完成。我有一个TextView,它简单地说加载到我的XML中: <TextView android:id="@+id/loader_tv" android:text="@string/loading" android:layout_height="wrap_content" android:l

在我的活动中,我必须使用HttpPost获得一个大的JSON~600KB。这需要一些时间来下载和分配内存,所以我在一个异步任务中完成。我有一个TextView,它简单地说加载到我的XML中:

    <TextView
        android:id="@+id/loader_tv"
        android:text="@string/loading"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_marginTop="15dip"
        android:layout_alignLeft="@id/shipid_tv"
        android:layout_marginLeft="50dip"
        android:layout_below="@id/shipid_tv"
        android:textSize="18sp" />
所有这些工作都做得很好

现在,我注释掉了TextView并放置了一个进度条:

在onCreate中:

以及loading.setVisibilityView.GONE;线路显然保持不变

这样做会让我的程序失控


ProgressBar会出现,但JSON不会开始下载。如果我等待的时间太长~1分钟,不会超过5秒,我会收到连接超时的异常。更奇怪的是,如果我不等待太久就按back,它会返回到上一个活动并立即开始下载JSON。我不明白progressbar是如何影响代码的doInBackground部分的,我甚至没有在该函数中提到它。我已经反复评论了progressbar,并将textview放回原处,反之亦然,只要它在textview上就可以正常工作,而在progressbar上就不工作

我被难住了,有什么解决办法吗

编辑:

我的背景如下:

protected JSONObject doInBackground(Void... params){

    String s;
    String keyval=intent_data;
    String orgcode=MainActivity.un.getText().toString();
    JSONObject jsonobj=null, shipmentcodesend=null;
    try{
        jsonobj=new JSONObject("someHeavyJson");
    }
    catch(JSONException je){
        Log.e("ShipmentEventFindingActivity.EventFindAsyncTask","JSON error" , je);
        ErrorDialog displayerror=new ErrorDialog(je, context);
        displayerror.showDialog();
    }

    System.out.println("Made jsonobject");
    System.out.println(jsonobj.toString());

    JSONConnectorPost connector1=new JSONConnectorPost(jsonobj, getString(R.string.PIDsearch_url), MainActivity.cookieStore.getCookies());
    JSONObject result=connector1.connectClient();
    s=result.toString();

    try{
        String cacheID=result.getString("cacheId");
        shipmentcodesend=new JSONObject("andMoreJson");
    }
    catch(JSONException je){
        Log.e("ShipmentEventFindingActivity.EventFindAsyncTask","JSON error" , je);
        ErrorDialog displayerror=new ErrorDialog(je, context);
        displayerror.showDialog();
    }

    System.out.println("Made shipmentcodesend");
    System.out.println(shipmentcodesend.toString());

    JSONConnectorPost connector2=new JSONConnectorPost(shipmentcodesend, getString(R.string.PIDsearch_url),MainActivity.cookieStore.getCookies());
    shipmentcodes=connector2.connectClient();

    return result;
}

您需要在异步任务中包含以下内容:

private final ProgressBar progressBar;
private final TextView textView;
public LoadingTask(TextView textView, ProgressBar progressBar) {
        this.progressBar = progressBar;
        this.textView = textView;
}

@Override
protected Integer doInBackground(Integer... params) {
   //Your post request
}

@Override
protected void onProgressUpdate(Integer... values) {
    super.onProgressUpdate(values);
    progressBar.setProgress(values[0]); // This is ran on the UI thread so it is ok to update our progress bar ( a UI view ) here
}

发布你的do asyntask/@Rod_Algonquin我一开始打算发布,但它有点大,我会在这里发布,不管怎样,所有更新UI的事情都不能在doInBackground中完成。所以displayerror.showDialog;将导致崩溃。@WarrenFaith同意,但当有加载文本视图时,它永远不会被捕获,为什么当我使用进度条时它会被捕获并崩溃?它甚至没有出现异常,progressbar只是继续加载。我遇到了一个连接超时的异常,我首先要解决这个问题:
//TextView loading;
ProgressBar loading;
//loading=(TextView)findViewById(R.id.loader_tv);
loading=(ProgressBar)findViewById(R.id.loading_pb);
protected JSONObject doInBackground(Void... params){

    String s;
    String keyval=intent_data;
    String orgcode=MainActivity.un.getText().toString();
    JSONObject jsonobj=null, shipmentcodesend=null;
    try{
        jsonobj=new JSONObject("someHeavyJson");
    }
    catch(JSONException je){
        Log.e("ShipmentEventFindingActivity.EventFindAsyncTask","JSON error" , je);
        ErrorDialog displayerror=new ErrorDialog(je, context);
        displayerror.showDialog();
    }

    System.out.println("Made jsonobject");
    System.out.println(jsonobj.toString());

    JSONConnectorPost connector1=new JSONConnectorPost(jsonobj, getString(R.string.PIDsearch_url), MainActivity.cookieStore.getCookies());
    JSONObject result=connector1.connectClient();
    s=result.toString();

    try{
        String cacheID=result.getString("cacheId");
        shipmentcodesend=new JSONObject("andMoreJson");
    }
    catch(JSONException je){
        Log.e("ShipmentEventFindingActivity.EventFindAsyncTask","JSON error" , je);
        ErrorDialog displayerror=new ErrorDialog(je, context);
        displayerror.showDialog();
    }

    System.out.println("Made shipmentcodesend");
    System.out.println(shipmentcodesend.toString());

    JSONConnectorPost connector2=new JSONConnectorPost(shipmentcodesend, getString(R.string.PIDsearch_url),MainActivity.cookieStore.getCookies());
    shipmentcodes=connector2.connectClient();

    return result;
}
private final ProgressBar progressBar;
private final TextView textView;
public LoadingTask(TextView textView, ProgressBar progressBar) {
        this.progressBar = progressBar;
        this.textView = textView;
}

@Override
protected Integer doInBackground(Integer... params) {
   //Your post request
}

@Override
protected void onProgressUpdate(Integer... values) {
    super.onProgressUpdate(values);
    progressBar.setProgress(values[0]); // This is ran on the UI thread so it is ok to update our progress bar ( a UI view ) here
}