【发布时间】:2015-02-16 11:10:08
【问题描述】:
我有两个片段和一个具有导航抽屉的主要活动。在导航抽屉中,我有两个菜单项,即 Home 和 Places。我为这两个菜单项创建了 xml 片段。
fragment_home.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp"
tools:context="com.gaurav.googlemap.HomeMap" >
<fragment
class="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent">
</fragment>
</RelativeLayout>
fragment_places.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Places"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Java 代码
package com.beproject.ourway;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
public class MainHome extends FragmentActivity{
private String[] mNavigationDrawerItemTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
Fragment fragment = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_home);
// Initialization
mNavigationDrawerItemTitles= getResources().getStringArray(R.array.navigation_drawer_items_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// Defining drawer items
ObjectDrawerItem[] drawerItem = new ObjectDrawerItem[2];
drawerItem[0] = new ObjectDrawerItem(R.drawable.ic_launcher, "Home");
drawerItem[1] = new ObjectDrawerItem(R.drawable.ic_launcher, "Places");
DrawerItemCustomAdapter adapter =
new DrawerItemCustomAdapter(this, R.layout.home_drawer_item, drawerItem);
mDrawerList.setAdapter(adapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
// selectItem(0);
}
public class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
void selectItem(int position) {
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new PlacesFragment();
break;
default:
break;
}
if (fragment != null) {
mDrawerLayout.closeDrawer(mDrawerList);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
getActionBar().setTitle(mNavigationDrawerItemTitles[position]);
} else {
Log.e("MainActivity", "Error in creating fragment");
}
}
public class HomeFragment extends Fragment {
public HomeFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
return rootView;
}
}
public class PlacesFragment extends Fragment {
public PlacesFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_places, container, false);
return rootView;
}
}
}
问题
当我运行项目时,我点击主页菜单,然后它会以很好的方式向我显示带有地图的主页片段。之后我点击地方然后它显示我的地方片段很好。 但是当我再次点击主页时,我的应用程序将因以下异常而崩溃
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gaurav.googlemap/com.gaurav.googlemap.MainActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
java 文件中是否有任何更正以正确打开片段?
谢谢
LogCat
02-16 16:50:58.324: V/Provider/Setting(25249): from settings cache , name = sound_effects_enabled value = 0
02-16 16:50:58.358: D/AndroidRuntime(25249): Shutting down VM
02-16 16:50:58.359: W/dalvikvm(25249): threadid=1: thread exiting with uncaught exception (group=0x4188b908)
02-16 16:50:58.388: E/AndroidRuntime(25249): FATAL EXCEPTION: main
02-16 16:50:58.388: E/AndroidRuntime(25249): android.view.InflateException: Binary XML file line #8: Error inflating class fragment
02-16 16:50:58.388: E/AndroidRuntime(25249): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
02-16 16:50:58.388: E/AndroidRuntime(25249): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
02-16 16:50:58.388: E/AndroidRuntime(25249): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
【问题讨论】:
-
请发布您的完整 LogCat
-
我认为这是因为您使用 support.v4.app.FragmentActivity 和 android.app.Fragment,您应该使用 support.v4.app.Fragment
-
在哪里更改我的代码? @SboneloMbhamali
-
@93gaurav93:在
HomeFragment中扩展SupportMapFragment而不是Fragment -
删除这一行:import android.app.Fragment;并将其替换为 import android.support.v4.app.Fragment;
标签: java android xml android-layout android-fragments