asynctask不断使android emulator上运行的演示应用程序崩溃

d6kp6zgx  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(317)

我目前正在通过一本书学习android应用程序开发,我目前正在努力学习一些表面上看起来相当简单的东西。界面由一个按钮和一个文本视图组成。这个练习的目的是演示在执行耗时的任务时如何使用单独的线程。
我试过调试 values 的数组 onProgressUpdate 方法确实包含传递给它的值,并且在尝试为其设置文本时似乎总是发生崩溃 myTextView . 如果有任何帮助,我将不胜感激。
代码:

package com.ebookfrenzy.asyncdemo;

import androidx.appcompat.app.AppCompatActivity;
import android.os.AsyncTask;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private TextView myTextView;
    private String tag = "Test";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void buttonClick(View view) {
        AsyncTask task = new MyTask().execute();
    }

    private class MyTask extends AsyncTask<String,Integer,String> {
        @Override
        protected void onPreExecute() {

        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            Log.i(tag,"publish: " + Integer.toString(values[0]));
            myTextView.setText("Counter = " + values[0]);
        }

        @Override
        protected void onPostExecute(String result) {
            myTextView.setText(result);
        }

        @Override
        protected String doInBackground(String... strings) {
            int i = 0;
            while (i <= 20) {
                Log.i(tag,"before publish: " + Integer.toString(i));
                publishProgress(i);
                try {
                    Thread.sleep(1000);
                    i++;
                } catch(Exception e) {

                }
            }

            return "Button Pressed";
        }
    }
}

logcat输出(由我为故障排除添加的log.i项创建)通常如下所示,但如果警告消息提示应用程序不断崩溃,则“发布前”项会不断填充i,每次递增,直到我关闭警告消息:

2020-12-13 23:17:04.499 31729-31772/com.ebookfrenzy.asyncdemo I/Test: before publish: 0
2020-12-13 23:17:04.503 31729-31729/com.ebookfrenzy.asyncdemo I/Test: publish: 0

logcat的错误部分:

--------- beginning of crash
2020-12-13 23:19:47.635 353-353/com.ebookfrenzy.asyncdemo E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.ebookfrenzy.asyncdemo, PID: 353
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at com.ebookfrenzy.asyncdemo.MainActivity$MyTask.onProgressUpdate(MainActivity.java:34)
        at com.ebookfrenzy.asyncdemo.MainActivity$MyTask.onProgressUpdate(MainActivity.java:25)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:715)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2020-12-13 23:19:47.639 353-353/com.ebookfrenzy.asyncdemo I/Process: Sending signal. PID: 353 SIG: 9
gt0wga4j

gt0wga4j1#

我想 myTextView 必须为空。
试试这个。

if (myTextView != null){
  myTextView.setText("Counter = " + values[0]);
}

你应该初始化 myTextViewonCreate() 方法。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myTextView = (TextView) findViewById(R.id.mytextviewid); // put 
}

相关问题