我想创建一个在1分钟内填满进度条的应用程序,但它在前5秒内崩溃

时间:2018-12-17 17:29:37

标签: java android android-studio progress-bar

这是布局文件    正常布局

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="horizontal"
    >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:text="Press Me!"
        android:textAllCaps="false"
        android:id="@+id/button"
        />
    <ProgressBar
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:id="@+id/progressbar"
        style="@style/Widget.AppCompat.ProgressBar.Horizontal"
        />
</LinearLayout>

主要活动课程 按下按钮

之后,将调用AsynTaskTest类
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {
    ProgressBar progressBar;
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button)findViewById(R.id.button);
        progressBar = (ProgressBar)findViewById(R.id.progressbar);
        progressBar.setMax(100);
        progressBar.setVisibility(View.VISIBLE);

       button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AsynTaskTest asynTaskTest = new AsynTaskTest(v.getContext());
                asynTaskTest.execute();
            }
        });
    }
}

AsynTask类 启动5秒后,应用程式当机。 我希望进度条每5秒增加一次,以在1分钟内达到最大值。

import android.content.Context;
import android.os.AsyncTask;
import android.view.View;
import android.widget.ProgressBar;

public class AsynTaskTest extends AsyncTask<Void, Integer, Void>{
    ProgressBar progressBar;
    Context context;
    int count = 1;
    int update = 5;

    public AsynTaskTest(Context context){
        this.context = context;
    }

    @Override
    protected Void doInBackground(Void... voids) {

        while(count <= 11){
            try {
                Thread.sleep(5000);
                int progress = (update*100)/60;
                publishProgress(progress);
                update += 5;
                count++;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }

        return null;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        progressBar.setVisibility(View.GONE);
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        progressBar.setProgress(values[0]);
    }
}

请提供帮助,并向我展示如何解决此崩溃

1 个答案:

答案 0 :(得分:0)

按如下所示更改代码

@Autowired
List<MyInterceptor> interceptors;

public class AsynTaskTest extends AsyncTask<Void, Integer, Void> {
ProgressBar progressBar;
Context context;
int count = 1;
int update = 5;

public AsynTaskTest(Context context ,  ProgressBar progressBar){
    this.context = context;
    this.progressBar = progressBar;
}

@Override
protected Void doInBackground(Void... voids) {

    while(count <= 11){
        try {
            Thread.sleep(5000);
            int progress = (update*100)/60;
            publishProgress(progress);
            update += 5;
            count++;
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    return null;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
}

@Override
protected void onPostExecute(Void aVoid) {
    progressBar.setVisibility(View.GONE);
}

@Override
protected void onProgressUpdate(Integer... values) {
    progressBar.setProgress(values[0]);
}
}