Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取了java.lang.IllegalStateException:指定的子级已具有父级_Java_Android_Sqlite - Fatal编程技术网

获取了java.lang.IllegalStateException:指定的子级已具有父级

获取了java.lang.IllegalStateException:指定的子级已具有父级,java,android,sqlite,Java,Android,Sqlite,我添加了两个线性布局,并在这些布局上动态添加复选框。最终目的是将数据存储在数据库中 我得到这个错误: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.freedomkitchen.sonali.freedomkitchen/com.freedomkitchen.sonali.freedomkitchenAndroidApp.AddRecipes}: java.lang.IllegalStateExcep

我添加了两个
线性布局
,并在这些布局上动态添加
复选框
。最终目的是将数据存储在数据库中

我得到这个错误:

java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.freedomkitchen.sonali.freedomkitchen/com.freedomkitchen.sonali.freedomkitchenAndroidApp.AddRecipes}:
java.lang.IllegalStateException: The specified child already has a
parent. You must call removeView() on the child's parent first.
你能帮我找到问题吗?谢谢

这是我的代码:

public class AddRecipes extends AppCompatActivity {
    static int a=0,b=0,c=0,d=0;
    static int id=0;
    public int x;
    int idvalueVeg;
    int idvalueFruits;
    int idvalueGrains;
    int idvalueDairy;
    int idvalueSeaFood;
    int end_of_sup_ing;
    int sup_ing_id;
    ArrayList<String> Main_Ingredients;
    ArrayList<String> Supporting_Ingredients;
    LinearLayout ll;
    LinearLayout ll2;
    Spinner FoodCatValue;
    public String MealSelected;
    EditText RecipeNameValue;
    public   Spinner sItems;


    private static final boolean AUTO_HIDE = true;


    private static final int AUTO_HIDE_DELAY_MILLIS = 3000;


    private static final int UI_ANIMATION_DELAY = 300;
    private final Handler mHideHandler = new Handler();
    private View mContentView;
    private final Runnable mHidePart2Runnable = new Runnable() {
        @SuppressLint("InlinedApi")
        @Override
        public void run() {

            mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        }
    };
    private View mControlsView;
    private final Runnable mShowPart2Runnable = new Runnable() {
        @Override
        public void run() {
            // Delayed display of UI elements
            ActionBar actionBar = getSupportActionBar();
            if (actionBar != null) {
                actionBar.show();
            }
            mControlsView.setVisibility(View.VISIBLE);
        }
    };
    private boolean mVisible;
    private final Runnable mHideRunnable = new Runnable() {
        @Override
        public void run() {
            hide();
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addrecipes);

         ll = (LinearLayout) findViewById(R.id.chk_layout);
        ll2 = (LinearLayout) findViewById(R.id.chk_layout2);
        FoodCatValue = (Spinner)findViewById(R.id.spinnerFoodCat);



        Resources res = getResources();
        String[] Appetizers  = res.getStringArray(R.array.Appetizer_Meals);
        String[] Main_Course  = res.getStringArray(R.array.Main_Course_Meals);
        String[] Desserts  = res.getStringArray(R.array.Desserts_Meals);

        ArrayList<String> AppetizerList = new ArrayList<String>(Arrays.asList(Appetizers));
        ArrayList<String> Main_CourseList = new ArrayList<String>(Arrays.asList(Main_Course));
        ArrayList<String> DessertList = new ArrayList<String>(Arrays.asList(Desserts));

        if(FoodCatValue.getSelectedItem().toString().equals("Appetizers")){
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                    this, android.R.layout.simple_spinner_item, AppetizerList);

            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            sItems = (Spinner) findViewById(R.id.spinnerMealCat);
            sItems.setAdapter(adapter);
            getMealSelectedVal();
        }

        else if(FoodCatValue.getSelectedItem().toString().equals("Main Course"))

            {
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                        this, android.R.layout.simple_spinner_item, Main_CourseList);

                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                sItems = (Spinner) findViewById(R.id.spinnerMealCat);
                sItems.setAdapter(adapter);
                getMealSelectedVal();

            }
        else if(FoodCatValue.getSelectedItem().toString().equals("Dessert")){

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                    this, android.R.layout.simple_spinner_item, DessertList);

            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            sItems = (Spinner) findViewById(R.id.spinnerMealCat);
            sItems.setAdapter(adapter);
            getMealSelectedVal();

        }


                RecipeNameValue =(EditText)findViewById(R.id.RecipeName);

                DB_Access mydb=new DB_Access(this);
                ArrayList<String> Vegetables=mydb.getIngredients(1);
                ArrayList<String> Fruits=mydb.getIngredients(2);
                ArrayList<String> Dairy=mydb.getIngredients(3);
                ArrayList<String> Grains=mydb.getIngredients(4);
                ArrayList<String> Seafood=mydb.getIngredients(5);
                ArrayList<String> Sup_Ing=mydb.getSupportingIngredients();
                final TextView VegTv = new TextView(this);
                VegTv.setText("Vegetables");
                ll.addView(VegTv);
                for (x=0;x<Vegetables.size(); x++,id++) {
                    final CheckBox checkbox = new CheckBox(this);
                    checkbox.setId(id);
                    String Item = Vegetables.get(x);
                    checkbox.setText(Item);
                    ll.addView(checkbox);
                    idvalueVeg=id;
                }
                TextView FruitsTv = new TextView(this);
                FruitsTv.setText("Fruits");
                ll.addView(FruitsTv);



                for (x=0;x<Fruits.size(); x++,id++) {
                    CheckBox checkbox = new CheckBox(this);
                    checkbox.setId(id);
                    String Item = Fruits.get(x);
                    checkbox.setText(Item);
                    ll.addView(checkbox);
                    idvalueFruits=id;

                }
                TextView GrainsTv = new TextView(this);
                GrainsTv.setText("Grains");
                ll.addView(GrainsTv);

                for (x=0;x<Grains.size(); x++,id++) {
                    CheckBox checkbox = new CheckBox(this);
                    checkbox.setId(id);
                    String Item = Grains.get(x);
                    checkbox.setText(Item);
                    ll.addView(checkbox);
                    idvalueGrains=id;


                }
                TextView DairyTv = new TextView(this);
                DairyTv.setText("Dairy");
                ll.addView(DairyTv);

                for(x=0;x<Dairy.size(); x++,id++) {
                    CheckBox checkbox = new CheckBox(this);
                    checkbox.setId(id);
                    String Item = Dairy.get(x);
                    checkbox.setText(Item);
                    ll.addView(checkbox);
                    idvalueDairy=id;

                }
                TextView SeafoodTv = new TextView(this);
                SeafoodTv.setText("Seafood");
                ll.addView(DairyTv);
                for(x=0;x<Seafood.size(); x++,id++) {
                    CheckBox checkbox = new CheckBox(this);
                    checkbox.setId(id);
                    String Item = Seafood.get(x);
                    checkbox.setText(Item);
                    ll2.addView(checkbox);
                    idvalueSeaFood=id;

                }
             TextView supIng = (TextView)findViewById(R.id.sup_ingTextView);
                DairyTv.setText("Supporting Ingredients");
                 ll.addView(supIng);
                sup_ing_id = idvalueSeaFood;

                for(x=0;x<Sup_Ing.size(); x++,id++) {
                    CheckBox checkbox = new CheckBox(this);
                    checkbox.setId(id);
                    String Item = Sup_Ing.get(x);
                    checkbox.setText(Item);
                    ll.addView(checkbox);
                    end_of_sup_ing=id;
                }

            }

    public void getMealSelectedVal(){
        MealSelected= sItems.getSelectedItem().toString();

    }

    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);


    }


    private void hide() {
        // Hide UI first
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        mControlsView.setVisibility(View.GONE);
        mVisible = false;

        // Schedule a runnable to remove the status and navigation bar after a delay
        mHideHandler.removeCallbacks(mShowPart2Runnable);
        mHideHandler.postDelayed(mHidePart2Runnable, UI_ANIMATION_DELAY);
    }

    @SuppressLint("InlinedApi")
    private void show() {
        // Show the system bar
        mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
        mVisible = true;

        // Schedule a runnable to display UI elements after a delay
        mHideHandler.removeCallbacks(mHidePart2Runnable);
        mHideHandler.postDelayed(mShowPart2Runnable, UI_ANIMATION_DELAY);
    }


    public void btnAddRecipes(View v){
        GetInfo();

        DB_Access mydb=new DB_Access(this);
        EditText ins =(EditText)findViewById(R.id.RecipeTextArea);

        mydb.adduserRecipes(FoodCatValue.getSelectedItem().toString(),MealSelected,RecipeNameValue.getText().toString(), Main_Ingredients,Supporting_Ingredients,ins.getText().toString());


    }
    public void GetInfo(){
        Main_Ingredients=new ArrayList<String>(200);
        Supporting_Ingredients=new ArrayList<String>(200);
        for(x=0;x<id;x++){
            CheckBox cb=(CheckBox)findViewById(x);
            if(cb.isChecked()){
                   {
                       String item=cb.getText().toString();
                       Main_Ingredients.add(item);


                }

            }


        }
        //disp main ing
        for(int i=0;i<Main_Ingredients.size();i++){

            Log.i("Main_Ing:",Main_Ingredients.get(i));
        }


        for(x=sup_ing_id;x<=end_of_sup_ing;x++){
            CheckBox cb=(CheckBox)findViewById(x);
            if(cb.isChecked()){
                {
                    String item=cb.getText().toString();
                    Supporting_Ingredients.add(item);


                }

            }

        }
        for(int i=0;i<Supporting_Ingredients.size();i++){

            Log.i("Sup_Ing:",Supporting_Ingredients.get(i));
        }

    }
 }
public类AddRecipes扩展了AppCompative活动{
静态整数a=0,b=0,c=0,d=0;
静态int id=0;
公共int x;
int-idvalueVeg;
int-idvalues;
int-idvalueGrains;
国际乳业;
国际海鲜;
供应的内端;
内部监督id;
ArrayList主要成分;
ArrayList支持_成分;
线性布局;
线性布局ll2;
微调器FoodCatValue;
已选择公共字符串;
EditText RecipeName评估值;
公共微调器站点;
私有静态最终布尔自动隐藏=真;
专用静态最终int自动隐藏延迟=3000;
私有静态最终int UI_动画_延迟=300;
私有最终处理程序mHideHandler=新处理程序();
私有视图mContentView;
私有最终可运行mHidePart2Runnable=新可运行(){
@SuppressLint(“InlinedApi”)
@凌驾
公开募捐{
mContentView.SetSystemMivibility(View.SYSTEM\u UI\u FLAG\u LOW\u PROFILE
|View.SYSTEM\u UI\u FLAG\u全屏显示
|View.SYSTEM\u UI\u FLAG\u布局\u稳定
|View.SYSTEM\u UI\u FLAG\u沉浸式\u粘性
|View.SYSTEM\u UI\u FLAG\u布局\u隐藏\u导航
|视图。系统(用户界面、标志、隐藏、导航);
}
};
私有视图mControlsView;
private final Runnable mShowPart2Runnable=new Runnable(){
@凌驾
公开募捐{
//UI元素的延迟显示
ActionBar ActionBar=getSupportActionBar();
if(actionBar!=null){
actionBar.show();
}
mControlsView.setVisibility(View.VISIBLE);
}
};
私有布尔mVisible;
private final Runnable mHideRunnable=新Runnable(){
@凌驾
公开募捐{
隐藏();
}
};
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.addrecipes);
ll=(线性布局)findViewById(R.id.chk_布局);
ll2=(线性布局)findViewById(R.id.chk_布局2);
FoodCatValue=(微调器)findViewById(R.id.spinnerFoodCat);
Resources res=getResources();
String[]开胃菜=res.getStringArray(R.array.apperizer\u餐点);
String[]Main\u Course=res.getStringArray(R.array.Main\u Course\u);
String[]Desserts=res.getStringArray(R.array.Desserts\u餐点);
ArrayList开胃菜列表=新的ArrayList(Arrays.asList(开胃菜));
ArrayList Main_CourseList=新的ArrayList(Arrays.asList(Main_CourseList));
ArrayList DessertList=新的ArrayList(Arrays.asList(Desserts));
if(FoodCatValue.getSelectedItem().toString().equals(“开胃菜”)){
ArrayAdapter适配器=新的ArrayAdapter(
这个,android.R.layout.simple_spinner_item,开胃菜列表);
setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
sItems=(微调器)findViewById(R.id.spinnerMealCat);
setAdapter(适配器);
getMealSelectedVal();
}
else if(FoodCatValue.getSelectedItem().toString().equals(“主菜”))
{
ArrayAdapter适配器=新的ArrayAdapter(
这个,android.R.layout.simple\u spinner\u item,Main\u CourseList);
setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
sItems=(微调器)findViewById(R.id.spinnerMealCat);
setAdapter(适配器);
getMealSelectedVal();
}
else if(FoodCatValue.getSelectedItem().toString().equals(“甜点”)){
ArrayAdapter适配器=新的ArrayAdapter(
这个,android.R.layout.simple\u spinner\u项目,甜点列表);
setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
sItems=(微调器)findViewById(R.id.spinnerMealCat);
setAdapter(适配器);
getMealSelectedVal();
}
RecipeNameValue=(EditText)findViewById(R.id.RecipeName);
DB_访问mydb=新的DB_访问(本);
ArrayList蔬菜=mydb.GetComponents(1);
ArrayList水果=mydb.GetComponents(2);
ArrayList Dairy=mydb.GetComponents(3);
ArrayList Grains=mydb.GetComponents(4);
ArrayList海鲜=mydb.GetComponents(5);
ArrayList Sup_Ing=mydb.getsupportinggreedients();
最终文本视图VegTv=新文本视图(本);
VegTv.setText(“蔬菜”);
ll.addView(VegTv);

对于(x=0;x此异常被激发,然后您尝试向布局添加一些视图两次

您复制粘贴了代码,但未在此处更改

TextView SeafoodTv = new TextView(this);
SeafoodTv.setText("Seafood");
ll.addView(DairyTv);
我相信应该是这样

TextView SeafoodTv = new TextView(this);
SeafoodTv.setText("Seafood");
ll.addView(SeafoodTv);

为避免此类错误,最好将静态视图放在xml布局中。

问题仍然存在。@Sonaliset您可以在logcat的stacktrace outlput中找到导致异常的确切行号。我想您还有一些类似的打字错误
TextView SeafoodTv = new TextView(this);
SeafoodTv.setText("Seafood");
ll.addView(SeafoodTv);