0

我正在尝试实现 Google Places API,但它不断发送 java.lang.nullpointerexception。我从这个网站上得到了代码:http ://www.androidhive.info/2012/08/android-working-with-google-places-and-maps-tutorial/

从我能收集到的信息中,我将 URL 发送到某个 Google 服务器并得到响应。该响应采用 JSON 格式,然后被解析为我的特定“位置”对象。我认为在这个解析阶段会发生一些事情。在站点中,他们使用了一些中间对象 PlacesList,我认为这就是问题所在,因为我不明白。

地点列表:

        public class PlacesList implements Serializable {

    @Key
    public String status;

    @Key
    public List<Locations> results;

}

谷歌搜索:

       public PlacesList search(double latitude, double longitude, double radius)
        throws Exception {

    this._latitude = latitude;
    this._longitude = longitude;
    this._radius = radius;

    try {

        HttpRequestFactory httpRequestFactory = createRequestFactory(HTTP_TRANSPORT);
        HttpRequest request = httpRequestFactory
                .buildGetRequest(new GenericUrl(PLACES_SEARCH_URL));
        request.getUrl().put("key", API_KEY);
        request.getUrl().put("location", _latitude + "," + _longitude);
        request.getUrl().put("radius", _radius); // in meters
        request.getUrl().put("sensor", "false");


       PlacesList list = request.execute().parseAs(PlacesList.class);
        Log.i("placelist.class", list.toString());
        // Check log cat for places response status
       // Log.i("Places Status", "" + list.status);

        return list;

    } catch (HttpResponseException e) {
        Log.e("Error:", e.getMessage());
        return null;
    }

}

在 MainActivity System.err(6447):atcom.example.mapmapagain.MainActivity.onCreate(MainActivity.java:86) ==

              try {
        PlacesList places = ps.search(curr_lat, curr_longitude, 100.0);
    } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();

这是Logcat:

              04-26 19:10:52.839: W/System.err(4156): android.os.NetworkOnMainThreadException
              04-26 19:10:52.899: W/System.err(4156):   at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
              04-26 19:10:52.899: W/System.err(4156):   at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
              04-26 19:10:52.899: W/System.err(4156):   at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
              04-26 19:10:52.899: W/System.err(4156):   at java.net.InetAddress.getAllByName(InetAddress.java:214)
              04-26 19:10:52.899: W/System.err(4156):   at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
              04-26 19:10:52.899: W/System.err(4156):   at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
              04-26 19:10:52.899: W/System.err(4156):   at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
              04-26 19:10:52.899: W/System.err(4156):   at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
              04-26 19:10:52.899: W/System.err(4156):   at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
              04-26 19:10:52.899: W/System.err(4156):   at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
              04-26 19:10:52.899: W/System.err(4156):   at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:461)
             04-26 19:10:52.899: W/System.err(4156):    at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433)
             04-26 19:10:52.899: W/System.err(4156):    at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
             04-26 19:10:52.899: W/System.err(4156):    at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
             04-26 19:10:52.899: W/System.err(4156):    at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
             04-26 19:10:52.899: W/System.err(4156):    at libcore.net.http.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:165)
             04-26 19:10:52.899: W/System.err(4156):    at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:88)
             04-26 19:10:52.899: W/System.err(4156):    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:814)
             04-26 19:10:52.909: W/System.err(4156):    at com.example.mapmapagain.PlaceSearch.search(PlaceSearch.java:64)
             04-26 19:10:52.909: W/System.err(4156):    at com.example.mapmapagain.MainActivity.onCreate(MainActivity.java:86)
             04-26 19:10:52.909: W/System.err(4156):    at android.app.Activity.performCreate(Activity.java:5104)
             04-26 19:10:52.909: W/System.err(4156):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
             04-26 19:10:52.909: W/System.err(4156):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
             04-26 19:10:52.909: W/System.err(4156):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
             04-26 19:10:52.909: W/System.err(4156):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
             04-26 19:10:52.909: W/System.err(4156):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
             04-26 19:10:52.909: W/System.err(4156):    at android.os.Handler.dispatchMessage(Handler.java:99)
             04-26 19:10:52.909: W/System.err(4156):    at android.os.Looper.loop(Looper.java:137)
             04-26 19:10:52.909: W/System.err(4156):    at android.app.ActivityThread.main(ActivityThread.java:5039)
             04-26 19:10:52.909: W/System.err(4156):    at java.lang.reflect.Method.invokeNative(Native Method)
             04-26 19:10:52.909: W/System.err(4156):    at java.lang.reflect.Method.invoke(Method.java:511)
             04-26 19:10:52.909: W/System.err(4156):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
             04-26 19:10:52.909: W/System.err(4156):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
             04-26 19:10:52.909: W/System.err(4156):    at dalvik.system.NativeStart.main(Native Method)

谢谢你的帮助。

4

1 回答 1

1

android.os.NetworkOnMainThreadException如您的异常所示,您正在主 UI 线程上进行网络连接,这现在被 Android 框架禁止。

这是禁止的,因为假设您的服务器无法访问,或者您的用户的互联网连接速度非常慢。当程序启动search您的该功能时,UI 将完全冻结,直到它返回,因为 UI 线程将忙于尝试联系该服务器。

将您的搜索功能放在 a 中AsyncTask,或将其包装在 a 中Runnable,以便它在自己的线程中执行。看看这个问题以获得一个很好的例子AsyncTask Android 例子

于 2013-04-26T23:34:26.703 回答