2

AsyncTaskを使用する単純なアクティビティがあります

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)
4

2 に答える 2

1

NullPointerExceptionあなたがどこから来ているかを考えると( onPostExecute()),txtどうやら ですnull.

于 2012-05-08T00:24:22.383 に答える