2012-09-08 26 views
0

我想通过短信将我的当前位置经纬度发送给我的朋友。这个应用程序我启动对话框,并有两个按钮之一是sendlocation另一个是取消按钮。但我在我的模拟器中启动我的程序,我得到以下错误。将GPS位置发送给朋友时出错

的logcat:

09-08 10:58:04.140: E/AndroidRuntime(212): Uncaught handler: thread main exiting due to uncaught exception 
09-08 10:58:04.180: E/AndroidRuntime(212): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sendlocation/com.example.sendlocation.MainActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 
09-08 10:58:04.180: E/AndroidRuntime(212): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401) 
09-08 10:58:04.180: E/AndroidRuntime(212): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417) 
09-08 10:58:04.180: E/AndroidRuntime(212): at android.app.ActivityThread.access$2100(ActivityThread.java:116) 
09-08 10:58:04.180: E/AndroidRuntime(212): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
09-08 10:58:04.180: E/AndroidRuntime(212): at android.os.Handler.dispatchMessage(Handler.java:99) 
09-08 10:58:04.180: E/AndroidRuntime(212): at android.os.Looper.loop(Looper.java:123) 
09-08 10:58:04.180: E/AndroidRuntime(212): at android.app.ActivityThread.main(ActivityThread.java:4203) 
09-08 10:58:04.180: E/AndroidRuntime(212): at java.lang.reflect.Method.invokeNative(Native Method) 
09-08 10:58:04.180: E/AndroidRuntime(212): at java.lang.reflect.Method.invoke(Method.java:521) 
09-08 10:58:04.180: E/AndroidRuntime(212): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) 
09-08 10:58:04.180: E/AndroidRuntime(212): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549) 
09-08 10:58:04.180: E/AndroidRuntime(212): at dalvik.system.NativeStart.main(Native Method) 
09-08 10:58:04.180: E/AndroidRuntime(212): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 
09-08 10:58:04.180: E/AndroidRuntime(212): at android.view.ViewRoot.setView(ViewRoot.java:460) 
09-08 10:58:04.180: E/AndroidRuntime(212): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177) 
09-08 10:58:04.180: E/AndroidRuntime(212): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) 
09-08 10:58:04.180: E/AndroidRuntime(212): at android.app.Dialog.show(Dialog.java:238) 
09-08 10:58:04.180: E/AndroidRuntime(212): at android.app.AlertDialog$Builder.show(AlertDialog.java:802) 
09-08 10:58:04.180: E/AndroidRuntime(212): at com.example.sendlocation.MainActivity.updateWithNewLocation(MainActivity.java:177) 
09-08 10:58:04.180: E/AndroidRuntime(212): at com.example.sendlocation.MainActivity.onCreate(MainActivity.java:74) 
09-08 10:58:04.180: E/AndroidRuntime(212): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123) 
09-08 10:58:04.180: E/AndroidRuntime(212): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364) 
09-08 10:58:04.180: E/AndroidRuntime(212): ... 11 more 

mycode的:

private void updateWithNewLocation(Location location) { 
      String latLong; 
      TextView myLocation; 
      myLocation = (TextView) findViewById(R.id.myLocation); 

      String addressString = "no address found"; 
      if(location!=null) { 

       positionOverlay.setLocation(location); 


       Double geoLat=location.getLatitude()*1E6; 
       Double geoLng=location.getLongitude()*1E6; 
       GeoPoint point=new GeoPoint(geoLat.intValue(),geoLng.intValue()); 

       mapController.animateTo(point); 

      double lat = location.getLatitude(); 
      double lon = location.getLongitude(); 
      latLong = "Lat:" + lat + "\nLong:" + lon; 



      double lattitude = location.getLatitude(); 
      double longitude = location.getLongitude(); 

      Geocoder gc = new Geocoder(this,Locale.getDefault()); 
      try { 
      List<Address> addresses= gc.getFromLocation(lattitude, longitude, 1); 
      StringBuilder sb = new StringBuilder(); 
      if(addresses.size()>0) { 
      Address address=addresses.get(0); 
      for(int i=0;i<address.getMaxAddressLineIndex();i++) 
       sb.append(address.getAddressLine(i)).append("\n"); 
      sb.append(address.getLocality()).append("\n"); 
      sb.append(address.getPostalCode()).append("\n"); 
     sb.append(address.getCountryName()); 
     } 
      addressString = sb.toString(); 
      } 
      catch (Exception e) { 
      } 
      } else { 
      latLong = " NO Location Found "; 
      } 

      myLocation.setText("Your Current Position is :\n"+ latLong + "\n"+ addressString); 


       final String Touch=myLocation.getText().toString(); 

       String title="friendloc"; 
       String message="frndlatlong"; 
       AlertDialog.Builder dialog = new AlertDialog.Builder(getBaseContext()); 
        dialog.setTitle(title); 
        dialog.setMessage(message);    

        // dialog to send location via SMS 
        dialog.setCancelable(false); 
        dialog.setPositiveButton("Send Location", 
         new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          // call SMS program 
          Intent sendIntent = new Intent(Intent.ACTION_VIEW); 
          sendIntent.putExtra("sms_body", "My location: " + Touch); 
          sendIntent.setType("vnd.android-dir/mms-sms"); 
          startActivity(sendIntent); 
         } 
        }) ; 
        dialog.setNegativeButton("Cancel", 
         new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          dialog.dismiss(); 
         } 
        }); 
        dialog.show(); 

      } 

回答

1

使用当前Activiity语境,而不是旧的应用程序上下文getBaseContext()getApplicationContext();为:

AlertDialog.Builder dialog = new AlertDialog.Builder(Your_Current_Activity.this); 
+0

完全正确的。得到的答案非常感谢......... – Ram

+0

如何保存这个纬度,经度在我的目录? – Ram

+0

你想把这些坐标保存在SD-CARD上? –