无法解析助手类中的getSupportFragmentManager()

时间:2019-02-22 17:46:06

标签: java android here-api

我正在尝试按照此处maps api上的教程来学习Java,以制作一个Android应用程序。在本教程中,有一个辅助类MapInitializer在Main Activity中调用。函数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"/>

如何获取片段?任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

出现此错误是因为变量private final Activity activity;是活动类型,getSupportFragmentManager()仅可用于FragmentActivity和AppCompatActivity子类。

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

private final AppCompatActivity activity;

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

    // your code
}

希望这会有所帮助。