gpt4 book ai didi

android - 调用 AsyncTask.onPreExecute() 时出现 NullPointerException

转载 作者:行者123 更新时间:2023-11-29 14:03:07 26 4
gpt4 key购买 nike

我是这个网站的新手。我的 AsyncTask 和我的 android 程序有一个错误...这是我编写的第一个异步任务。

名为 SocialApp.java 的主类

@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView t = (TextView) this.findViewById(R.id.textView1);

HTMLParser task = new HTMLParser();
task.setTextView(t);
task.execute();
}

一个基本的AsyncTask

public class HTMLParser extends AsyncTask<Void, Void, Boolean> {

TextView t;
String title = "error";

public void setTextView(TextView t){
this.t = t;
}

@Override
protected void onPreExecute() {
t.setText(title);
}

@Override
protected Boolean doInBackground(Void ... arg0) {
t.setText(title);
}

@Override
protected void onPostExecute(Boolean result) {
t.setText(title);
}
}

结果

12-15 12:34:11.808: E/AndroidRuntime(590):  at cv.mk.android.utapp.HTMLParser.onPreExecute(HTMLParser.java:24)
12-15 12:34:11.808: E/AndroidRuntime(590): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
12-15 12:34:11.808: E/AndroidRuntime(590): at android.os.AsyncTask.execute(AsyncTask.java:511)
12-15 12:34:11.808: E/AndroidRuntime(590): at cv.mk.android.utapp.SocialApp.onCreate(SocialApp.java:26)
12-15 12:34:11.808: E/AndroidRuntime(590): at android.app.Activity.performCreate(Activity.java:4465)
12-15 12:34:11.808: E/AndroidRuntime(590): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-15 12:34:11.808: E/AndroidRuntime(590): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
12-15 12:34:11.808: E/AndroidRuntime(590): ... 11 more
12-15 12:34:19.666: D/dalvikvm(609): GC_FOR_ALLOC freed 33K, 3% free 9890K/10179K, paused 215ms
12-15 12:34:19.677: I/dalvikvm-heap(609): Grow heap (frag case) to 11.363MB for 1700016-byte allocation
12-15 12:34:19.747: D/dalvikvm(609): GC_CONCURRENT freed <1K, 3% free 11549K/11847K, paused 3ms+12ms
12-15 12:34:19.967: D/dalvikvm(609): GC_CONCURRENT freed 1K, 2% free 12079K/12231K, paused 2ms+3ms
12-15 12:34:20.037: D/dalvikvm(609): GC_FOR_ALLOC freed 1K, 2% free 12298K/12487K, paused 24ms
12-15 12:34:20.277: D/AndroidRuntime(609): Shutting down VM
12-15 12:34:20.277: W/dalvikvm(609): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
12-15 12:34:20.277: D/dalvikvm(609): GC_CONCURRENT freed 1K, 1% free 13063K/13191K, paused 3ms+3ms
12-15 12:34:20.287: E/AndroidRuntime(609): FATAL EXCEPTION: main
12-15 12:34:20.287: E/AndroidRuntime(609): java.lang.RuntimeException: Unable to start activity ComponentInfo{cv.mk.android.utapp/cv.mk.android.utapp.SocialApp}: java.lang.NullPointerException
12-15 12:34:20.287: E/AndroidRuntime(609): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
12-15 12:34:20.287: E/AndroidRuntime(609): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
12-15 12:34:20.287: E/AndroidRuntime(609): at android.app.ActivityThread.access$600(ActivityThread.java:122)
12-15 12:34:20.287: E/AndroidRuntime(609): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
12-15 12:34:20.287: E/AndroidRuntime(609): at android.os.Handler.dispatchMessage(Handler.java:99)
12-15 12:34:20.287: E/AndroidRuntime(609): at android.os.Looper.loop(Looper.java:137)
12-15 12:34:20.287: E/AndroidRuntime(609): at android.app.ActivityThread.main(ActivityThread.java:4340)
12-15 12:34:20.287: E/AndroidRuntime(609): at java.lang.reflect.Method.invokeNative(Native Method)
12-15 12:34:20.287: E/AndroidRuntime(609): at java.lang.reflect.Method.invoke(Method.java:511)
12-15 12:34:20.287: E/AndroidRuntime(609): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-15 12:34:20.287: E/AndroidRuntime(609): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-15 12:34:20.287: E/AndroidRuntime(609): at dalvik.system.NativeStart.main(Native Method)
12-15 12:34:20.287: E/AndroidRuntime(609): Caused by: java.lang.NullPointerException
12-15 12:34:20.287: E/AndroidRuntime(609): at cv.mk.android.utapp.HTMLParser.onPreExecute(HTMLParser.java:24)
12-15 12:34:20.287: E/AndroidRuntime(609): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
12-15 12:34:20.287: E/AndroidRuntime(609): at android.os.AsyncTask.execute(AsyncTask.java:511)
12-15 12:34:20.287: E/AndroidRuntime(609): at cv.mk.android.utapp.SocialApp.onCreate(SocialApp.java:26)
12-15 12:34:20.287: E/AndroidRuntime(609): at android.app.Activity.performCreate(Activity.java:4465)
12-15 12:34:20.287: E/AndroidRuntime(609): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-15 12:34:20.287: E/AndroidRuntime(609): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
12-15 12:34:20.287: E/AndroidRuntime(609): ... 11 more

我已经尝试了一切,但我真的很困惑这里发生了什么......

感谢您为我们付出的时间和精力。

最佳答案

您不能在 doInBackground 中调用 t.setText。该方法在不同的线程中运行,因此它无法更新 UI。只有 UI 线程中的东西可以做到这一点。所以,在处理过程中如果你想更新UI,你必须调用publishProgress,然后UI线程会调用onProgressUpdate,你可以在那里更新UI。

请参阅此线程以获得很好的解释:

UpdateUI from AsyncTask

虽然这仍然有效,但我错过了 NullPointer 异常。 t 可能没有像上面所说的那样被初始化。

关于android - 调用 AsyncTask.onPreExecute() 时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8525013/

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