致命异常 java.lang.RuntimeException: 无法启动 activity

Fatal Exception java.lang.RuntimeException: Unable to start activity

我是一名在大学学习的法国学生,我必须制作一个 Android 使用 Google 地图的应用程序。不幸的是,当我尝试在 phone 上使用我的应用程序时遇到问题:当我尝试访问带有 GoogleMap 的部分时,应用程序关闭。 感谢您的帮助。

package darknesshadow.localisation;

import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.widget.Toast;

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

public class MapsActivity extends AppCompatActivity implements LocationListener{

    //Declaration des Variables
    private GoogleMap gMap;
    private LocationManager locationManager;
    private Marker marker;


    //Méthodes
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        gMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
        marker = gMap.addMarker(new MarkerOptions().title("Vous êtes ici").position(new LatLng(0, 0)));
    }

    @Override
    protected void onResume() {
        super.onResume();

        locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
            activationGPS();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        desactivationGPS();
    }

    public void activationGPS() {
        if ((ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
                && (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, this);
        }
    }

    public void desactivationGPS() {
        if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            locationManager.removeUpdates(this);
        }
    }

    @Override
    public void onLocationChanged(final Location location) {
        final StringBuilder msg = new StringBuilder("lat : ");
        msg.append(location.getLatitude());
        msg.append("; lng : ");
        msg.append(location.getLongitude());
        Toast.makeText(this, msg.toString(), Toast.LENGTH_SHORT).show();
        final LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
        gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15));
        marker.setPosition(latLng);

    }

    @Override
    public void onProviderDisabled(final String provider) {
        if ("gps".equals(provider)) {
            desactivationGPS();
        }
    }

    @Override
    public void onProviderEnabled(final String provider) {
        if ("gps".equals(provider)) {
            activationGPS();
        }
    }

    @Override
    public void onStatusChanged(final String provider, final int status, final Bundle extras) {
    }
} 

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="darknesshadow.localisation"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"
        />

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="17" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses_permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key" />

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version"
            />

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".NouveauTrajet"
            android:label="Nouveau Trajet">
        </activity>

        <activity
            android:name=".Tab"
            android:label="Tab">
        </activity>

        <activity
            android:name=".OngletAcceuil"
            android:label="OngletAcceuil">
        </activity>

        <activity
            android:name=".Onglet1"
            android:label="Onglet1">
        </activity>

        <activity
            android:name=".Onglet2"
            android:label="Onglet2">
        </activity>

        <activity
            android:name=".Onglet3"
            android:label="Onglet3">
        </activity>

        <activity
            android:name=".Onglet4"
            android:label="Onglet4">
        </activity>

        <activity
            android:name=".MapsActivity"
            android:label="@string/title_activity_maps">
        </activity>

    </application>

</manifest>

XML:

fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:map="http://schemas.android.com/apk/res-auto"
/>

和 Android 监视器:

03-12 19:01:38.881 17146-17146/darknesshadow.localisation E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: darknesshadow.localisation, PID: 17146
                                                                        java.lang.RuntimeException: Unable to start activity ComponentInfo{darknesshadow.localisation/darknesshadow.localisation.MapsActivity}: java.lang.NullPointerException
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2187)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2236)
                                                                            at android.app.ActivityThread.access0(ActivityThread.java:138)
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                            at android.os.Looper.loop(Looper.java:136)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5095)
                                                                            at java.lang.reflect.Method.invokeNative(Native Method)
                                                                            at java.lang.reflect.Method.invoke(Method.java:515)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
                                                                            at dalvik.system.NativeStart.main(Native Method)
                                                                         Caused by: java.lang.NullPointerException
                                                                            at darknesshadow.localisation.MapsActivity.onCreate(MapsActivity.java:33)
                                                                            at android.app.Activity.performCreate(Activity.java:5231)
                                                                            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2151)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2236) 
                                                                            at android.app.ActivityThread.access0(ActivityThread.java:138) 
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199) 
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                            at android.os.Looper.loop(Looper.java:136) 
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5095) 
                                                                            at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                            at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) 
                                                                            at dalvik.system.NativeStart.main(Native Method) 

您在片段管理器或地图片段上有一个空指针。当您使用 AppCompatActivity 时,请尝试使用 getSupportFragmentManager() 而不是 getFragmentManager()。

在布局文件中您指的是 SupportMapFragment,但在 activity 中您使用的是 MapFragment, 将布局 XML 文件更改为 MapFragment