android - 异步任务的 ProgressBar Spinner 不起作用

标签 android android-asynctask android-progressbar

我的问题是,当我单击按钮“单击”以调用我的 API 时,它应该在我调用 API 时显示我的进度条(微调器)。相反,我的应用程序卡住不到一秒钟,然后当它完成调用时,它会短暂显示我的加载微调器(它只是闪烁)

这是我的代码

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>

最佳答案

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

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

关于android - 异步任务的 ProgressBar Spinner 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35904035/

相关文章:

android - 解析JSON数据并将其放入gridview中

android - 我如何将 Looper 与 asyncTask 一起使用?

Android AsyncTask 实例或类

android - 进度条在省电模式下消失(Android 5.x)?

javascript - Nativescript 是如何工作的?

android - 使用相机录制时抛出异常

android - 如何从 Android 中的 Drawable 将图像附加到彩信?

java - 应用程序在模拟器上运行良好,但在真实设备上崩溃

android - 捕获 Beaming 进度并显示在进度条上

android - ProgressBar 在后台任务中的数据源操作期间不旋转