Android 带有嵌套片段的AdapterView中不支持addView()
在尝试了标题已有答案中提到的解决方案后,同样的错误似乎仍然存在。我正试图从导航栏中使用的自定义ArrayAdapter中膨胀一个片段。我试图膨胀的碎片本身有两个嵌套的碎片。我的直觉是,它与试图在线性布局中膨胀有关,但我似乎无法让这些碎片膨胀。非常感谢您的帮助 这是错误日志Android 带有嵌套片段的AdapterView中不支持addView(),android,android-fragments,navigation-drawer,android-nested-fragment,Android,Android Fragments,Navigation Drawer,Android Nested Fragment,在尝试了标题已有答案中提到的解决方案后,同样的错误似乎仍然存在。我正试图从导航栏中使用的自定义ArrayAdapter中膨胀一个片段。我试图膨胀的碎片本身有两个嵌套的碎片。我的直觉是,它与试图在线性布局中膨胀有关,但我似乎无法让这些碎片膨胀。非常感谢您的帮助 这是错误日志 10-14 21:22:57.209: E/AndroidRuntime(30555): java.lang.RuntimeException: Unable to start activity ComponentInfo{c
10-14 21:22:57.209: E/AndroidRuntime(30555): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.inviscidlabs.schooled/com.inviscidlabs.schooled.ActivityClassEdit}: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
10-14 21:22:57.209: E/AndroidRuntime(30555): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
10-14 21:22:57.209: E/AndroidRuntime(30555): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
10-14 21:22:57.209: E/AndroidRuntime(30555): at android.app.ActivityThread.access$800(ActivityThread.java)
10-14 21:22:57.209: E/AndroidRuntime(30555): at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
10-14 21:22:57.209: E/AndroidRuntime(30555): at android.os.Handler.dispatchMessage(Handler.java)
10-14 21:22:57.209: E/AndroidRuntime(30555): at android.os.Looper.loop(Looper.java)
10-14 21:22:57.209: E/AndroidRuntime(30555): at android.app.ActivityThread.main(ActivityThread.java)
10-14 21:22:57.209: E/AndroidRuntime(30555): at java.lang.reflect.Method.invokeNative(Native Method)
10-14 21:22:57.209: E/AndroidRuntime(30555): at java.lang.reflect.Method.invoke(Method.java:515)
10-14 21:22:57.209: E/AndroidRuntime(30555): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
10-14 21:22:57.209: E/AndroidRuntime(30555): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
10-14 21:22:57.209: E/AndroidRuntime(30555): at dalvik.system.NativeStart.main(Native Method)
10-14 21:22:57.209: E/AndroidRuntime(30555): Caused by: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
10-14 21:22:57.209: E/AndroidRuntime(30555): at android.widget.AdapterView.addView(AdapterView.java)
10-14 21:22:57.209: E/AndroidRuntime(30555): at android.view.LayoutInflater.inflate(LayoutInflater.java)
10-14 21:22:57.209: E/AndroidRuntime(30555): at android.view.LayoutInflater.inflate(LayoutInflater.java)
10-14 21:22:57.209: E/AndroidRuntime(30555): at android.view.LayoutInflater.inflate(LayoutInflater.java)
10-14 21:22:57.209: E/AndroidRuntime(30555): at com.inviscidlabs.schooled.ContainerFragmentCriteria.onCreateView(ContainerFragmentCriteria.java:28)
10-14 21:22:57.209: E/AndroidRuntime(30555): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
10-14 21:22:57.209: E/AndroidRuntime(30555): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
10-14 21:22:57.209: E/AndroidRuntime(30555): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
10-14 21:22:57.209: E/AndroidRuntime(30555): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
10-14 21:22:57.209: E/AndroidRuntime(30555): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
10-14 21:22:57.209: E/AndroidRuntime(30555): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:571)
10-14 21:22:57.209: E/AndroidRuntime(30555): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java)
以下是代码,从违规实体开始:
public class ContainerFragmentCriteria extends Fragment{
private FragmentManager fm;
private boolean insertMode;
//=====================ACTIVITY LIFECYCLE==============================
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.frag_container_criteria, container);
return v;
}
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//Instantiate Fragments, set Arguments
FragmentCriteriaEdit fCrE= new FragmentCriteriaEdit();
FragmentCriteriaList fCrL = new FragmentCriteriaList();
Bundle fCritListArguments = new Bundle();
fCritListArguments.putBoolean(CM.BKEY_INSERTMODE, insertMode);
//Begin the Transaction
fm=getChildFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fccr_rootLayout, fCrE, FragmentCriteriaEdit.sTag);
ft.add(R.id.fccr_rootLayout, fCrL, FragmentCriteriaList.sTag);
ft.commit();
}
@Override
public void onStart() {
super.onStart();
//Get our arguments
Bundle args = getArguments();
if(args!=null){
insertMode=args.getBoolean(CM.BKEY_INSERTMODE);
}
}
@Override
public void onDetach() {
super.onDetach();
try {
Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, null);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
AdapterView。请注意,在展开布局时,我将parent View参数设置为null:
public class ClassEditDrawerAdapter extends ArrayAdapter<ClassEditDrawerItem>{
Context ctx;
List<ClassEditDrawerItem> itemList;
int layoutResID;
public ClassEditDrawerAdapter(Context context, int layoutResourceID, List<ClassEditDrawerItem> items){
super(context, layoutResourceID, items);
ctx=context;
layoutResID=layoutResourceID;
itemList=items;
}
//An Effective ViewHolder, but for this Array!
private class DrawerItemHolderThing{
TextView itemName;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
DrawerItemHolderThing bobSagget;
View v = convertView;
if(v==null){
Log.d("adapter", "v was null");
LayoutInflater inflater = ((Activity) ctx).getLayoutInflater();
bobSagget= new DrawerItemHolderThing();
v=inflater.inflate(layoutResID, null, false);
bobSagget.itemName=((TextView) v.findViewById(R.id.item_basicItem));
v.setTag(bobSagget);
} else {
bobSagget = (DrawerItemHolderThing) v.getTag();
}
ClassEditDrawerItem drawerItem = (ClassEditDrawerItem) this.itemList.get(position);
if(bobSagget.itemName==null){Log.e("Adapter", "no TextView");}
bobSagget.itemName.setText(drawerItem.getItemName());
return v;
}
}
公共类ClassEditDrawerRadapter扩展了ArrayAdapter{
上下文ctx;
清单项目清单;
int layoutResID;
公共类EditDrawerRadapter(上下文上下文、内部布局资源ID、列表项){
超级(上下文、布局资源ID、项目);
ctx=上下文;
layoutResID=layoutResourceID;
项目列表=项目;
}
//一个有效的视图持有者,但是对于这个阵列!
私有类抽屉式文件夹{
TextView项目名称;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
付款人:Bobsaget Holderthing;
视图v=转换视图;
如果(v==null){
Log.d(“适配器”,“v为空”);
LayoutInflater充气器=((活动)ctx).getLayoutInflater();
BOBSAGET=新抽屉格式HolderThing();
v=充气机充气(layoutResID,null,false);
bobsagge.itemName=((TextView)v.findViewById(R.id.item_basicItem));
v、 setTag(BOBSAGET);
}否则{
bobSagget=(DrawerItemHolderThing)v.getTag();
}
ClassEditDrawerItem drawerItem=(ClassEditDrawerItem)this.itemList.get(位置);
如果(bobSagget.itemName==null){Log.e(“适配器”,“无文本视图”);}
bobsaget.itemName.setText(drawerItem.getItemName());
返回v;
}
}
活动:
public class ActivityClassEdit extends FragmentActivity{
//Variables
//Fragments
private ContainerFragmentCriteria frag_Criteria;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
//used as the effective ArrayAdapter for the NavigationDrawer
private ClassEditDrawerAdapter mNavAdapter;
private CharSequence sDrawerTitle;
private CharSequence sTitle;
//Serves as List of Strings to populate Nav Drawer's ListView of options
List<ClassEditDrawerItem> navOptions;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_classedit);
//Initialize List
navOptions= new ArrayList<ClassEditDrawerItem>();
sTitle=sDrawerTitle=getTitle();
mDrawerLayout = (DrawerLayout) findViewById(R.id.ace_drawer);
mDrawerList=(ListView) findViewById(R.id.ace_drawer_list);
navOptions.add(new ClassEditDrawerItem("Criteria", R.drawable.ic_launcher));
mNavAdapter = new ClassEditDrawerAdapter(this, R.layout.item_basic, navOptions);
mDrawerList.setAdapter(mNavAdapter);
//Set ListView onClickListener
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
//Set up the home button to open the nav drawer
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_launcher, R.string.hello_world,
R.string.name);
mDrawerLayout.setDrawerListener(mDrawerToggle);
//Select first item by default
if(savedInstanceState==null){
SelectItem(0);
}
}
@Override
public void setTitle(CharSequence title) {
sTitle = title;
getActionBar().setTitle(sTitle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return false;
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggles
mDrawerToggle.onConfigurationChanged(newConfig);
}
//What to do when Item is Selected
public void SelectItem(int position){
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
switch(position){
//Class Edit
case 0:
if(frag_Criteria==null){
frag_Criteria = new ContainerFragmentCriteria();
}
transaction.replace(R.id.ace_drawer_list, frag_Criteria);
transaction.commit();
break;
}
Toast.makeText(this, String.valueOf(position), Toast.LENGTH_LONG).show();
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
SelectItem(position);
}
}
}
公共类ActivityClassEdit扩展了FragmentActivity{
//变数
//碎片
私人集装箱破碎标准框架标准;
私人抽屉布局mDrawerLayout;
私有列表视图mDrawerList;
私有操作bardrawertoggle mDrawerToggle;
//用作导航抽屉的有效阵列适配器
私有类编辑器;
私钥;
私有轮滑;
//用作字符串列表,以填充Nav抽屉的选项列表视图
列出导航选项;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u classedit);
//初始化列表
navOptions=newarraylist();
sTitle=sDrawerTitle=getTitle();
mDrawerLayout=(抽屉布局)findViewById(R.id.ace\U抽屉);
mDrawerList=(ListView)findViewById(R.id.ace\u抽屉\u列表);
添加(新的ClassEditDrawerItem(“标准”,R.drawable.ic_启动器));
mNavAdapter=newclasseditDrawerRadapter(this,R.layout.item_basic,navOptions);
mDrawerList.setAdapter(mNavAdapter);
//设置ListView onClickListener
setOnItemClickListener(新的DrawerItemClickListener());
//设置主页按钮以打开导航抽屉
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle=新操作BarDrawerToggle(此,mDrawerLayout,
R.drawable.ic_启动器,R.string.hello_世界,
R.string.name);
mDrawerLayout.setDrawerListener(mDrawerToggle);
//默认情况下选择第一项
如果(savedInstanceState==null){
选择项目(0);
}
}
@凌驾
公共无效设置标题(字符序列标题){
针=标题;
getActionBar().setTitle(针);
}
@凌驾
后期创建时受保护的空(捆绑包savedInstanceState){
super.onPostCreate(savedInstanceState);
//在onRestoreInstanceState发生后同步切换状态。
mDrawerToggle.syncState();
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//操作栏home/up操作应打开或关闭抽屉。
//ActionBarDrawerToggle会处理好的。
如果(MDRAWERTOGLE.onOptionsItemSelected(项目)){
返回true;
}
返回false;
}
@凌驾
公共无效OnConfiguration已更改(配置newConfig){
super.onConfigurationChanged(newConfig);
//将任何配置更改传递给抽屉开关
mDrawerToggle.onConfigurationChanged(newConfig);
}
//选择项目时要执行的操作
public void SelectItem(内部位置){
FragmentTransaction=getSupportFragmentManager().beginTransaction();
开关(位置){
//类编辑
案例0:
if(frag_标准==null){
frag_Criteria=新的ContainerFragmentCriteria();
}
交易。替换(R.id.ace\U出票人清单、frag\U标准);
commit();
打破
}
Toast.makeText(this,String.valueOf(position),Toast.LENGTH_LONG.show();
}
私有类DrawerItemClickListener实现ListView.OnItemClickListener{
@凌驾
public void onItemClick(AdapterView父视图、视图、整型位置、,
长id){
选择项目(位置);
}
}
}
最后,XMLs:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fccr_rootLayout"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout android:id="@+id/item_ace_itemlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:orientation="vertical"
android:background="?android:attr/activatedBackgroundIndicator"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="55dp"
>
<ImageView
android:id="@+id/drawer_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/drawer_itemName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="1dp"
android:layout_marginTop="1dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#DADADC"
></View>
</LinearLayout>
</RelativeLayout>
以及活动的XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/ace_drawer" >
<FrameLayout android:id="@+id/ace_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<ListView
android:id="@+id/ace_drawer_list"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
/>
</android.support.v4.widget.DrawerLayout>
M
View v = inflater.inflate(R.layout.frag_container_criteria, container, false);// false to not add the inflated layout to container