【问题标题】:AndroidXMapFragment returns null when initializing from fragmentAndroidXMapFragment 从片段初始化时返回 null
【发布时间】:2020-01-23 09:50:58
【问题描述】:

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

我在我的活动项目中实施,但我需要像以前一样在片段上工作。

我将所有内容都替换为 AndroidX:

来自活动作品

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()方法返回null

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

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

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 {
    }
}

框架网布局

<?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>

【问题讨论】:

  • 尝试使用getChildFragmentManager()而不是m_activity.getSupportFragmentManager()
  • @Md.Asaduzzaman Cannot resolve method 'getChildFragmentManager()' m_activity.getChildFragmentManager() 相同
  • 你能添加你的片段代码吗?
  • @Md.Asaduzzaman 补充说,我尝试过这种方式,当然还有其他一些方式,但都没有奏效,它只是说片段为空!

标签: android here-api


【解决方案1】:

这是一个特定于 Android 的问题,然后是 SDK,为什么以下返回 null:

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

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

【讨论】:

  • 你有上面我试图在框架上实现的示例类。而且我在您的存储库中找不到任何以片段形式实现地图的示例。如果您有任何示例,请分享。
  • 我还添加了片段布局。
  • 这是应用程序方面的问题。我们没有时间重新创建用户应用程序问题。没有样本 - 没有调查。
【解决方案2】:

我想通了。如果有人在从 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
    }
}

【讨论】:

    猜你喜欢
    • 2018-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 2020-03-26
    • 2017-05-19
    相关资源
    最近更新 更多