我试图在5天内解决这个问题,但我仍然没有解决这个问题。
主要Java代码
package com.beproject.ourway;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.os.Handler;
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;
import android.widget.Toast;
public class MainHome extends FragmentActivity{
private String[] mNavigationDrawerItemTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
Fragment fragment = null;
FragmentManager fragmentManager;
@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[6];
drawerItem[0] = new ObjectDrawerItem(R.drawable.ic_launcher, "Home");
drawerItem[1] = new ObjectDrawerItem(R.drawable.ic_launcher, "Places");
drawerItem[2] = new ObjectDrawerItem(R.drawable.ic_launcher, "Friends");
drawerItem[3] = new ObjectDrawerItem(R.drawable.ic_launcher, "Settings");
drawerItem[4] = new ObjectDrawerItem(R.drawable.ic_launcher, "Help");
drawerItem[5] = new ObjectDrawerItem(R.drawable.ic_launcher, "About");
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) {
fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
getActionBar().setTitle(mNavigationDrawerItemTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
Log.e("MainActivity", "Error in creating fragment");
}
}
public static 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 static 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;
}
}
}
主要XML文件
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>
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
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" >
</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>
logcat的
02-16 20:08:05.714: E/AndroidRuntime(5999): FATAL EXCEPTION: main
02-16 20:08:05.714: E/AndroidRuntime(5999): android.view.InflateException: Binary XML file line #8: Error inflating class fragment
02-16 20:08:05.714: E/AndroidRuntime(5999): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
02-16 20:08:05.714: E/AndroidRuntime(5999): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
02-16 20:08:05.714: E/AndroidRuntime(5999): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
02-16 20:08:05.714: E/AndroidRuntime(5999): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
02-16 20:08:05.714: E/AndroidRuntime(5999): at com.beproject.ourway.MainHome$HomeFragment.onCreateView(MainHome.java:105)
02-16 20:08:05.714: E/AndroidRuntime(5999): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
02-16 20:08:05.714: E/AndroidRuntime(5999): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
02-16 20:08:05.714: E/AndroidRuntime(5999): at android.app.BackStackRecord.run(BackStackRecord.java:635)
02-16 20:08:05.714: E/AndroidRuntime(5999): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
02-16 20:08:05.714: E/AndroidRuntime(5999): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
02-16 20:08:05.714: E/AndroidRuntime(5999): at android.os.Handler.handleCallback(Handler.java:615)
02-16 20:08:05.714: E/AndroidRuntime(5999): at android.os.Handler.dispatchMessage(Handler.java:92)
02-16 20:08:05.714: E/AndroidRuntime(5999): at android.os.Looper.loop(Looper.java:153)
02-16 20:08:05.714: E/AndroidRuntime(5999): at android.app.ActivityThread.main(ActivityThread.java:4987)
02-16 20:08:05.714: E/AndroidRuntime(5999): at java.lang.reflect.Method.invokeNative(Native Method)
02-16 20:08:05.714: E/AndroidRuntime(5999): at java.lang.reflect.Method.invoke(Method.java:511)
02-16 20:08:05.714: E/AndroidRuntime(5999): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
02-16 20:08:05.714: E/AndroidRuntime(5999): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
02-16 20:08:05.714: E/AndroidRuntime(5999): at dalvik.system.NativeStart.main(Native Method)
02-16 20:08:05.714: E/AndroidRuntime(5999): Caused by: java.lang.IllegalArgumentException: Binary XML file line #8: Duplicate id 0x7f0c0017, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
02-16 20:08:05.714: E/AndroidRuntime(5999): at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2164)
02-16 20:08:05.714: E/AndroidRuntime(5999): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:297)
当我从导航抽屉中单击主菜单项时,它可以正常工作。但是当我再次点击Home之后,我得到了这个例外
02-16 20:08:05.714: E/AndroidRuntime(5999): android.view.InflateException: Binary XML file line #8: Error inflating class fragment
java代码中是否有任何更正?请帮我解决这个问题。
感谢
答案 0 :(得分:2)
将以下变量添加到Fragment类中:
private static View view;
然后在同一个片段类中尝试替换onCreateView方法:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(view != null)
{
ViewGroup parent = (ViewGroup) view.getParent();
if(parent != null)
{
parent.removeView(view);
}
}
try
{
view = inflater.inflate(R.layout.fragment_places, container, false);
}
catch(InflateException e){
// map is already there, just return view as it is
}
return view;
}