带进度条的AsyncTask

时间:2015-11-07 19:41:43

标签: java android android-asynctask

以下代码是我的应用程序的一部分,它不起作用,我不知道为什么。

我不知道在哪个地方可以放置"新的MormaKaugummi()。execute();"。在任何地方,应用程序崩溃并给了我错误"不幸的是,犯罪生活已经停止。"

"反文本视图"是在同一个应用程序中的另一个活动。

(应用程序的名称是犯罪生活)。

我需要一个独特的解决方案,因为我是Android编程的新手。

public class Morma extends AppCompatActivity {

class MormaKaugummi extends AsyncTask<String, String, String> {

    private int count = 0;
    private void count() {
        count++;
    }

    private void update() {
        TextView counter = (TextView) findViewById(R.id.counter);
        String Kcounter = Integer.toString(count);
        counter.setText(Kcounter);
    }

    @Override
    protected String doInBackground(String... params) {
        count();
        update();
        return null;
    }
}

private ProgressBar progressBarKaugummi;
private int progressStatusKaugummi = 0;
private Handler handlerKaugummi = new Handler();

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_morma);
    Button buttonMormaKaugummiKlauen = (Button) findViewById(R.id.buttonMormaKaugummiKlauen);

    buttonMormaKaugummiKlauen.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            progressBarKaugummi = (ProgressBar) findViewById(R.id.progressBarMormaKaugummi);
            new Thread(new Runnable() {
                public void run() {
                    while (progressStatusKaugummi < 100) {
                        progressStatusKaugummi += 1;
                        handlerKaugummi.post(new Runnable() {
                            public void run() {
                                progressBarKaugummi.setProgress(progressStatusKaugummi);
                            }
                        });
                        try {
                            Thread.sleep(4000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }).start();
            if (progressStatusKaugummi == 100) {
                progressBarKaugummi.setProgress(0);
                recreate();
                new MormaKaugummi().execute();
            }
        }
    });

这是logcat

11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime: java.lang.RuntimeException: An error occured while executing doInBackground()
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:     at android.os.AsyncTask$3.done(AsyncTask.java:299)
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:856)
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:  Caused by: java.lang.NullPointerException
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:     at peppermine_studios.criminallife.Morma$MormaKaugummi.update(Morma.java:26)
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:     at peppermine_studios.criminallife.Morma$MormaKaugummi.doInBackground(Morma.java:32)
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:     at peppermine_studios.criminallife.Morma$MormaKaugummi.doInBackground(Morma.java:15)
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:     at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:856) 
11-07 13:48:35.514 622-622/peppermine_studios.criminallife W/EGL_emulation: eglSurfaceAttrib not implemented
11-07 13:48:35.674 622-622/peppermine_studios.criminallife W/EGL_emulation: eglSurfaceAttrib not implemented
11-07 13:48:37.364 622-1376/? I/Process: Sending signal. PID: 622 SIG: 9

1 个答案:

答案 0 :(得分:1)

您正尝试通过update函数执行findviewbyId并更新doInBackground方法中的文本视图,这是您无法做到的。

至于由于空指针异常而发生的崩溃,因为你可以在logcat引起的子句中自己读取它告诉你抛出空指针异常的确切行号

Caused by: java.lang.NullPointerException
11-07 13:48:34.964 622-1376/peppermine_studios.criminallife E/AndroidRuntime:     at peppermine_studios.criminallife.Morma$MormaKaugummi.update(Morma.java:26)

正如你在这里看到的,它是由Morma.java中第26行的空指针引起的,我猜你的TextView counter为空,因为你试图初始化一个变量来引用一个UI元素一个非UI线程,计数器变量显然会初始化为null。

现在解决方案:

您需要做的是将更新函数放在异步任务类的onPostExecute方法中。原因:onPostExceute在调用asyntcTask.execute方法的任何线程上运行,因此您应该在onPostExecute方法中进行UI更新

@Override
protected String doInBackground(String... params) {
    count();
    return null;
}

@Override
protect void onPostExecute(String result) {
    update();
}

此外,由于您要从doInBackground返回null,所以您应该理想地扩展AsyncTask<String,String,Void>

编辑1:

添加了代码

public class Morma extends AppCompatActivity {

// make the text view class variable if you want to update it outside of onCreate.
private TextView mCounterTextView;

class MormaKaugummi extends AsyncTask<Void, Void, Void> {

    private int count = 0;
    private void count() {
        count++;
    }

    private void update() {
        String Kcounter = Integer.toString(count);
        mCounterTextView.setText(Kcounter);
    }

    @Override
    protected void doInBackground(Void... aVoid) {
        count();
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        update();
    }
}

private ProgressBar progressBarKaugummi;
private int progressStatusKaugummi = 0;
private Handler handlerKaugummi = new Handler();

@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_morma);
Button buttonMormaKaugummiKlauen = (Button) findViewById(R.id.buttonMormaKaugummiKlauen);

// initiate all your view items in onCreate.
mCounterTextView = (TextView) findViewById(R.id.counter);

buttonMormaKaugummiKlauen.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        progressBarKaugummi = (ProgressBar) findViewById(R.id.progressBarMormaKaugummi);
        new Thread(new Runnable() {
            public void run() {
                while (progressStatusKaugummi < 100) {
                    progressStatusKaugummi += 1;
                    handlerKaugummi.post(new Runnable() {
                        public void run() {
                            progressBarKaugummi.setProgress(progressStatusKaugummi);
                        }
                    });
                    try {
                        Thread.sleep(4000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        if (progressStatusKaugummi == 100) {
            progressBarKaugummi.setProgress(0);
            recreate();
            new MormaKaugummi().execute();
        }
    }
});

如果要在onCreate之外使用该视图,则需要初始化所有视图元素,如onCreate本身的textview,并将引用保留为类变量。