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 not found, line:- 2059

4

3 に答える 3

0

メイン UI スレッドでは、ネットワーク関連のアクティビティを実行しないでください。これを行うと、アプリがクラッシュします。

非同期タスクを使用するか、新しいスレッドを開始して JSON データを取得します

より具体的には、次のことを試してください。onCreate 関数では、DefaultHttpClient の部分から始めて、ここに含めます。

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

};

よろしくお願いします、ラジャマニケム。S

于 2012-10-22T09:42:07.440 に答える
0

ログに明確に記載されています原因: android.os.NetworkOnMainThreadException

于 2012-10-22T09:57:51.183 に答える
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 に答える