当前位置: 代码迷 >> java >> 无法解析 helper 类中的 getSupportFragmentManager()
  详细解决方案

无法解析 helper 类中的 getSupportFragmentManager()

热度:27   发布时间:2023-08-04 09:13:33.0

我正在尝试通过遵循 here maps api 上的教程来学习 java 以制作 android 应用程序。 在本教程中,有一个在 Main Activity 中调用的辅助类 MapInitializer。 函数 initializeMap 有一个 MapFragment 和 getFragmentManager(),我了解到它们已被弃用。 当我尝试将其更改为 getSupportFragmentManager() 以获取片段时,它表示无法解析该方法。 我认为这与活动前缀有关,因为我在另一个教程中使用了 getSupportFragmentManager 并且它有效。 我曾尝试使用 FragmentActivity 和 AppCompatActivity 扩展该类,但它不起作用。 这是帮助类:

package helper;
import smart.wheels.R;

import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.ContextCompat;
import android.util.Log;

import com.here.android.mpa.mapping.SupportMapFragment;
import com.here.android.mpa.common.MapSettings;
import com.here.android.mpa.common.OnEngineInitListener;
import com.here.android.mpa.mapping.Map;
import com.here.android.mpa.mapping.MapFragment;

import java.io.File;
import java.util.ArrayList;

public class MapInitializer {

    private static final String LOG_TAG = MapInitializer.class.getName();
    private static final int PERMISSIONS_REQUEST_CODE = 42;
    private static final String MAP_SERVICE_INTENT_NAME = "com.here.msdkui.example.MapService";

    private final ResultListener resultListener;
    private final Activity activity;

    public interface ResultListener {
        void onMapInitDone(Map map);
    }

    public MapInitializer(@NonNull Activity activity, @NonNull ResultListener resultListener) {
        this.activity = activity;
        this.resultListener = resultListener;

        String[] missingPermissions = getPermissionsToRequest();
        if (missingPermissions.length == 0) {
            initializeMap();
        } else {
            ActivityCompat.requestPermissions(activity, missingPermissions, PERMISSIONS_REQUEST_CODE);
        }
    }

    private void initializeMap() {
        String cacheLocation =
                activity.getApplicationContext().getFilesDir().getPath() + File.separator + "example_maps_cache";
        if (MapSettings.setIsolatedDiskCacheRootPath(cacheLocation, MAP_SERVICE_INTENT_NAME)) {
            SupportMapFragment mapFragment = (SupportMapFragment) activity.getSupportFragmentManager().findFragmentById(R.id.mapfragment);
            mapFragment.init(error -> {
                if (error == OnEngineInitListener.Error.NONE) {
                    mapFragment.getPositionIndicator().setVisible(true);
                    resultListener.onMapInitDone(mapFragment.getMap());
                } else {
                    Log.e(LOG_TAG, "Cannot initialize Map Fragment: " + error.getDetails());
                }
            });
        } else {
            Log.e(LOG_TAG, "Unable to set isolated disk cache path.");
        }
    }

    private String[] getPermissionsToRequest() {
        ArrayList<String> permissionList = new ArrayList<>();
        try {
            PackageInfo packageInfo = activity.getPackageManager().getPackageInfo(
                    activity.getPackageName(), PackageManager.GET_PERMISSIONS);
            if (packageInfo.requestedPermissions != null) {
                for (String permission : packageInfo.requestedPermissions) {
                    if (ContextCompat.checkSelfPermission(
                            activity, permission) != PackageManager.PERMISSION_GRANTED) {
                        permissionList.add(permission);
                    }
                }
            }
        } catch (Exception e) {
            Log.e(LOG_TAG, "Cannot read permissions from manifest: " + e.getMessage());
        }
        return permissionList.toArray(new String[permissionList.size()]);
    }

    public void onRequestPermissionsResult(int requestCode, @NonNull int[] grantResults) {
        if (requestCode == PERMISSIONS_REQUEST_CODE) {
            boolean allGranted = true;
            for (int result : grantResults) {
                allGranted &= result == PackageManager.PERMISSION_GRANTED;
            }

            if (allGranted) {
                initializeMap();
            } else {
                Log.e(LOG_TAG, "Required Android permissions denied by user.");
            }
        }
    }
}

和布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<com.here.msdkui.routing.WaypointList
    android:id="@+id/waypointList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<com.here.msdkui.routing.TransportModePanel
    android:id="@+id/transportModePanel"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

<fragment
    class="com.here.android.mpa.mapping.SupportMapFragment"
    android:id="@+id/mapfragment"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"/>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Show route details"
    android:onClick="onRouteDetailsButtonClick"/>

我怎样才能得到片段? 任何帮助将不胜感激!

您收到此错误是因为您的可变private final Activity activity; 是 Activity 类型,getSupportFragmentManager() 仅适用于 FragmentActivity 和 AppCompatActivity 子类。

如果您确定传递给 MapInitializer 构造函数的实例是 AppCompatActivity 类型,则应更改构造函数的签名:

private final AppCompatActivity activity;

public MapInitializer(@NonNull AppCompatActivity activity, @NonNull ResultListener resultListener) {
    this.activity = activity;

    // your code
}

希望这可以帮助。