0

現在地の緯度と経度をSMSで友達に送信したい。このアプリケーションはダイアログを起動し、2つのボタンがあります。1つはsendlocationで、もう1つはキャンセルボタンです。しかし、エミュレータでプログラムを起動すると、次のエラーが発生しました。

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();

            }
4

1 に答える 1

1

古いアプリケーションコンテキストの代わりに、getBaseContext()または次のように、現在のアクティビティコンテキストを使用getApplicationContext();します。

 AlertDialog.Builder dialog = new AlertDialog.Builder(Your_Current_Activity.this);
于 2012-09-08T05:44:24.103 に答える