地图活动出错

时间:2012-09-20 05:35:40

标签: java android google-maps map

我开发了一个简单的mapActivity,用户通过使用对话框警报和定位服务的意图来检查他是否启用了gps系统。如果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

1 个答案:

答案 0 :(得分: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" />