【问题标题】:Google Maps - Android App not load - null object reference谷歌地图 - Android 应用程序未加载 - 空对象引用
【发布时间】:2015-05-21 23:54:29
【问题描述】:

我目前正在尝试开发一个与谷歌地图集成的 Android 应用程序。目前我很难找到错误,因为代码来自谷歌网站本身,除了它是一个 SupportMapFragment。如果您知道 SupportMapFragment 的实际教程也会很棒。实际,因为我认为.getMap() 已贬值。如果我应该发布整个代码,请告诉我(我是 stackoverflow 的新手)

如果尝试使用此代码:

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsFragment extends SupportMapFragment implements OnMapReadyCallback {

函数在哪里:

    @Override
     public void onResume() {
    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
    SupportMapFragment supportMapFragment = (SupportMapFragment) fragmentManager
            .findFragmentById(R.id.map);

    supportMapFragment.getMapAsync(this);
}

错误是:java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference

片段 xml 如下所示:

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

【问题讨论】:

    标签: java android google-maps


    【解决方案1】:

    地图片段正在作用于 Lollipop,您可能需要它。

    SupportMapFragment supportMapFragment;
    if (Build.VERSION.SDK_INT < 21) {
        supportMapFragment = (SupportMapFragment) getActivity()
            .getSupportFragmentManager().findFragmentById(R.id.map);
    } else {
        supportMapFragment = (SupportMapFragment) getActivity()
            .getChildFragmentManager().findFragmentById(R.id.map);
    }
    supportMapFragment.getMapAsync(this);
    

    【讨论】:

    • 此代码有所帮助,但应用程序仍然崩溃。出现此错误:java.lang.NullPointerException: Attempt to invoke interface method 'void com.google.maps.api.android.lib6.c.ey.o()' on a null object reference
    • 这是我目前得到的确切错误 - “空对象引用上的 'void com.google.maps.api.android.lib6.c.ey.o()'”
    【解决方案2】:

    看来您需要使用FragmentSupportFragmentManager 看这个findFragmentById for SupportMapFragment returns null in Android Studio的答案

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 2014-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      相关资源
      最近更新 更多