gpt4 book ai didi

android - AsyncTasks 有问题吗?

转载 作者:行者123 更新时间:2023-11-30 01:30:21 26 4
gpt4 key购买 nike

我对 AsyncTasks 有疑问。我在 onCreate() 中有这个

new ProgressTask().execute();

这是我的任务:

private class ProgressTask extends AsyncTask<String, Integer, Boolean> {
private ProgressDialog dialog;
public ProgressTask() {
//dialog = new ProgressDialog(getBaseContext());
}



/** progress dialog to show user that the backup is processing. */

/** application context. */
private Context context;

protected void onPreExecute() {
dialog = new ProgressDialog(getBaseContext());
dialog.setTitle("Loading connection...");
dialog.setMessage("Loading...");
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMax(100);
dialog.setProgress(0);
dialog.show();
}

@Override
protected void onPostExecute(final Boolean success) {
if (dialog.isShowing()) {
dialog.dismiss();
}

if (success) {
Toast.makeText(context, "OK", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "Error", Toast.LENGTH_LONG).show();
}
}

protected Boolean doInBackground(final String... args) {
try{
//Get device ID
publishProgress(0);
loading.setMessage("Getting device ID...");
try {
getDeviceId();
} catch (Exception ex) {
ErrorMessage("Couldn't get device ID");
ErrorAlert += "Error 101: Couldn't get device ID\n";
}
Thread.sleep(2000);
publishProgress(20);

//Open connection
loading.setMessage("Opening connection...");
try {
openConnection();
} catch (IOException ex) {
ErrorMessage("Couldn't open connection");
ErrorAlert += "Error 102: Couldn't open connection\n";
}
Thread.sleep(2000);
publishProgress(40);

//Testing connection
loading.setMessage("Testing connection...");
if (!mmSocket.isConnected()) {
ErrorMessage("Test failed!");
ErrorAlert += "Error 103: Test failed!\n";
}
Thread.sleep(2000);
publishProgress(60);

//Calibrate sensors
loading.setMessage("Calibrating sensors...");
try {
sendCommand("c");
} catch (IOException ex) {
ErrorMessage("Couldn't calibrate sensors");
ErrorAlert += "Error 104: Couldn't calibrate sensors\n";
}
Thread.sleep(2000);
publishProgress(80);

//Finish
loading.setMessage("Finishing...");
Thread.sleep(1000);
publishProgress(90);

//Clear
loading.setMessage("Clearing some stuff...");
Thread.sleep(1000);
publishProgress(100);

return true;
} catch (Exception e){

return false;
}
}

protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values[0]);
dialog.setProgress(values[0]);
}


}

但是当我启动 Activity 时,应用程序停止了:(

这是输出:

03-06 12:32:35.902 10648-10648/com.jules_citronic.racecarcontrol E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.jules_citronic.racecarcontrol, PID: 10648
Theme: themes:{default=overlay:theme.lonecm12.kikkosart.com.lonecm12, iconPack:theme.lonecm12.kikkosart.com.lonecm12, fontPkg:theme.lonecm12.kikkosart.com.lonecm12, com.android.systemui=overlay:theme.lonecm12.kikkosart.com.lonecm12, com.android.systemui.navbar=overlay:theme.lonecm12.kikkosart.com.lonecm12}
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jules_citronic.racecarcontrol/com.jules_citronic.racecarcontrol.control}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5466)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.view.ViewRootImpl.setView(ViewRootImpl.java:571)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:310)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:86)
at android.app.Dialog.show(Dialog.java:319)
at com.jules_citronic.racecarcontrol.control$ProgressTask.onPreExecute(control.java:109)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:604)
at android.os.AsyncTask.execute(AsyncTask.java:551)
at com.jules_citronic.racecarcontrol.control.onCreate(control.java:84)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5466) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

我对 Threads 之类的东西不是很好,所以我希望有人能帮助我。

编辑:我将 getBaseContext 更改为上下文并放置了“context = control.this”。顺便说一句,控制是我的 Activity/类(class)的名称。现在它启动了 Activity ,但从不显示 ProgressDialog 并且不连接到蓝牙。它让我为“错误”干杯。

编辑:异常导出这个:

03-06 13:09:04.858 15370-15535/com.jules_citronic.racecarcontrol E/MYAPP: exception
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:200)
at android.os.Handler.<init>(Handler.java:114)
at android.widget.Toast$TN.<init>(Toast.java:347)
at android.widget.Toast.<init>(Toast.java:103)
at android.widget.Toast.makeText(Toast.java:261)
at com.jules_citronic.racecarcontrol.control.ErrorMessage(control.java:301)
at com.jules_citronic.racecarcontrol.control$ProgressTask.doInBackground(control.java:135)
at com.jules_citronic.racecarcontrol.control$ProgressTask.doInBackground(control.java:90)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)

编辑:有效 :)。这是最后一个类:

private class ProgressTask extends AsyncTask<String, Integer, Boolean> {
private ProgressDialog dialog;
private Context context;
public ProgressTask(Context context) {
this.context = context;
//dialog = new ProgressDialog(getBaseContext());
}



/** progress dialog to show user that the backup is processing. */

/** application context. */

protected void onPreExecute() {
dialog = new ProgressDialog(context);
dialog.setTitle("Loading connection...");
dialog.setMessage("Loading...");
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMax(100);
dialog.setProgress(0);
dialog.show();
}

@Override
protected void onPostExecute(final Boolean success) {
if (dialog.isShowing()) {
dialog.dismiss();
}

if (success) {
Toast.makeText(context, "OK", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "Error", Toast.LENGTH_LONG).show();
}
}

protected Boolean doInBackground(final String... args) {
try {
Looper.prepare();
} catch (Exception ex) {
Log.e("MYAPP", "Exception in looper " + ex.getMessage());
}
try{
//Get device ID
publishProgress(0);
try {
getDeviceId();
} catch (Exception ex) {
ErrorMessage("Couldn't get device ID");
ErrorAlert += "Error 101: Couldn't get device ID\n";
}
Thread.sleep(2000);
publishProgress(20);

//Open connection
try {
openConnection();
} catch (IOException ex) {
ErrorMessage("Couldn't open connection");
ErrorAlert += "Error 102: Couldn't open connection\n";
}
Thread.sleep(2000);
publishProgress(40);

//Testing connection
if (!mmSocket.isConnected()) {
ErrorMessage("Test failed!");
ErrorAlert += "Error 103: Test failed!\n";
}
Thread.sleep(2000);
publishProgress(60);

//Calibrate sensors
try {
sendCommand("c");
} catch (IOException ex) {
ErrorMessage("Couldn't calibrate sensors");
ErrorAlert += "Error 104: Couldn't calibrate sensors\n";
}
Thread.sleep(2000);
publishProgress(80);

//Finish
Thread.sleep(1000);
publishProgress(90);

//Clear
Thread.sleep(1000);
publishProgress(100);

return true;
} catch (Exception e){
Log.e("MYAPP", "exception", e);
return false;
}
}

protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values[0]);
dialog.setProgress(values[0]);

if (values[0] == 0){
dialog.setMessage("Getting device ID...");
}
if (values[0] == 20){
dialog.setMessage("Opening connection...");
}
if (values[0] == 40){
dialog.setMessage("Testing connection...");
}
if (values[0] == 60){
dialog.setMessage("Calibrating sensors...");
}
if (values[0] == 80){
dialog.setMessage("Finishing...");
}
if (values[0] == 90){
dialog.setMessage("Clearing some stuff...");
}
}


}

最佳答案

由于 AsyncTask 没有继承自 Context,因此它无法访问当前上下文。

您必须将当前上下文作为参数传递给构造函数并将其设置在那里。

private Context context;
public ProgressTask(Context context) {
this.context = context;
}

然后从 new ProgressTask(context).execute();

传递上下文

关于android - AsyncTasks 有问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35826371/

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