在Fragment

时间:2018-08-03 05:47:09

标签: android android-fragments dji-sdk

我正在尝试在Android Fragment中使用DJI UX SDK Map-widget。它可以很好地与活动配合使用,但是当我使用带有碎片的小部件时,它就无法正常工作。

有人知道这个问题可能是什么吗?

这是我实现的代码。

public class HomeFragment extends Fragment{
    private MapWidget mapWidget;

    public HomeFragment() {
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_home, container, false);
        mapWidget = inflate.findViewById(R.id.map_widget);
        mapWidget = inflate.findViewById(R.id.map_widget);
        mapWidget.initGoogleMap(new MapWidget.OnMapReadyListener() {
            @Override
            public void onMapReady(@NonNull DJIMap djiMap) {
            }
        });
        return inflate;
    }

    @Override
    public void onResume() {
        super.onResume();
        mapWidget.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mapWidget.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mapWidget.onDestroy();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mapWidget.onLowMemory();
    }
}

StackTrace

  

原因:java.lang.NullPointerException:尝试在空对象引用上调用接口方法“ void com.google.maps.api.android.lib6.impl.bo.o()”           com.google.maps.api.android.lib6.impl.cw.b(:com.google.android.gms.dynamite_mapsdynamite @ 12874021 @ 12.8.74(040306-204998136):21)           com.google.android.gms.maps.internal.w.a(:com.google.android.gms.dynamite_mapsdynamite @ 12874021 @ 12.8.74(040306-204998136):12)           在fc.onTransact(:com.google.android.gms.dynamite_mapsdynamite @ 12874021 @ 12.8.74(040306-204998136):12)           在android.os.Binder.transact(Binder.java:387)           在com.google.android.gms.internal.zzeu.zzb(未知来源)           com.google.android.gms.maps.internal.zzk.onResume上(未知来源)           com.google.android.gms.maps.MapView $ zza.onResume处(未知来源)           在com.google.android.gms.dynamic.zzh.zzb(未知来源)           在com.google.android.gms.dynamic.zzb.zza(未知来源)           位于com.google.android.gms.maps.MapView $ zzb.zza(未知来源)           在com.google.android.gms.dynamic.zza.zza(未知来源)           com.google.android.gms.dynamic.zza.onResume上(未知来源)           com.google.android.gms.maps.MapView.onResume上(未知来源)           在dji.ux.widget.MapWidget.onResume(未知来源)           在com.xxx.xxx.HomeFragment.onResume(HomeFragment.java)

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View inflate = inflater.inflate(R.layout.fragment_home, container, false);
    mapWidget = inflate.findViewById(R.id.map_widget);
    mapWidget.initGoogleMap(new MapWidget.OnMapReadyListener() {
        @Override
        public void onMapReady(@NonNull DJIMap djiMap) {
        }
    });
    mapWidget.onCreate(savedInstanceState); // add this line

    return inflate;
}