从片段初始化时 AndroidXMapFragment returns null

AndroidXMapFragment returns null when initializing from fragment

SupportMapFragment 在这里 sdk 中被弃用,当我开始导航时,在屏幕旋转时它崩溃了。

我更新到最新版本的 sdk 并按照官方文档中的说明进行操作: https://github.com/heremaps/here-android-sdk-examples/tree/master/turn-by-turn-navigation/app/src/main/java/com/here/android/example/guidance

我在 activity 中实现了我的项目,但我需要它像以前一样处理片段。

我将所有内容替换为 AndroidX:

来自 Activity 作品

import androidx.appcompat.app.AppCompatActivity;
public MapFragmentView(AppCompatActivity activity) {
   m_activity = activity;
   initMapFragment();
   initNaviControlButton();
}
private AndroidXMapFragment getMapFragment() {
    return (AndroidXMapFragment) m_activity.getSupportFragmentManager().findFragmentById(R.id.mapfragment);
}

从 Activity 调用此方法有效

MapFragmentView mapFragmentView = new MapFragmentView (this);

当我从 Fragment 调用它时

MapFragmentView mapFragmentView = new MapFragmentView (getActivity ());

This does not accept getActivity() because is not compatible.
androidx.appcompat.app.AppCompatActivity in MapFragmentView can not be applied to androidx.fragment.app.ragmentActivity

我尝试转换它,它接受了

AppCompatActivity appCompatActivity = (AppCompatActivity) getActivity ();
MapFragmentView mapFragmentView = new MapFragmentView (appCompatActivity);

但现在方法 getMapFragment() return null

private AndroidXMapFragment getMapFragment() {
    /* this returns null */ return (AndroidXMapFragment) m_activity.getSupportFragmentManager().findFragmentById(R.id.mapfragment);
}

这是片段 class,我删除了其他代码只是为了简化

package com.example.Fragments;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.drivosity.drivosity.R;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;

public class FragmentHome extends Fragment {

    private final static int REQUEST_CODE_ASK_PERMISSIONS = 1;
    private static final String[] RUNTIME_PERMISSIONS = {
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.INTERNET,
            Manifest.permission.ACCESS_WIFI_STATE,
            Manifest.permission.ACCESS_NETWORK_STATE
    };
    private final String TAG = "FragmentHome";
    private View view;
    // Directions


    public FragmentHome() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        view = inflater.inflate (R.layout.fragment_home, container, false);

        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated (view, savedInstanceState);

        if (hasPermissions(getActivity (), RUNTIME_PERMISSIONS)) {
            setupMapFragmentView();
        } else {
            ActivityCompat
                    .requestPermissions(getActivity (), RUNTIME_PERMISSIONS, REQUEST_CODE_ASK_PERMISSIONS);
        }
    }

    /**
     * Only when the app's target SDK is 23 or higher, it requests each dangerous permissions it
     * needs when the app is running.
     */
    private static boolean hasPermissions(Context context, String... permissions) {
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && permissions != null) {
            for (String permission : permissions) {
                if (ActivityCompat.checkSelfPermission(context, permission)
                        != PackageManager.PERMISSION_GRANTED) {
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CODE_ASK_PERMISSIONS: {
                for (int index = 0; index < permissions.length; index++) {
                    if (grantResults[index] != PackageManager.PERMISSION_GRANTED) {

                        /*
                         * If the user turned down the permission request in the past and chose the
                         * Don't ask again option in the permission request system dialog.
                         */
                        if (!ActivityCompat
                                .shouldShowRequestPermissionRationale(getActivity (), permissions[index])) {
//                            Toast.makeText(this, "Required permission " + permissions[index]
//                                            + " not granted. "
//                                            + "Please go to settings and turn on for sample app",
//                                    Toast.LENGTH_LONG).show();
                        } else {
//                            Toast.makeText(this, "Required permission " + permissions[index]
//                                    + " not granted", Toast.LENGTH_LONG).show();
                        }
                    }
                }

                setupMapFragmentView();
                break;
            }
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

    private void setupMapFragmentView() {
        // All permission requests are being handled. Create map fragment view. Please note
        // the HERE Mobile SDK requires all permissions defined above to operate properly.
        AppCompatActivity appCompatActivity = (AppCompatActivity) getActivity ();
        MapFragmentView mapFragmentView = new MapFragmentView (appCompatActivity);

    }

    public interface OnFragmentInteractionListener {
    }
}

Framgnet 布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:mapbox="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/homeToolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/AppTheme"
        app:popupTheme="@style/AppTheme"
        android:visibility="gone">

        <RelativeLayout
            android:id="@+id/homeToolbarItems"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="visible">

            <TextView
                android:id="@+id/appTitle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Home"
                android:textAlignment="center"
                android:textColor="@color/color_white"
                android:textSize="20sp"
                android:textStyle="bold" />


        </RelativeLayout>

    </androidx.appcompat.widget.Toolbar>

    <!-- Map Fragment embedded with the map object -->
    <fragment
        class="com.here.android.mpa.mapping.AndroidXMapFragment"
        android:id="@+id/mapfragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


</RelativeLayout>

这是一个问题 Android 特定于 SDK,为什么以下 return 为空:

m_activity.getSupportFragmentManager().findFragmentById(R.id.mapfragment);

我们从未在样本中观察到这个问题。请提供您发现问题的示例应用程序,我们将进行查看。

我明白了。这是如果有人在 Fragment 中实现它时遇到问题。

但这适用于 SupportMapFragment,也适用于 AndroidXMapFragment

public class FragmentHome extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate (R.layout.fragment_home, container, false);

        SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager ().findFragmentById (R.id.mapFragment);
        MapFragmentView fragmentMap = new MapFragmentView ((AppCompatActivity) getActivity (), mapFragment);

        return view;
    }
}

在 MapFragmentView 构造函数中,我为 SupportMapFragment 添加了一个参数

public class MapFragmentView {

    private AppCompatActivity m_activity;
    private SupportMapFragment mapFragment;

    public MapFragmentView(AppCompatActivity activity, SupportMapFragment mapFragment) {
        m_activity = activity;
        this.mapFragment = mapFragment;
        initialize (); // This is init map
    }
}