0

ダイアログアラートと位置情報サービスのインテントを使用して、ユーザーがgpsシステムを有効にしているかどうかを確認するための簡単なmapActivityを開発します。また、gpsが有効になっている場合は、mapViewに直接移動しますが、これは私には起こりません。を押してmapViewに移動すると、強制的に閉じます。

これはダイアログコードです:

  public void onClick(View arg0) {

                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        context);

                // set title
                alertDialogBuilder.setTitle("Your Title");

                // set dialog message
                alertDialogBuilder
                        .setMessage("Click yes TO ENABLE GPS")
                        .setCancelable(false)
                        .setPositiveButton("Yes",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                            int id) {
                                        // if this button is clicked, close
                                        // current activity
                                        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);  
                                          startActivityForResult(intent, 1);  
                                          } 

                                })
                        .setNegativeButton("No",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                            int id) {
                                        // if this button is clicked, just close
                                        // the dialog box and do nothing
                                        //dialog.cancel();

                                        Intent intent = new Intent(MainActivity.this, AndroidGoogleMapsActivity.class);
                                        context.startActivity(intent);
                                    }
                                });



                // create alert dialog
                AlertDialog alertDialog = alertDialogBuilder.create();

                // show it
                alertDialog.show();

            }

        });



    }

そしてこれはマニフェストファイルです:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"    

package="com.mkyong.android"

 android:versionCode="1"


    android:versionName="1.0" ><uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <uses-library
            android:name="com.google.android.maps"
            android:required="true" />

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".AndroidGoogleMapsActivity" >
        </activity>
    </application>

</manifest>

これはAndroidGoogleMapsActivityクラスのコードです

パブリッククラスAndroidGoogleMapsActivityはMapActivityを拡張します{

@Override


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

    // Displaying Zooming controls
    MapView mapView = (MapView) findViewById(R.id.mapView);
    Log.d("error", "the negative button befor the line 27 ");
    mapView.setBuiltInZoomControls(true);

    Log.d("error", "the negative button after the line 27 befor the map view settings ");     

     mapView.setSatellite(true); // Satellite View
     Log.d("error", "the negative button after the line 27 after satellite view  "); 
     mapView.setStreetView(false); // Street View
     Log.d("error", "the negative button after the line 27 after street view  "); 
     mapView.setTraffic(true); // Traffic view

     Log.d("error", "the negative button after the line 27 after  the map view settings ");  

    MapController mc = mapView.getController();
    Log.d("error", "the negative button after the line 27 after  the mapController ");  
    double lat = Double.parseDouble("33.823862");// kehale
    double lon = Double.parseDouble("35.590248");// kehale
    GeoPoint geoPoint = new GeoPoint((int)(lat * 1E6), (int)(lon * 1E6));
    mc.animateTo(geoPoint);
    mc.setZoom(15);
    mapView.invalidate(); 

    Log.d("error", "the negative button befor the GeoPoint");


@Override
protected boolean isRouteDisplayed() {
    return false;
}

}

そして、これは私が得るエラーです:

09-20 05:16:22.512: D/AndroidRuntime(309): Shutting down VM
09-20 05:16:22.512: W/dalvikvm(309): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
09-20 05:16:22.543: E/AndroidRuntime(309): FATAL EXCEPTION: main
09-20 05:16:22.543: E/AndroidRuntime(309): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mkyong.android/com.mkyong.android.AndroidGoogleMapsActivity}: java.lang.NullPointerException
09-20 05:16:22.543: E/AndroidRuntime(309):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-20 05:16:22.543: E/AndroidRuntime(309):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-20 05:16:22.543: E/AndroidRuntime(309):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-20 05:16:22.543: E/AndroidRuntime(309):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-20 05:16:22.543: E/AndroidRuntime(309):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-20 05:16:22.543: E/AndroidRuntime(309):  at android.os.Looper.loop(Looper.java:123)
09-20 05:16:22.543: E/AndroidRuntime(309):  at android.app.ActivityThread.main(ActivityThread.java:4627)
09-20 05:16:22.543: E/AndroidRuntime(309):  at java.lang.reflect.Method.invokeNative(Native Method)
09-20 05:16:22.543: E/AndroidRuntime(309):  at java.lang.reflect.Method.invoke(Method.java:521)
09-20 05:16:22.543: E/AndroidRuntime(309):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-20 05:16:22.543: E/AndroidRuntime(309):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-20 05:16:22.543: E/AndroidRuntime(309):  at dalvik.system.NativeStart.main(Native Method)
09-20 05:16:22.543: E/AndroidRuntime(309): Caused by: java.lang.NullPointerException
09-20 05:16:22.543: E/AndroidRuntime(309):  at com.mkyong.android.AndroidGoogleMapsActivity.onCreate(AndroidGoogleMapsActivity.java:27)
09-20 05:16:22.543: E/AndroidRuntime(309):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-20 05:16:22.543: E/AndroidRuntime(309):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-20 05:16:22.543: E/AndroidRuntime(309):  ... 11 more
09-20 05:16:25.141: I/Process(309): Sending signal. PID: 309 SIG: 9
4

1 に答える 1

0

この NullPointerException を引き起こすレイアウト ファイルに問題があるようです。

// This command tries to get the mapView but fails to find it, so it returns null, instead of the mapView
MapView mapView = (MapView) findViewById(R.id.mapView);
// This tries to call a method on null, which will cause a NullPointerException.
mapView.setBuiltInZoomControls(true);

このチェックを解決するにlayout/map_activity.xmlは、マップで実際に次のように mapView という名前が付けられていることを確認します。

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mapView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.google.android.gms.maps.SupportMapFragment" />
于 2013-07-11T12:48:00.060 に答える