2013-06-01 70 views
1

我有一個GoogleMap v2應用程序。在我的設備(Galaxy Nexus)中工作正常,但在其他手機中它會崩潰,我不知道爲什麼。我的代碼:NullPointerException GoogleMaps在某些設備上

public class Activity_Maps extends android.support.v4.app.FragmentActivity { 
private GoogleMap map = null; 
    //... 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_game); 
    setUpMapIfNeeded(); // Verify Google Maps and load it 
    //... 
    map.setMapType(GoogleMap.MAP_TYPE_SATELLITE); // HERE CRASH !!!!!!!!! 
    //... 
    } 

private void setUpMapIfNeeded() { 
    if (map == null) { 
     if (isGoogleMapsInstalled()) { 
      map = ((SupportMapFragment) getSupportFragmentManager() 
        .findFragmentById(R.id.map)).getMap(); 
      Notification notification = new Notification(getBaseContext(), 
        "GoogleMaps", getResources().getString(
          R.string.googlemaps_found)); 
      notification.show(); 
     } else { 
      dialogNeedGoogleMap(); 
     } 
    } else { 
     Log.i(TAG, "map != null"); 
    } 
} 

public boolean isGoogleMapsInstalled() { 
    try { 
     getPackageManager().getApplicationInfo(
       "com.google.android.apps.maps", 0); 
     return true; 
    } catch (PackageManager.NameNotFoundException e) { 
     return false; 
    } 
} 

    /* 
    * Go GooglePlay to install GoogleMap 
    */ 
public void dialogNeedGoogleMap() { 
    final String MARKET = "market://details?id=com.google.android.apps.maps"; 
    AlertDialog.Builder dialog = new AlertDialog.Builder(this); 
    dialog.setTitle(getResources().getString(
      R.string.googlemaps_need1)); 
    dialog.setMessage(getResources().getString(
      R.string.googlemaps_need2)); 
    dialogo.setCancelable(false); 

    dialog.setPositiveButton(getResources().getString(R.string.ok), 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri 
          .parse(MARKET)); 
        startActivity(intent); 
        finish(); 
       } 
      }); 

    dialog.setNegativeButton(getResources().getString(R.string.cancel), 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        finish(); 
       } 
      }); 
    dialog.show(); 
} 

我的地圖中的xml:

<fragment 
    android:id="@+id/map" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    class="com.google.android.gms.maps.SupportMapFragment" /> 

我覺得崩潰可能來自設備,而不GoogleMap的,但在這種情況下應該顯示一個對話框,讓選擇安裝或退出而不加載地圖。在這種情況下,應該顯示一個對話框給出選項來安裝它或退出。

我趕上從Google分析異常,一切又都在該行:

map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);  

我能趕上這個例外,並顯示谷歌地圖的對話,但我不明白的代碼是怎麼來這裏之前。在我的手機中,它工作正常。

回答

2

您正在做錯誤的檢​​查。使用GooglePlayServicesUtil.isGooglePlayServicesAvailable而不是尋找不相關的應用程序。

,並把這個行:

map.setMapType(GoogleMap.MAP_TYPE_SATELLITE); // HERE CRASH !!!!!!!!! 

檢查中,有人進行而不是之後。此代碼將無條件執行,因此您將始終在未安裝Google Play服務的設備上獲得NPE。

您也可以在自己的設備上進行測試。只需卸載Google Play服務即可。

+0

謝謝,它適用於我。我會在其他設備上測試。 – k0nig

相關問題