gpt4 book ai didi

android - 无法将 Google map 添加到抽屉导航 fragment

转载 作者:行者123 更新时间:2023-11-30 02:35:37 25 4
gpt4 key购买 nike

我正在尝试在 Android 中制作一个应用程序,我想在抽屉导航中添加一个 map 。抽屉选项打开新 fragment 但无法使 map fragment 工作。

MainActivity.java 以下列方式设置 fragment :

/**
* Diplaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new PoliFragment();
break;
case 2:
fragment = new TilefonaFragment();
break;
case 3:
fragment = new DromologiaFragment();
break;
case 4:
fragment = new XartisFragment();
break;

default:
break;
}

if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();

// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
//setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}

map fragment (XartisFragment.java)如下:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;


public class XartisFragment extends Fragment {
private GoogleMap map;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.map_layout,container, false);

map = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMap();//((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();
return rootView;
}


}

ma​​p_layout.xml 如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp" />


</RelativeLayout>

当我尝试运行它时,我得到:

Error:(178, 15) error: incompatible types: XartisFragment cannot be converted to Fragment

它将错误指向 MainActivity.java 中的这一行

fragment = new XartisFragment();

关于要更改什么以使其加载有什么想法吗?

最佳答案

尝试使用 getSupportFragmentManager() 而不是 getFragmentManager()

确保两个地方都导入相同的 Fragment 类。感觉有点像在一个地方导入 android.app.Fragment(原生 API 级别 11 版本的 fragment ),而在其他地方导入 android.support.v4.app.Fragment(来自 Android Support 的 fragment )包)。

关于android - 无法将 Google map 添加到抽屉导航 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26606473/

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