2

I've created a new Android project in which the only code I've added is this code in the onCreate() method (I also added the INTERNET permission to the manifest):

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_http_get_test);
    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet();
    try
    {
        System.out.println("making uri");
        URI uri = new URI("http://w3mentor.com/");
        request.setURI(uri);
        System.out.println("executing");
        client.execute(request); // This line throws the exception
        System.out.println("Done!");
    }
    catch(Exception e)
    {
        System.out.println(e.getMessage());
    }
}

In the log, I see that the last thing printed by the try block was executing, after which a NullPointerException was thrown.

I loosely based my code on the beginning of this example, but I can't see what I'm doing differently that would cause a NullPointerException.

When run in the debugger I see that the exception is actually a NetworkOnMainThreadException, which apparently causes a NullPointerException when I try e.getMessage().

The error message being printed out is

07-12 02:58:33.997: E/AndroidRuntime(983): FATAL EXCEPTION: main
07-12 02:58:33.997: E/AndroidRuntime(983): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.whatever.http.test/com.whatever.http.test.HttpGetTest}: java.lang.NullPointerException
07-12 02:58:33.997: E/AndroidRuntime(983):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
07-12 02:58:33.997: E/AndroidRuntime(983):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-12 02:58:33.997: E/AndroidRuntime(983):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-12 02:58:33.997: E/AndroidRuntime(983):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-12 02:58:33.997: E/AndroidRuntime(983):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-12 02:58:33.997: E/AndroidRuntime(983):  at android.os.Looper.loop(Looper.java:137)
07-12 02:58:33.997: E/AndroidRuntime(983):  at android.app.ActivityThread.main(ActivityThread.java:4424)
07-12 02:58:33.997: E/AndroidRuntime(983):  at java.lang.reflect.Method.invokeNative(Native Method)
07-12 02:58:33.997: E/AndroidRuntime(983):  at java.lang.reflect.Method.invoke(Method.java:511)
07-12 02:58:33.997: E/AndroidRuntime(983):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-12 02:58:33.997: E/AndroidRuntime(983):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-12 02:58:33.997: E/AndroidRuntime(983):  at dalvik.system.NativeStart.main(Native Method)
07-12 02:58:33.997: E/AndroidRuntime(983): Caused by: java.lang.NullPointerException
07-12 02:58:33.997: E/AndroidRuntime(983):  at com.android.internal.os.LoggingPrintStream.println(LoggingPrintStream.java:298)
07-12 02:58:33.997: E/AndroidRuntime(983):  at com.whatever.http.test.HttpGetTest.onCreate(HttpGetTest.java:33)
07-12 02:58:33.997: E/AndroidRuntime(983):  at android.app.Activity.performCreate(Activity.java:4465)
07-12 02:58:33.997: E/AndroidRuntime(983):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-12 02:58:33.997: E/AndroidRuntime(983):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
07-12 02:58:33.997: E/AndroidRuntime(983):  ... 11 more
4

1 回答 1

5

The Issue is not a NullPointerException but if you debug your application, you will find that the exception is : NetworkOnMainThreadException.

Most likely you are running your application on the newer SDKs (Honeycomb or greater) as a result of which this is discouraged.

Any networking code, you should put in a thread. Ideally use the AsyncTask for that. Check this link out : http://www.techblogistech.com/2011/11/how-to-fix-the-android-networkonmainthreadexception/

Hope this helps.

于 2012-07-12T03:16:34.947 回答