【发布时间】: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 补充说,我尝试过这种方式,当然还有其他一些方式,但都没有奏效,它只是说片段为空!