gpt4 book ai didi

java - 带进度条的 AsyncTask

转载 作者:行者123 更新时间:2023-12-01 10:56:47 25 4
gpt4 key购买 nike

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

我不知道可以把“new MormaKaugummi().execute();”放在哪里。应用程序在任何地方崩溃并给我错误“不幸的是,犯罪生活已经停止。”

“计数器 TextView ”位于同一应用程序的另一个 Activity 中。

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

我需要一个独特的解决方案,因为我是 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

最佳答案

您正在尝试执行 findviewbyId 并通过 update 更新 doInBackground 方法中的 TextView 功能,这是你做不到的。

至于崩溃是由于空指针异常而发生的,您可以在 logcat 的 Caused by 子句中自行阅读,它会告诉您抛出空指针异常的确切行号

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为 null,因为您尝试初始化变量以从非 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 之外使用该 View ,则需要初始化所有 View 元素,例如 onCreate 本身中的 textview,并将引用保留为类变量。

关于java - 带进度条的 AsyncTask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33586976/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com