指定的 child 已经有一个 parent。您必须先在 child 的 parent 上调用 removeView()。 (带工具栏)
The specified child already has a parent. You must call removeView() on the child's parent first. (with tool bar)
我正在尝试将带有自定义适配器的微调器添加到工具栏,但遇到此错误:
指定的 child 已经有一个 parent。您必须先在 child 的 parent 上调用 removeView()。
这是我的微调适配器 class:
public class SpinnerAdapter extends BaseAdapter {
ArrayList<ShopCartModel> list;
Context context;
LayoutInflater inflater;
public SpinnerAdapter (Context c , ArrayList<ShopCartModel> list){
this.context = c;
this.list = list;
inflater = (LayoutInflater.from(c));
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = inflater.inflate(R.layout.custom_spinner_item , null);
TextView name = (TextView)convertView.findViewById(R.id.textView);
ShopCartModel tmp = list.get(position);
name.setText(tmp.getName());
return convertView;
}
}
这是我的主要 class,当我尝试将微调器添加到工具栏时:
public class ShopCartScreen extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
Spinner spin;
ArrayList<ShopCartModel> shopCarts;
SpinnerAdapter spinnerAdapter;
DBHelper db;
ApartmentModel apartment;
GetShopLists g;
SharedPreferences preferences;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shop_carts_list);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
items = (ListView)findViewById(R.id.shop_carts_lists);
spin = (Spinner)findViewById(R.id.spinner);
db = DBHelper.getInstance(this);
preferences = getSharedPreferences("appData", 0);
boolean flag = preferences.getBoolean("ShopCartListsLoadedFromDB" , false); //
int apartmentNumber = preferences.getInt("apartmentNumber" , 0);
apartment = new ApartmentModel(apartmentNumber);
if(flag == true){
shopCarts = db.getshopLists(apartment);
spinnerAdapter = new SpinnerAdapter(getApplicationContext(),shopCarts);
spin.setAdapter(spinnerAdapter);
}
else{
g = new GetShopLists(this, shopCarts , spin , spinnerAdapter);
g.execute("this is where i set the items on the spinner");
preferences = getSharedPreferences("appData", 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("ShopCartListsLoadedFromDB" , true);
editor.apply();
}
toolbar.addView(spin, 0); //this is where the application crushes
spin.setOnItemSelectedListener(this);
}
编辑:我的主要 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<!--list of all shop carts of an apartment-->
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:background="#ff1e8622"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.Toolbar>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:weightSum="1"
android:id="@+id/newList"
android:baselineAligned="false">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/newList"
android:id="@+id/textView"
android:layout_weight="0.12"
android:layout_gravity="center"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:src="@android:drawable/ic_input_add"
android:background="#ffffff"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_gravity="center"
android:onClick="createNewList"
/>
</LinearLayout>
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/shop_carts_lists"
android:layout_gravity="center_horizontal"
android:layout_below="@id/newList"
android:paddingTop="10dp"/>
</LinearLayout>
和我的旋转器项目 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="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="@dimen/activity_horizontal_margin"
android:text="Demo"
android:textColor="#000" />
</LinearLayout>
因为某些布局中已经存在微调器。你可以通过两种类型来完成。
删除 toolbar.addView(spin, 0);
行。
或
将微调器初始化更改为..
旋转器旋转 = 新旋转器(YourACtivity.this);
并将其从布局中删除 xml。
我正在尝试将带有自定义适配器的微调器添加到工具栏,但遇到此错误: 指定的 child 已经有一个 parent。您必须先在 child 的 parent 上调用 removeView()。 这是我的微调适配器 class:
public class SpinnerAdapter extends BaseAdapter {
ArrayList<ShopCartModel> list;
Context context;
LayoutInflater inflater;
public SpinnerAdapter (Context c , ArrayList<ShopCartModel> list){
this.context = c;
this.list = list;
inflater = (LayoutInflater.from(c));
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = inflater.inflate(R.layout.custom_spinner_item , null);
TextView name = (TextView)convertView.findViewById(R.id.textView);
ShopCartModel tmp = list.get(position);
name.setText(tmp.getName());
return convertView;
}
}
这是我的主要 class,当我尝试将微调器添加到工具栏时:
public class ShopCartScreen extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
Spinner spin;
ArrayList<ShopCartModel> shopCarts;
SpinnerAdapter spinnerAdapter;
DBHelper db;
ApartmentModel apartment;
GetShopLists g;
SharedPreferences preferences;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shop_carts_list);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
items = (ListView)findViewById(R.id.shop_carts_lists);
spin = (Spinner)findViewById(R.id.spinner);
db = DBHelper.getInstance(this);
preferences = getSharedPreferences("appData", 0);
boolean flag = preferences.getBoolean("ShopCartListsLoadedFromDB" , false); //
int apartmentNumber = preferences.getInt("apartmentNumber" , 0);
apartment = new ApartmentModel(apartmentNumber);
if(flag == true){
shopCarts = db.getshopLists(apartment);
spinnerAdapter = new SpinnerAdapter(getApplicationContext(),shopCarts);
spin.setAdapter(spinnerAdapter);
}
else{
g = new GetShopLists(this, shopCarts , spin , spinnerAdapter);
g.execute("this is where i set the items on the spinner");
preferences = getSharedPreferences("appData", 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("ShopCartListsLoadedFromDB" , true);
editor.apply();
}
toolbar.addView(spin, 0); //this is where the application crushes
spin.setOnItemSelectedListener(this);
}
编辑:我的主要 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<!--list of all shop carts of an apartment-->
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:background="#ff1e8622"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.Toolbar>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:weightSum="1"
android:id="@+id/newList"
android:baselineAligned="false">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/newList"
android:id="@+id/textView"
android:layout_weight="0.12"
android:layout_gravity="center"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:src="@android:drawable/ic_input_add"
android:background="#ffffff"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_gravity="center"
android:onClick="createNewList"
/>
</LinearLayout>
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/shop_carts_lists"
android:layout_gravity="center_horizontal"
android:layout_below="@id/newList"
android:paddingTop="10dp"/>
</LinearLayout>
和我的旋转器项目 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="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="@dimen/activity_horizontal_margin"
android:text="Demo"
android:textColor="#000" />
</LinearLayout>
因为某些布局中已经存在微调器。你可以通过两种类型来完成。
删除
toolbar.addView(spin, 0);
行。或
将微调器初始化更改为..
旋转器旋转 = 新旋转器(YourACtivity.this);
并将其从布局中删除 xml。