在一些设备NullPointerException异常的GoogleMaps异常、设备、NullPointerException、GoogleMaps

2023-09-06 10:26:27 作者:亡命沵的心海つ

我有GoogleMap的V2的应用程序。在我的设备(Galaxy Nexus的)工作正常,但在其它手机它崩溃,我不知道为什么。我的code:

I have an app with GoogleMap v2. In my device (Galaxy Nexus) works fine but in other phones it crash and I don't know why. My code:

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映射:

My map in xml:

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

我觉得崩溃可能来自设备,而无需GoogleMap的,但在这种情况下应该显示一个对话框,让选择安装还是退出而不加载地图。在这种情况下应该显示一个对话框,让选择安装或退出。

I think crashes could come from devices without GoogleMap but in this case should show a dialog to give option to install or exit without load the map. In this case should show a dialog to give option to install it or exit.

我从Google分析赶上例外,一切又都在该行:

I catch exception from GoogleAnalytics and all they are in the line:

 map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);      

我可以捕获此异常,并显示的GoogleMaps的对话,但我不明白如何code谈到这里为止。在我的手机,它工作正常。

I could catch this exception and show dialog of GoogleMaps but I can not understand how code comes until here. In my phone it works fine.

推荐答案

您是做了错误的检查。使用 GooglePlayServicesUtil.isGooglePlayServicesAvailable ,而不是找不相关的应用。

You are doing the wrong check. Use GooglePlayServicesUtil.isGooglePlayServicesAvailable instead of looking for unrelated application.

,并把这个行:

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

检查里面,而不是之后进行。这code将无条件执行,所以你总是会得到NPE上没有谷歌播放服务安装的设备。

inside the check, not after it was performed. This code will execute unconditionally, so you will always get NPE on devices which have no Google Play Services installed.

您也可以你自己的设备上进行测试。只需卸载谷歌播放服务。

You may also test this on your own device. Just uninstall Google Play Services.