gpt4 book ai didi

java - 无法解析帮助​​程序类中的 getSupportFragmentManager()

转载 作者:行者123 更新时间:2023-11-29 23:18:49 26 4
gpt4 key购买 nike

我正在尝试按照此处 map API 上的教程来制作 Android 应用程序来学习 Java。在教程中,有一个在 Main Activity 中调用的辅助类 MapInitializer。函数 initializeMap 有一个 MapFragment 和 getFragmentManager(),我了解到它们已被弃用。当我试图将其更改为 getSupportFragmentManager() 以获取 fragment 时,它说无法解析该方法。我认为这与 Activity 前缀有关,因为我在另一个教程中使用了 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"/>

如何获得 fragment ?任何帮助将不胜感激!

最佳答案

您收到此错误是因为您的变量 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
}

希望这对您有所帮助。

关于java - 无法解析帮助​​程序类中的 getSupportFragmentManager(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54832520/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com