我有一个使用 AsyncTask 的简单 Activity
EditText txt;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.detectNetwork().permitAll().build();
StrictMode.setThreadPolicy(policy);
txt = (EditText) findViewById(R.id.edit_message);
setContentView(R.layout.main);
}
public void connectToNetwork(View view)
{
String address = "https://www.google.com";
new DownloadWebpageText().execute(address);
}
private class DownloadWebpageText extends AsyncTask<String, Void, String>
{
@Override
protected String doInBackground(String... params)
{
return params[0] + ",Some thing";
}
@Override
protected void onPostExecute(String result)
{
txt.setText(result); //this is an EditText
}
}
为了简洁起见,我简化了代码,但是它抛出了 InvocationTargetException 并且没有执行 onPostExecute 方法。调试时我无法理解任何内容。有什么帮助吗?
Logcat 错误:
05-08 00:16:44.045: E/AndroidRuntime(537): FATAL EXCEPTION: main
05-08 00:16:44.045: E/AndroidRuntime(537): java.lang.NullPointerException
05-08 00:16:44.045: E/AndroidRuntime(537): at myApp.src.MyAppActivity$DownloadWebpageText.onPostExecute(MyAppActivity.java:162)
05-08 00:16:44.045: E/AndroidRuntime(537): at myApp.src.MyAppActivity$DownloadWebpageText.onPostExecute(MyAppActivity.java:1)
05-08 00:16:44.045: E/AndroidRuntime(537): at android.os.AsyncTask.finish(AsyncTask.java:602)
05-08 00:16:44.045: E/AndroidRuntime(537): at android.os.AsyncTask.access$600(AsyncTask.java:156)
05-08 00:16:44.045: E/AndroidRuntime(537): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
05-08 00:16:44.045: E/AndroidRuntime(537): at android.os.Handler.dispatchMessage(Handler.java:99)
05-08 00:16:44.045: E/AndroidRuntime(537): at android.os.Looper.loop(Looper.java:137)
05-08 00:16:44.045: E/AndroidRuntime(537): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-08 00:16:44.045: E/AndroidRuntime(537): at java.lang.reflect.Method.invokeNative(Native Method)
05-08 00:16:44.045: E/AndroidRuntime(537): at java.lang.reflect.Method.invoke(Method.java:511)
05-08 00:16:44.045: E/AndroidRuntime(537): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-08 00:16:44.045: E/AndroidRuntime(537): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-08 00:16:44.045: E/AndroidRuntime(537): at dalvik.system.NativeStart.main(Native Method)