为什么在Android中执行此HttpGet会引发NullPointerException?

时间:2012-07-12 03:00:44

标签: android apache http

我创建了一个新的Android项目,其中我添加的唯一代码是onCreate()方法中的此代码(我还为清单添加了INTERNET权限):

@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());
    }
}

在日志中,我看到try块打印的最后一件事是executing,之后抛出了NullPointerException。

我在this example的开头松散地构建了我的代码,但是我看不出会有什么不同会导致NullPointerException。

在调试器中运行时,我发现该异常实际上是一个NetworkOnMainThreadException,当我尝试e.getMessage()时,它显然会导致NullPointerException。

正在打印的错误消息是

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

1 个答案:

答案 0 :(得分:5)

问题不是NullPointerException,但是如果你调试你的应用程序,你会发现异常是:NetworkOnMainThreadException

很可能您是在较新的SDK(Honeycomb或更高版本)上运行您的应用程序,因此不鼓励这样做。

任何网络代码,你都应该放入一个帖子。理想情况下使用AsyncTask。请查看此链接:http://www.techblogistech.com/2011/11/how-to-fix-the-android-networkonmainthreadexception/

希望这有帮助。