0

我正在将 json 数据输入 mapview,但每当我运行我的应用程序时,不幸的是 appname 已停止,我在我的项目中使用 mapviewballoons 库,但我没有收到任何编程错误,代码工作正常,logcat 说:

    10-22 14:46:35.115: D/dalvikvm(718): GC_CONCURRENT freed 177K, 4% free 8260K/8519K, paused 32ms+5ms, total 87ms
10-22 14:46:35.115: D/dalvikvm(718): WAIT_FOR_CONCURRENT_GC blocked 44ms
10-22 14:46:35.124: W/CursorWrapperInner(718): Cursor finalized without prior close()
10-22 14:46:35.124: W/CursorWrapperInner(718): Cursor finalized without prior close()
10-22 14:46:35.274: D/AndroidRuntime(718): Shutting down VM
10-22 14:46:35.285: W/dalvikvm(718): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
10-22 14:46:35.314: E/AndroidRuntime(718): FATAL EXCEPTION: main
10-22 14:46:35.314: E/AndroidRuntime(718): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.json.mapview/com.example.json.mapview.Main}: android.os.NetworkOnMainThreadException
10-22 14:46:35.314: E/AndroidRuntime(718):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
10-22 14:46:35.314: E/AndroidRuntime(718):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-22 14:46:35.314: E/AndroidRuntime(718):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-22 14:46:35.314: E/AndroidRuntime(718):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-22 14:46:35.314: E/AndroidRuntime(718):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-22 14:46:35.314: E/AndroidRuntime(718):  at android.os.Looper.loop(Looper.java:137)
10-22 14:46:35.314: E/AndroidRuntime(718):  at android.app.ActivityThread.main(ActivityThread.java:4745)
10-22 14:46:35.314: E/AndroidRuntime(718):  at java.lang.reflect.Method.invokeNative(Native Method)
10-22 14:46:35.314: E/AndroidRuntime(718):  at java.lang.reflect.Method.invoke(Method.java:511)
10-22 14:46:35.314: E/AndroidRuntime(718):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-22 14:46:35.314: E/AndroidRuntime(718):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-22 14:46:35.314: E/AndroidRuntime(718):  at dalvik.system.NativeStart.main(Native Method)
10-22 14:46:35.314: E/AndroidRuntime(718): Caused by: android.os.NetworkOnMainThreadException
10-22 14:46:35.314: E/AndroidRuntime(718):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
10-22 14:46:35.314: E/AndroidRuntime(718):  at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
10-22 14:46:35.314: E/AndroidRuntime(718):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
10-22 14:46:35.314: E/AndroidRuntime(718):  at java.net.InetAddress.getAllByName(InetAddress.java:214)
10-22 14:46:35.314: E/AndroidRuntime(718):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
10-22 14:46:35.314: E/AndroidRuntime(718):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
10-22 14:46:35.314: E/AndroidRuntime(718):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
10-22 14:46:35.314: E/AndroidRuntime(718):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
10-22 14:46:35.314: E/AndroidRuntime(718):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
10-22 14:46:35.314: E/AndroidRuntime(718):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-22 14:46:35.314: E/AndroidRuntime(718):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-22 14:46:35.314: E/AndroidRuntime(718):  at com.example.json.mapview.Main.onCreate(Main.java:64)
10-22 14:46:35.314: E/AndroidRuntime(718):  at android.app.Activity.performCreate(Activity.java:5008)
10-22 14:46:35.314: E/AndroidRuntime(718):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-22 14:46:35.314: E/AndroidRuntime(718):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
10-22 14:46:35.314: E/AndroidRuntime(718):  ... 11 more
10-22 14:46:35.433: D/dalvikvm(718): GC_CONCURRENT freed 130K, 3% free 8629K/8839K, paused 23ms+36ms, total 177ms
10-22 14:51:35.465: I/Process(718): Sending signal. PID: 718 SIG: 9
10-22 14:52:57.604: E/Trace(743): error opening trace file: No such file or directory (2)
10-22 14:52:58.524: D/dalvikvm(743): GC_CONCURRENT freed 181K, 4% free 8260K/8519K, paused 25ms+4ms, total 118ms
10-22 14:52:58.524: D/dalvikvm(743): WAIT_FOR_CONCURRENT_GC blocked 73ms
10-22 14:52:58.534: W/CursorWrapperInner(743): Cursor finalized without prior close()
10-22 14:52:58.534: W/CursorWrapperInner(743): Cursor finalized without prior close()
10-22 14:52:58.724: D/AndroidRuntime(743): Shutting down VM
10-22 14:52:58.724: W/dalvikvm(743): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
10-22 14:52:58.764: E/AndroidRuntime(743): FATAL EXCEPTION: main
10-22 14:52:58.764: E/AndroidRuntime(743): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.json.mapview/com.example.json.mapview.Main}: android.os.NetworkOnMainThreadException
10-22 14:52:58.764: E/AndroidRuntime(743):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
10-22 14:52:58.764: E/AndroidRuntime(743):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-22 14:52:58.764: E/AndroidRuntime(743):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-22 14:52:58.764: E/AndroidRuntime(743):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-22 14:52:58.764: E/AndroidRuntime(743):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-22 14:52:58.764: E/AndroidRuntime(743):  at android.os.Looper.loop(Looper.java:137)
10-22 14:52:58.764: E/AndroidRuntime(743):  at android.app.ActivityThread.main(ActivityThread.java:4745)
10-22 14:52:58.764: E/AndroidRuntime(743):  at java.lang.reflect.Method.invokeNative(Native Method)
10-22 14:52:58.764: E/AndroidRuntime(743):  at java.lang.reflect.Method.invoke(Method.java:511)
10-22 14:52:58.764: E/AndroidRuntime(743):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-22 14:52:58.764: E/AndroidRuntime(743):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-22 14:52:58.764: E/AndroidRuntime(743):  at dalvik.system.NativeStart.main(Native Method)
10-22 14:52:58.764: E/AndroidRuntime(743): Caused by: android.os.NetworkOnMainThreadException
10-22 14:52:58.764: E/AndroidRuntime(743):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
10-22 14:52:58.764: E/AndroidRuntime(743):  at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
10-22 14:52:58.764: E/AndroidRuntime(743):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
10-22 14:52:58.764: E/AndroidRuntime(743):  at java.net.InetAddress.getAllByName(InetAddress.java:214)
10-22 14:52:58.764: E/AndroidRuntime(743):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
10-22 14:52:58.764: E/AndroidRuntime(743):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
10-22 14:52:58.764: E/AndroidRuntime(743):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
10-22 14:52:58.764: E/AndroidRuntime(743):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
10-22 14:52:58.764: E/AndroidRuntime(743):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
10-22 14:52:58.764: E/AndroidRuntime(743):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-22 14:52:58.764: E/AndroidRuntime(743):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-22 14:52:58.764: E/AndroidRuntime(743):  at com.example.json.mapview.Main.onCreate(Main.java:64)
10-22 14:52:58.764: E/AndroidRuntime(743):  at android.app.Activity.performCreate(Activity.java:5008)
10-22 14:52:58.764: E/AndroidRuntime(743):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-22 14:52:58.764: E/AndroidRuntime(743):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
10-22 14:52:58.764: E/AndroidRuntime(743):  ... 11 more
10-22 14:52:58.866: D/dalvikvm(743): GC_CONCURRENT freed 130K, 3% free 8629K/8839K, paused 30ms+5ms, total 162ms

显现:-

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.json.mapview"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".Main"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
         <uses-library android:name="com.google.android.maps" />
    </application>

</manifest>

Java代码:-

 public class Main extends MapActivity {
    public GeoPoint point;
    TapControlledMapView mapView; // use the custom TapControlledMapView
    List<Overlay> mapOverlays;
    Drawable drawable;
    SimpleItemizedOverlay itemizedOverlay;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mapView = (TapControlledMapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        mapView.setSatellite(false);
        // dismiss balloon upon single tap of MapView (iOS behavior) 
        mapView.setOnSingleTapListener(new OnSingleTapListener() {      
            public boolean onSingleTap(MotionEvent e) {
                itemizedOverlay.hideAllBalloons();
                return true;
            }
        });



        mapOverlays = mapView.getOverlays();        
        drawable = getResources().getDrawable(R.drawable.ic_launcher);
        itemizedOverlay = new SimpleItemizedOverlay(drawable, mapView);         
        itemizedOverlay.setShowClose(false);
        itemizedOverlay.setShowDisclosure(true);
        itemizedOverlay.setSnapToCenter(false);

        HttpClient client = new DefaultHttpClient();
        // Perform a GET request for a JSON list
        HttpUriRequest request = new HttpGet("https://dl.xxx.com/maps.json");
        // Get the response that sends back
        HttpResponse response = null;
        try {
            response = client.execute(request);
        } catch (ClientProtocolException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        // Convert this response into a readable string
        String jsonString = null;
        try {
            jsonString = StreamUtils.convertToString(response.getEntity().getContent());
        } catch (IllegalStateException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        // Create a JSON object that we can use from the String
        JSONObject json = null;
        try {
            json = new JSONObject(jsonString);
        } catch (JSONException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

                   try{

         JSONArray jsonArray = json.getJSONArray("maps");
         Log.e("log_tag", "Opening JSON Array ");
            for(int i=0;i < jsonArray.length();i++){                        

                JSONObject jsonObject = jsonArray.getJSONObject(i);

                    String latitude =  jsonObject.getString("latitude");
                    String longitude =  jsonObject.getString("longitude");
                    String title =  jsonObject.getString("title");
                    String country = jsonObject.getString("country");


                    double lat = Double.parseDouble(latitude);
                    double lng = Double.parseDouble(longitude);


                         Log.e("log_tag", "ADDING GEOPOINT"+title); 

                          point = new GeoPoint(
                                 (int) (lat * 1E6), 
                                 (int) (lng * 1E6));
                        OverlayItem overlayItem = new OverlayItem(point, title, 
                                country);
                        itemizedOverlay.addOverlay(overlayItem);

                        }
                   }catch(JSONException e)        {
                     Log.e("log_tag", "Error parsing data "+e.toString());
                } 

                   itemizedOverlay.populateNow(); 

                   mapOverlays.add(itemizedOverlay);
                        if (savedInstanceState == null) {



                            MapController controller = mapView.getController();
                            controller.setCenter(point);
                            controller.setZoom(7);

                        } else {

                            // example restoring focused state of overlays
                            int focused;
                            focused = savedInstanceState.getInt("focused_1", -1);
                            if (focused >= 0) {
                                itemizedOverlay.setFocus(itemizedOverlay.getItem(focused));
                            }
                        }    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }







}

注意:- 每当我调试我的应用程序获取 ActivityThread....soure 未找到,行:- 2059

4

3 回答 3

0

日志中明确提到了 Caused by: android.os.NetworkOnMainThreadException

于 2012-10-22T09:57:51.183 回答
0

网络相关活动不应在主 UI 线程中执行。如果这样做,应用程序将崩溃。

使用异步任务或启动新线程来获取 JSON 数据

要更具体,请尝试以下操作。在 onCreate 函数中,从 DefaultHttpClient 部分开始将它们包含在此处。

new Runnable() {
@Override
 public void run() {
   //Include the code here.
 }

};

谢谢和问候, Rajamanickem。小号

于 2012-10-22T09:42:07.440 回答
0

这个:

HttpClient client = new DefaultHttpClient();
        // Perform a GET request for a JSON list
        HttpUriRequest request = new HttpGet("https://dl.xxx.com/maps.json");
        // Get the response that sends back
        HttpResponse response = null;
        try {
            response = client.execute(request);
        } catch (ClientProtocolException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

摆脱它。

读取进程和线程 这将描述如何处理诸如与网络通信或从流中写入/读取之类的事情。不要在 UI 线程上放置此类代码或期望休眠、超时或锁定获取(这是任何生命周期方法,onCreate()、onResume()、onPause() ...)。

于 2012-10-22T10:15:09.333 回答