点击onMyLocationButtonClick后,Android崩溃的谷歌地图

时间:2014-06-03 20:47:56

标签: android google-maps gps

在这种情况下,我的代码因某种原因崩溃了。 如果GPS关闭,那么我运行应用程序,在我的应用程序运行后打开我的GPS ,点击onMyLocationButtonClick,然后崩溃。 我会说,如果在运行应用程序之前我的GPS已打开,相机会进行动画制作 点击按钮,我到我的位置。 我看到了logcat,但仍无法找到问题所在。 但我看到它与我用我的手机(Galaxy S4)运行应用程序有关。 我做了什么猫?

我的Erros log cat

        06-03 23:28:48.961: E/ViewRootImpl(4527): sendUserActionEvent() mView == null
06-03 23:28:54.491: E/AndroidRuntime(4527): FATAL EXCEPTION: main
06-03 23:28:54.491: E/AndroidRuntime(4527): java.lang.NullPointerException
06-03 23:28:54.491: E/AndroidRuntime(4527):     at nir.rauch.flantir.MainActivity$1.onMyLocationButtonClick(MainActivity.java:58)
06-03 23:28:54.491: E/AndroidRuntime(4527):     at com.google.android.gms.maps.GoogleMap$2.onMyLocationButtonClick(Unknown Source)
06-03 23:28:54.491: E/AndroidRuntime(4527):     at com.google.android.gms.maps.internal.m$a.onTransact(Unknown Source)
06-03 23:28:54.491: E/AndroidRuntime(4527):     at android.os.Binder.transact(Binder.java:347)
06-03 23:28:54.491: E/AndroidRuntime(4527):     at foj.a(SourceFile:81)
06-03 23:28:54.491: E/AndroidRuntime(4527):     at lxw.onClick(Unknown Source)
06-03 23:28:54.491: E/AndroidRuntime(4527):     at android.view.View.performClick(View.java:4475)
06-03 23:28:54.491: E/AndroidRuntime(4527):     at android.view.View$PerformClick.run(View.java:18786)
06-03 23:28:54.491: E/AndroidRuntime(4527):     at android.os.Handler.handleCallback(Handler.java:730)
06-03 23:28:54.491: E/AndroidRuntime(4527):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-03 23:28:54.491: E/AndroidRuntime(4527):     at android.os.Looper.loop(Looper.java:137)
06-03 23:28:54.491: E/AndroidRuntime(4527):     at android.app.ActivityThread.main(ActivityThread.java:5419)
06-03 23:28:54.491: E/AndroidRuntime(4527):     at java.lang.reflect.Method.invokeNative(Native Method)
06-03 23:28:54.491: E/AndroidRuntime(4527):     at java.lang.reflect.Method.invoke(Method.java:525)
06-03 23:28:54.491: E/AndroidRuntime(4527):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
06-03 23:28:54.491: E/AndroidRuntime(4527):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
06-03 23:28:54.491: E/AndroidRuntime(4527):     at dalvik.system.NativeStart.main(Native Method)

我的活动中崩溃的部分:

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Criteria;
import android.location.LocationManager;
import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMyLocationButtonClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;

public class MainActivity extends Activity {

    public GoogleMap map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainmap);
        final GoogleMap map = ((MapFragment) getFragmentManager()
                .findFragmentById(R.id.map)).getMap();

        map.setMyLocationEnabled(true);

        final LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        final android.location.Location location = locationManager
                .getLastKnownLocation(locationManager.getBestProvider(criteria,
                        false));

        if (location != null) {
            map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
                    location.getLatitude(), location.getLongitude()), 10));
        }



        map.setOnMyLocationButtonClickListener(new OnMyLocationButtonClickListener() {

            @Override
            public boolean onMyLocationButtonClick() {
                if (!locationManager
                        .isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                    buildAlertMessageNoGps();
                } else {

            map.animateCamera(CameraUpdateFactory.newLatLngZoom(
                            new LatLng(location.getLatitude(),location.getLongitude()), 15));
                    return false;
                }
                return false;
            }
        });
    }

0 个答案:

没有答案