2016-04-25 11 views
-1

2つを追加しましたLinearLayoutsこれらのレイアウトでは、動的にCheckboxesを追加します。最終的な目的は、データをデータベースに格納することです。java.lang.IllegalStateExceptionを取得しました:指定された子が既に親を持っています

私はこのエラーを取得しています:

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)); 
     } 

    } 
} 

XML

<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:background="#cdd2ea" 
    android:id="@+id/RL" 
    android:orientation="vertical" 
    android:weightSum="1"> 


    <ScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:id="@+id/sv" 
     android:fillViewport="true" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentStart="true" 
     android:layout_above="@+id/buttonAddRecipes"> 



    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:id="@+id/l_layout" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentStart="true"> 


     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:text="Add a New Recipe" 
      android:id="@+id/textView" 
      android:layout_alignParentTop="true" 
      android:layout_centerHorizontal="true" 
      android:layout_marginTop="60dp" 
      android:layout_gravity="center_horizontal" /> 


     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:text="Food Category :" 
      android:id="@+id/textView11" 
      android:layout_marginTop="25dp" 
      android:layout_below="@+id/textView" 
      android:layout_alignParentStart="true" 
      android:layout_marginStart="47dp" /> 

     <Spinner 
      android:layout_width="244dp" 
      android:layout_height="match_parent" 
      android:id="@+id/spinnerFoodCat" 
      android:entries="@array/Food_Cat" 
      android:layout_gravity="center" /> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:text="Meal Category :" 
      android:id="@+id/textView12" 
      android:layout_marginTop="25dp" 
      android:layout_below="@+id/textView" 
      android:layout_alignParentStart="true" 
      android:layout_marginStart="47dp" /> 

     <Spinner 
      android:layout_width="244dp" 
      android:layout_height="match_parent" 
      android:id="@+id/spinnerMealCat" 

      android:layout_gravity="center" /> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:text="Recipe Name :" 
      android:id="@+id/textView13" 
      android:layout_marginTop="25dp" 
      android:layout_below="@+id/spinner" 
      android:layout_alignParentStart="true" 
      android:layout_marginStart="47dp" /> 

     <EditText 
      android:layout_width="271dp" 
      android:layout_height="match_parent" 
      android:id="@+id/RecipeName" 
      android:hint="Enter Recipe Name" 
      android:layout_below="@+id/textView13" 
      android:layout_gravity="center_horizontal" /> 



     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:text="Main Ingredients:" 
      android:id="@+id/textView15" 
      android:layout_marginStart="47dp" /> 

     <LinearLayout 
      android:orientation="vertical" 
      android:id="@+id/chk_layout" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 
     </LinearLayout> 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:id="@+id/sup_ingTextView" 
      android:layout_marginStart="47dp" /> 
     <LinearLayout 
      android:orientation="vertical" 
      android:id="@+id/chk_layout2" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 
     </LinearLayout> 


     <TextView 
      android:layout_width="303dp" 
      android:layout_height="match_parent" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:text="Instructions :" 
      android:id="@+id/textView17" 
      android:layout_marginTop="25dp" 
      android:layout_below="@+id/textView15" 
      android:layout_alignParentStart="true" 
      android:layout_marginStart="25dp" 
      android:layout_marginRight="20dp" /> 

     <EditText 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:inputType="textMultiLine" 
      android:ems="10" 
      android:id="@+id/RecipeTextArea" 
      android:editable="true" 
      android:layout_marginTop="25dp" 
      android:enabled="true" 
      android:minLines="6" 
      android:maxLines="6" 
      android:isScrollContainer="true" 
      android:hint="Enter Recipe Instructions here." 
      android:background="#fafafa" 
      android:textIsSelectable="true" 
      android:focusable="true" 
      android:gravity="top" 
      android:padding="24dp" 
      style="@style/Base.TextAppearance.AppCompat.Large" 
      /> 

    </LinearLayout> 
    </ScrollView> 

    <Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Add Recipe" 
    android:id="@+id/buttonAddRecipes" 
    android:capitalize="words" 
    android:clickable="true" 
    android:longClickable="false" 
    android:background="#4052b5" 
    android:textColor="#fefefe" 
    android:textSize="@dimen/abc_select_dialog_padding_start_material" 
    android:onClick="btnAddRecipes" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentEnd="true" /> 



</RelativeLayout> 

答えて

0

この例外は、あなたがレイアウトに二回、いくつかのビューを追加しようとし、その後焼成されます。

あなたはあなたのコードをcopypasted、私はそれはそれは、XMLレイアウト内の静的ビューを置く方が良いでしょう、このようなミスを避けるために

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

する必要があります信じ

ここ

TextView SeafoodTv = new TextView(this); 
SeafoodTv.setText("Seafood"); 
ll.addView(DairyTv); 
それを変えませんでした。

+0

まだ問題が存在します。 –

+0

@SonaliSeth logcatのスタックトレース出力に例外が発生した行番号を正確に見つけることができます。あなたにはもっと類似したタイプミスがあると思います – nnesterov

関連する問題