我是這個網站的新手。 我與我的AsyncTask
和我的android程序有一個錯誤...這是我寫的第一個異步任務。NullPointerException調用AsyncTask.onPreExecute()
主類稱爲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
得到的我都試過一切,我真的很困惑,什麼樣正在這裏...
感謝您的時間和精力。
,而不是`t.setText(職稱);`異步任務裏,嘗試在您的活動創建一個方法來做到這一點,和呼叫那你的異步任務 – SERPRO 2011-12-15 18:45:15
方法,所以我必須發送SocialApp的實例到AsyncTask? – bluejamesbond 2011-12-15 18:46:40
你正在得到一個NullPointerException,你確定TextView被正確初始化了嗎? – bvd 2011-12-15 18:48:51