【发布时间】:2019-10-01 21:22:11
【问题描述】:
所以我正在为我的 Android 应用程序使用默认的导航抽屉。我想要做的是将按钮链接到我创建的片段。
在我创建的片段中,我正在尝试打开 Google 地图。
然而,每当我点击按钮时,我都会遇到这个崩溃:
这是生成的错误(我没有语法错误):
E/FragmentManager: No view found for id 0x7f0d0089 (com.example.x.x:id/mapView) for fragment LocationFragment{35b5044 #1 id=0x7f0d0089}
这是默认菜单中的代码(我尽量只放需要的代码):
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
FragmentManager fm = getFragmentManager();
}
在相同的默认导航抽屉中,我们还有 onNavigationItemSelected(MenuItem item) 函数,当您单击菜单项时执行一个操作,这就是我让 FragmentManager 调用我的 LocationFragment 的地方:
public boolean onNavigationItemSelected(MenuItem item) {
FragmentManager fm = getFragmentManager();
int id = item.getItemId();
if (id == R.id.nav_camara) {
// Handle the camera action
} else if (id == R.id.nav_share) {
fm.beginTransaction().replace(R.id.mapView, new LocationFragment()).commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
这里我们有 LocationFragment Java 类(包括最重要的功能):
public class LocationFragment extends Fragment {
MapView mMapView;
private GoogleMap googleMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflate and return the layout
View v = inflater.inflate(R.layout.location_fragment, container,
false);
mMapView = (MapView) v.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();// needed to get the map to display immediately
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
googleMap = mMapView.getMap();
// latitude and longitude
double latitude = 17.385044;
double longitude = 78.486671;
// create marker
MarkerOptions marker = new MarkerOptions().position(
new LatLng(latitude, longitude)).title("Hello
return v;
}
LocationFragment的布局文件(xml)(称为location_fragment.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
我不明白为什么会出现错误,因为我已经检查了所有类中的所有 ID 可能有 100 次。我似乎被困在这里。任何帮助将不胜感激
提前致谢
添加了activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout"
android:layout_width="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true" tools:openDrawer="start">
<include layout="@layout/app_bar_main" android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView android:id="@+id/nav_view"
android:layout_width="wrap_content" android:layout_height="match_parent"
android:layout_gravity="start" android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
【问题讨论】:
-
你清理过你的项目吗?
-
错误似乎是从这一行产生的:fm.beginTransaction().replace(R.id.mapView, new LocationFragment()).commit();您可能想要的是用 location_fragment.xml 替换 activity_main 布局中的框架。所以 R.id.mapView 必须替换为该框架的 id。
-
但是我的activity_main布局是我加载屏幕后弹出的布局?
-
你的activity_main布局应该有一个“容器”布局,可能是一个FrameLayout,前一个片段所在的地方。当您单击导航抽屉中的按钮时,您想用 LocationFragment 的布局替换该容器的内容,因此您必须使用容器的 id 作为 replace() 的第一个参数。我希望您理解,如果没有,请包含您的 activity_main.xml,以便我发布完整的答案。
-
已添加(问题底部)。我不太完全理解您的方法,因为我注意到它是导航抽屉中创建 activity_main.xml 的默认模板。为了让我的程序顺利运行,是否必须编辑这个 xml 文件?谢谢
标签: java android android-fragments