gpt4 book ai didi

java - android.view.InflateException 膨胀类 fragment 时出错

转载 作者:行者123 更新时间:2023-12-02 04:58:12 29 4
gpt4 key购买 nike

我已经尝试解决这个问题 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)

当我单击抽屉导航中的“主页”菜单项时,它工作正常。但是当我再次单击“主页”后,我收到此异常

02-16 20:08:05.714: E/AndroidRuntime(5999): android.view.InflateException: Binary XML file line #8: Error inflating class fragment 

java代码有修改吗?请帮我解决这个问题。
谢谢

最佳答案

将以下变量添加到 Fragment 类中:

private static View view;

然后在同一个 fragment 类中尝试替换 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;

}

关于java - android.view.InflateException 膨胀类 fragment 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28554054/

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