2012-09-20 28 views
0

我開發了一個簡單的mapActivity,也就是說用戶通過使用對話框警報和定位服務的意圖來檢查他是否啓用了GPS系統。如果GPS啓用它直接到地圖查看,但這不會發生在我身上。當我按下去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 
+0

對不起你們,我新來這個論壇感謝您的幫助 – jiji

+0

的問題是在你的AndroidGoogleMapsActivity .onCreate()方法,如果可能,請張貼方法的代碼以及行號,或者告訴我們什麼行是第27行,因爲這是拋出NullPointerException的地方 –

+0

好吧我編輯我的問題與類,我知道錯誤是在27行,但我不知道如何解決它,謝謝你 – jiji

回答

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" />