用于异步任务的ProgressBar Spinner不起作用

时间:2016-03-09 22:32:49

标签: android android-asynctask android-progressbar

我的问题是当我点击按钮"点击"要调用我的API,它应该在我调用API时显示我的progressBar(微调器)。相反,我的应用程序冻结不到一秒钟,然后当它完成调用它显示我的加载微调器一小段时间(它只是闪烁)

这是我的代码

private ProgressBar spinner;
public View onCreateView(...)
{
    spinner = (ProgressBar) view.findViewById(R.id.progressBar1);
    spinner.setVisibility(View.GONE);
    ...
}



private class MyTask extends AsyncTask<String, Void, Void> {

    String pageContent = "";
    DataOutputStream wr;


    @Override
    protected void onPreExecute() {
        spinner.setVisibility(View.VISIBLE);
    }

    @Override
    public Void doInBackground(String... params) {

        requestResult.setSuccess(false);
        HttpURLConnection connection;

        try {
            String url = "myURL";

            //I only call my Api here. I delete rest so this won't bother you

            requestResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        JsonParser parser = new JsonParser();
        //Same here i delete nonimportant 

        MyResponse = new Gson().fromJson(jsonContent, MyResponse.class);
        done();
        spinner.setVisibility(View.GONE);
    }

}

我尝试过不同的东西,它总是冻结一秒钟,然后我的加载微调器闪存,我的API内容显示出来。

你能帮助我吗?

我的xml文件

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scroll_view_send"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <RelativeLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:minWidth="25px"
        android:minHeight="25px">
        <ProgressBar
            android:id="@+id/progressBar1"
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_below="@id/buttonSend"
            android:visibility="gone"/>
    </RelativeLayout>
</ScrollView>

2 个答案:

答案 0 :(得分:2)

尝试下面的代码段。它不需要在布局文件中声明微调器。

AsyncTask<String, String, String> asyncObject =
new AsyncTask<String, String, String>() {
    ProgressDialog progDailog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progDailog =
                new ProgressDialog(Activity.this);
        progDailog.setMessage("Loading");
        progDailog.setCancelable(false);
        progDailog.show();
    }

    @Override
    protected String doInBackground(String... urls) {
        //Do background stuff here
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        progDailog.cancel();
        //Do post background stuff here.
    }
};
asyncObject.execute(null, null, null);

答案 1 :(得分:1)

请尝试此工作示例,将spinner传递给MyTask,如下所示:

private class MyTask extends AsyncTask<String, Void, Void> {

    String pageContent = "";
    DataOutputStream wr;

    private final ProgressBar progress;

    public MyTask(final ProgressBar progress) {
        this.progress = progress;
    }


    @Override
    protected void onPreExecute() {
        progress.setVisibility(View.VISIBLE);
    }

    ...

    @Override
    protected void onPostExecute(Void result) {
        JsonParser parser = new JsonParser();
        //Same here i delete nonimportant 

        MyResponse = new Gson().fromJson(jsonContent, MyResponse.class);
        done();
        progress.setVisibility(View.GONE);
    }
}

然后拨打new MyTask(progress).execute();

编辑:在您的问题中,您提到您的手机在调用MyTask时冻结... 请检查此步骤以避免在AsyncTask中冻结UI:

  1. 请勿使用新的MyTask
  2. 致电MyTask().get()
  3. 尝试转移到doInBackground此部分MyResponse = new Gson().fromJson(jsonContent, MyResponse.class); done();,这可能会很昂贵。
  4. 希望它的帮助!!