私はこのスピナーをアクティビティのツールバーに入れる必要があるカスタムスピナーを作成しました。これが私の活動原因:java.lang.IllegalStateException:指定された子がすでに親を持っています
package abtech.waiteriano.com.waitrer;
import android.app.FragmentManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.SortedMap;
import abtech.waiteriano.com.waitrer.connection_class.ConnectionClass;
import abtech.waiteriano.com.waitrer.fragments.LVMenuFragment;
import abtech.waiteriano.com.waitrer.fragments.MenuGridFragment;
import abtech.waiteriano.com.waitrer.fragments.TablesFragment;
public class MenuActivity extends AppCompatActivity {
private android.support.v7.widget.Toolbar toolbar;
private ArrayList<String> Prests_ID;
private ArrayList<String> Prests_Name;
public static String Prst_ID = "0";
public static long Check_ID = 0;
public static int Check_Serial = 0;
ImageView listIcon, gridIcon;
boolean flag_item_grid = true;
RelativeLayout fire, showOrderCart;
String[] Menu_List = {"Senegal", "Afrique du Sud", "Kenya", "Ghana",
"Liberia", "Burkina Fasso", "Nigeria", "Cameroon", "Uganda",
"Tanzania", "Congo", "Malawi", "Zambia"};
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
Spinner menu_spinner = (Spinner) findViewById(R.id.spinner);
menu_spinner.setAdapter(new MyCustomAdapter(MenuActivity.this,
R.layout.spinner_row, Menu_List));
menu_spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
listIcon = (ImageView) findViewById(R.id.listicon);
gridIcon = (ImageView) findViewById(R.id.gridicon);
fire = (RelativeLayout) findViewById(R.id.fireID);
showOrderCart = (RelativeLayout) findViewById(R.id.cartID);
Prests_ID = new ArrayList<String>();
Prests_Name = new ArrayList<String>();
Prests_ID.add("-1");
Prests_Name.add("ALL");
Prst_ID = ConnectionClass.Ret_Col("Select MainCourse From OutLet_Setup Where OutLet_ID = " + ConnectionClass.OutletID);
if (Prst_ID.trim() == "")
Prst_ID = "0";
String str = "Select ID,Code,Name,Name2 From Presets Where Active = 1 And Rest_ID_Active = 1 AND OutLet_ID_Active = 1 ORDER BY Code";
ResultSet rs = ConnectionClass.Ret_RS(str);
try {
while (rs.next()) {
Prests_ID.add(rs.getString("ID"));
Prests_Name.add(rs.getString("Name"));
}
} catch (SQLException e) {
e.printStackTrace();
}
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
Spinner navigationSpinner = new Spinner(getSupportActionBar().getThemedContext());
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, Prests_Name);
navigationSpinner.setAdapter(adapter);
toolbar.addView(menu_spinner, 0);
int indx = -1;
indx = Prests_ID.indexOf(Prst_ID);
navigationSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Prst_ID = Prests_ID.get(position);
Toast.makeText(MenuActivity.this,
"you selected: " + Prests_ID.get(position) + " " + Prests_Name.get(position),
Toast.LENGTH_SHORT).show();
if (flag_item_grid) {
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, new MenuGridFragment()).commit();
} else {
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, new LVMenuFragment()).commit();
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
if (indx != -1)
navigationSpinner.setSelection(indx);
fire.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int i = 0;
for (String key : TablesFragment.Check_Items.keySet()) {
SortedMap<String, String> sm1 = TablesFragment.Check_Items.get(key);
int qty = Integer.parseInt(sm1.get("Qty"));
if (qty > 0) {
i++;
double Price = Double.parseDouble(sm1.get("Price"));
Price = qty * Price;
String st = "insert into ChecksItems (Check_ID,Item_ID,QTY,UnitPrice,TotalPrice,DicountValue,Tax_Value,Adj_Value,NetPrice,Serial,Fired,Fired_Time,Voided,Voided_Time,Voided_Reason,P_On_Check,Complement,Status,IsModifier,Ref_Mod_Item,IsAssimbly,Ref_Ass_Item,Taxable,NoServiceCharge,Num_Fired,Num_Print,Server_ID,P_On_Report,Check_ID_Combine,Round_Check_Fired,Void_Effect_Invn,Promo_ID,Orig_Price,Officer,Comp_Reason_ID,End_Serial_Count,Discount_ID,Disc_Reason_ID,Hold,Hold_Time,Voided_By,Comp_By,Disc_By) values (" + String.valueOf(MenuActivity.Check_ID) + "," + sm1.get("Item_ID").toString() + ",1," + sm1.get("Price").toString() + "," + Price + ",0,0,0," + Price + "," + String.valueOf(i) + ",0,GetDate(),0,GetDate(),0,'" + sm1.get("PrintOnChick").toString() + "',0,'New',0,0,0,0,'" + sm1.get("Taxable").toString() + "','" + sm1.get("NoServiceCharge").toString() + "',0,0,0,'" + sm1.get("PrintOnReport").toString() + "',0,1,0,0," + sm1.get("Price").toString() + ",0,0,0,0,0,0,GetDate(),0,0,0)";
ConnectionClass.executeUpdate(st);
}
}
Toast.makeText(MenuActivity.this, "Fired", Toast.LENGTH_SHORT).show();
}
});
showOrderCart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MenuActivity.this, ShowOrderActivity.class);
startActivity(i);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.iconmenu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(android.view.MenuItem item) {
switch (item.getItemId()) {
case R.id.searchicon:
// ...
return true;
case R.id.listicon:
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, new LVMenuFragment()).commit();
flag_item_grid = false;
return true;
case R.id.gridicon:
// ...
fm = getFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, new MenuGridFragment()).commit();
flag_item_grid = true;
return true;
}
return super.onOptionsItemSelected(item);
}
public class MyCustomAdapter extends ArrayAdapter<String> {
public MyCustomAdapter(Context context, int textViewResourceId,
String[] objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
}
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
// return super.getView(position, convertView, parent);
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.spinner_row, parent, false);
TextView label = (TextView) row.findViewById(R.id.menu_items);
label.setText(Menu_List[position]);
return row;
}
}
public class MyOnItemSelectedListener implements AdapterView.OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
Toast.makeText(
parent.getContext(),
"The country is "
+ parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
}
であり、これは私のxmlファイルであるとして、これは
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context="abtech.waiteriano.com.waitrer.MenuActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize" />
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:prompt="@string/ru_prompt" />
<!-- <FrameLayout
android:id="@+id/content_frame"
android:layout_width="matc
h_parent"
android:layout_height="wrap_content"></FrameLayout>-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:id="@+id/rl1"
android:orientation="horizontal"
android:weightSum="2">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottom_panel">
</FrameLayout>
<LinearLayout
android:id="@+id/bottom_panel"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="40dp"
android:weightSum="2"
android:layout_alignParentBottom="true">
<RelativeLayout
android:id="@+id/fireID"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_weight="1"
android:background="#ff6b6b"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:src="@drawable/fire" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/cartID"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_weight="1"
android:background="#6bfff3"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true">
<ImageView
android:id="@+id/imageCart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:src="@drawable/shoppingcart" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
このエラー
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
を得ましたあなたのmenu_spinnerはすでにあなたのxmlファイルで宣言されている親ビュー(LinearLayout
)が
<?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">
<TextView
android:id="@+id/menu_items"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_marginBottom="10dip"
android:layout_marginTop="10dip"
android:textColor="#000000" />
</LinearLayout>
[java.lang.IllegalStateExceptionの可能な複製:指定された子がすでに親を持っている](http://stackoverflow.com/questions/10007094/java-lang-illegalstateexception-the-specified-child-already-has- a-parent) –