单击按钮时显示Textview然后显示地图

时间:2013-04-06 21:42:54

标签: android google-maps-api-3 textview android-button

我希望我的程序在创建时运行ClosestObject函数&当用户点击按钮时,它应该运行ShowMap函数并显示map..how当它结合时它会崩溃这是我的代码

    protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_main);
                    showButton=(Button) findViewById(R.id.button1);
                    result = (TextView) findViewById(R.id.text1);
                    try {
                **//here I run Closest Object so I can display the result on the text box**
                          String Add= ClosestObject();

            } catch (ClientProtocolException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
**// Here I set the test with closestObject result**
                    result.setText(buff.toString());
**//So when I click show the map**            
                    View.OnClickListener myhandler1 = new View.OnClickListener() {
                        public void onClick(View v) {
                        new TestGoogleMaps().execute("");
                        }
                      };
                    gps = new GPSTracker(MainActivity.this);

            // here it reads from gps

                    if(gps.canGetLocation()){

                 latitude = gps.getLatitude();
                 longitude = gps.getLongitude();

                    }

                    else
                    {
                        gps.showSettingsAlert();
                    }



            }


              class TestGoogleMaps extends AsyncTask<String, Void, String>{
           @Override
           protected String doInBackground(String... params) {
                   // TODO Auto-generated method stub
               try {

                   ShowMap();

               } catch (ClientProtocolException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
               } catch (JSONException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
               } catch (IOException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
               }

           finally {

           }
                   return null;
           }
       }


}

这里是logcat

04-06 21:40:31.774: W/dalvikvm(963): threadid=1: thread exiting with uncaught exception    (group=0x40a71930)
04-06 21:40:31.814: E/AndroidRuntime(963): FATAL EXCEPTION: main
04-06 21:40:31.814: E/AndroidRuntime(963): java.lang.RuntimeException: Unable to start   activity ComponentInfo{com.example.distancemap/com.example.distancemap.MainActivity}:   android.os.NetworkOnMainThreadException
04-06 21:40:31.814: E/AndroidRuntime(963):  at    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-06 21:40:31.814: E/AndroidRuntime(963):  at and    roid.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-06 21:40:31.814: E/AndroidRuntime(963):  at     android.app.ActivityThread.access$600(ActivityThread.java:141)
04-06 21:40:31.814: E/AndroidRuntime(963):  at    android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-06 21:40:31.814: E/AndroidRuntime(963):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-06 21:40:31.814: E/AndroidRuntime(963):  at android.os.Looper.loop(Looper.java:137)
04-06 21:40:31.814: E/AndroidRuntime(963):  at android.app.ActivityThread.main(ActivityThread.java:5041)
04-06 21:40:31.814: E/AndroidRuntime(963):  at java.lang.reflect.Method.invokeNative(Native Method)
04-06 21:40:31.814: E/AndroidRuntime(963):  at java.lang.reflect.Method.invoke(Method.java:511)
04-06 21:40:31.814: E/AndroidRuntime(963):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-06 21:40:31.814: E/AndroidRuntime(963):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-06 21:40:31.814: E/AndroidRuntime(963):  at dalvik.system.NativeStart.main(Native Method)
04-06 21:40:31.814: E/AndroidRuntime(963): Caused by: android.os.NetworkOnMainThreadException
04-06 21:40:31.814: E/AndroidRuntime(963):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
04-06 21:40:31.814: E/AndroidRuntime(963):  at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
04-06 21:40:31.814: E/AndroidRuntime(963):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
04-06 21:40:31.814: E/AndroidRuntime(963):  at java.net.InetAddress.getAllByName(InetAddress.java:214)
04-06 21:40:31.814: E/AndroidRuntime(963):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
04-06 21:40:31.814: E/AndroidRuntime(963):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
04-06 21:40:31.814: E/AndroidRuntime(963):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
04-06 21:40:31.814: E/AndroidRuntime(963):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
04-06 21:40:31.814: E/AndroidRuntime(963):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
04-06 21:40:31.814: E/AndroidRuntime(963):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
04-06 21:40:31.814: E/AndroidRuntime(963):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
04-06 21:40:31.814: E/AndroidRuntime(963):  at com.example.distancemap.MainActivity.ClosestObject(MainActivity.java:159)
04-06 21:40:31.814: E/AndroidRuntime(963):  at com.example.distancemap.MainActivity.onCreate(MainActivity.java:63)
04-06 21:40:31.814: E/AndroidRuntime(963):  at android.app.Activity.performCreate(Activity.java:5104)
04-06 21:40:31.814: E/AndroidRuntime(963):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-06 21:40:31.814: E/AndroidRuntime(963):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-06 21:40:31.814: E/AndroidRuntime(963):  ... 11 more

1 个答案:

答案 0 :(得分:0)

 04-06 21:40:31.814: E/AndroidRuntime(963): Caused by: android.os.NetworkOnMainThreadException

您错过了结束}

这有助于解决这个问题。

       class TestGoogleMaps extends AsyncTask<String, Void, String>{
           @Override
           protected String doInBackground(String... params) {
                   // TODO Auto-generated method stub
               try {

                   ShowMap();

               } catch (ClientProtocolException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
               } catch (JSONException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
               } catch (IOException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
               }

           finally {

           }
                   return null;
           }
       }


}