ListFragment 指定的child已经有一个parent
ListFragment The specified child already has a parent
在我的应用程序中,我有一个带 ViewPagerAdapter 的选项卡式布局(4 个选项卡),用于从一个选项卡切换到另一个选项卡,在我实现 ListFragment 的所有选项卡中,因为我需要从 SQLite 加载数据并显示行.行已加载,但当我尝试切换到另一个选项卡时,应用程序崩溃并显示以下消息:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
所以我搜索了这个错误,并找到了如下解决方案:
v = inflater.inflate(R.layout.tab_1_list, container, false);
v = inflater.inflate(R.layout.tab_1_list, null);
但是不行,错误还是一样
ViewPagerAdapter
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.content.Context;
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
CharSequence Titles[]; // This will Store the Titles of the Tabs which are Going to be passed when ViewPagerAdapter is created
int NumbOfTabs; // Store the number of tabs, this will also be passed when the ViewPagerAdapter is created
private Context context;
// Build a Constructor and assign the passed Values to appropriate values in the class
public ViewPagerAdapter(FragmentManager fm,CharSequence mTitles[], int mNumbOfTabsumb) {
super(fm);
this.Titles = mTitles;
this.NumbOfTabs = mNumbOfTabsumb;
}
//This method return the fragment for the every position in the View Pager
@Override
public Fragment getItem(int position) {
if(position == 0) // if the position is 0 we are returning the First tab
{
Tab1 tab1 = new Tab1();
return tab1;
} else if(position == 1){
Tab2 tab2 = new Tab2();
return tab2;
} else if(position == 2) {
Tab3 tab3 = new Tab3();
return tab3;
} else if(position == 3) {
Tab4 tab4 = new Tab4();
return tab4;
} else {
Tab1 tab1 = new Tab1();
return tab1;
}
}
// This method return the titles for the Tabs in the Tab Strip
@Override
public CharSequence getPageTitle(int position) {
return Titles[position];
}
// This method return the Number of tabs for the tabs Strip
@Override
public int getCount() {
return NumbOfTabs;
}
}
Tab1.java
public class Tab1 extends ListFragment
{
public View v;
public String title;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.tab_1_list, container, false);
//database handler
DatabaseNumbersHandler db = new DatabaseNumbersHandler(getContext());
///get titles
ArrayList list = db.getTitles();
// Get a handle to the list view
ListView lv = (ListView) v.findViewById(android.R.id.list);
// Convert ArrayList to array
String[] lv_arr = (String[]) list.toArray(new String[list.size()]);
lv.setAdapter(new ArrayAdapter(getContext(), android.R.layout.simple_list_item_1, lv_arr));
return lv;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//
// insert code here to setup and call setListAdapter()
//
//database handler
}
@Override
public void onListItemClick(ListView lv, View v, int position, long id)
{
}
}
tab_1_list.xml
<?xml version="1.0" encoding="utf-8"?>
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.tyr.MainSettings">
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</RelativeLayout>
您返回 ListView
而不是 View
替换
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.tab_1_list, container, false);
//database handler
DatabaseNumbersHandler db = new DatabaseNumbersHandler(getContext());
///get titles
ArrayList list = db.getTitles();
// Get a handle to the list view
ListView lv = (ListView) v.findViewById(android.R.id.list);
// Convert ArrayList to array
String[] lv_arr = (String[]) list.toArray(new String[list.size()]);
lv.setAdapter(new ArrayAdapter(getContext(), android.R.layout.simple_list_item_1, lv_arr));
return v;
}
在我的应用程序中,我有一个带 ViewPagerAdapter 的选项卡式布局(4 个选项卡),用于从一个选项卡切换到另一个选项卡,在我实现 ListFragment 的所有选项卡中,因为我需要从 SQLite 加载数据并显示行.行已加载,但当我尝试切换到另一个选项卡时,应用程序崩溃并显示以下消息:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
所以我搜索了这个错误,并找到了如下解决方案:
v = inflater.inflate(R.layout.tab_1_list, container, false);
v = inflater.inflate(R.layout.tab_1_list, null);
但是不行,错误还是一样
ViewPagerAdapter
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.content.Context;
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
CharSequence Titles[]; // This will Store the Titles of the Tabs which are Going to be passed when ViewPagerAdapter is created
int NumbOfTabs; // Store the number of tabs, this will also be passed when the ViewPagerAdapter is created
private Context context;
// Build a Constructor and assign the passed Values to appropriate values in the class
public ViewPagerAdapter(FragmentManager fm,CharSequence mTitles[], int mNumbOfTabsumb) {
super(fm);
this.Titles = mTitles;
this.NumbOfTabs = mNumbOfTabsumb;
}
//This method return the fragment for the every position in the View Pager
@Override
public Fragment getItem(int position) {
if(position == 0) // if the position is 0 we are returning the First tab
{
Tab1 tab1 = new Tab1();
return tab1;
} else if(position == 1){
Tab2 tab2 = new Tab2();
return tab2;
} else if(position == 2) {
Tab3 tab3 = new Tab3();
return tab3;
} else if(position == 3) {
Tab4 tab4 = new Tab4();
return tab4;
} else {
Tab1 tab1 = new Tab1();
return tab1;
}
}
// This method return the titles for the Tabs in the Tab Strip
@Override
public CharSequence getPageTitle(int position) {
return Titles[position];
}
// This method return the Number of tabs for the tabs Strip
@Override
public int getCount() {
return NumbOfTabs;
}
}
Tab1.java
public class Tab1 extends ListFragment
{
public View v;
public String title;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.tab_1_list, container, false);
//database handler
DatabaseNumbersHandler db = new DatabaseNumbersHandler(getContext());
///get titles
ArrayList list = db.getTitles();
// Get a handle to the list view
ListView lv = (ListView) v.findViewById(android.R.id.list);
// Convert ArrayList to array
String[] lv_arr = (String[]) list.toArray(new String[list.size()]);
lv.setAdapter(new ArrayAdapter(getContext(), android.R.layout.simple_list_item_1, lv_arr));
return lv;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//
// insert code here to setup and call setListAdapter()
//
//database handler
}
@Override
public void onListItemClick(ListView lv, View v, int position, long id)
{
}
}
tab_1_list.xml
<?xml version="1.0" encoding="utf-8"?>
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.tyr.MainSettings">
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</RelativeLayout>
您返回 ListView
而不是 View
替换
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.tab_1_list, container, false);
//database handler
DatabaseNumbersHandler db = new DatabaseNumbersHandler(getContext());
///get titles
ArrayList list = db.getTitles();
// Get a handle to the list view
ListView lv = (ListView) v.findViewById(android.R.id.list);
// Convert ArrayList to array
String[] lv_arr = (String[]) list.toArray(new String[list.size()]);
lv.setAdapter(new ArrayAdapter(getContext(), android.R.layout.simple_list_item_1, lv_arr));
return v;
}