【问题标题】:Illegal State Exception when i set layout in java file android当我在java文件android中设置布局时出现非法状态异常
【发布时间】:2014-03-01 10:54:53
【问题描述】:

我在数据库 Sqlite 中有数据,然后我想获取它并设置输出看起来像书。我使用 ViewFlipper,然后我创建一个线性布局来放置数据并将其放入 viewFlipper。现在我有一些问题。可以你告诉我该怎么做?谢谢

这是我的代码

public class Dynamic_rb_Activity extends Activity{

int bookID, menuSize;
private Context context;
private BooksDB db;

@Override
protected void onCreate(Bundle savedInstanceState){

    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_dynamic_rb_sup2);
    context = this;
    ArrayList<MenuEntry> List_menus = new ArrayList<MenuEntry>();
    ThemeEntry theme = new ThemeEntry();
    ArrayList<IngredientEntry> ingredients = new ArrayList<IngredientEntry>();
    ArrayList<RecipeEntry> List_recipe = new ArrayList<RecipeEntry>();

    db = new BooksDB(context);

    Intent intent =  getIntent();
    int id = intent.getIntExtra("bookID", 0);
    System.out.println(id);

    //get menu by bookid
    List_menus = db.getMenu(id);
    int menuSize = List_menus.size();
    System.out.println(menuSize);

    if(menuSize != 0){
        ViewFlipper VF = (ViewFlipper) findViewById(R.id.viewflipper);
        //layout for book theme
        LinearLayout LLT = new LinearLayout(context);
        LLT.setOrientation(LinearLayout.VERTICAL);
        LLT.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        //get booktheme by bookID 
        theme = db.getthemeByID(id);
        String themePath = theme.getFilepath();
        int resid = getResources().getIdentifier(themePath, "drawable", getPackageName());
        LLT.setBackgroundResource(resid);

        // add view 
                    VF.removeAllViews();
        VF.addView(LLT);

        for (MenuEntry menu : List_menus) {

            int menuID = menu.getId();
            String menuName = menu.getName();

            //check kind of recipe menu
            System.out.println(menuID+","+menuName);
            String typeRecipe = db.getTypeRecipe(menuID);

            if(typeRecipe == "typeW"){
                // set layout to viewFlipper
                //layout for menu name
                LinearLayout LLM = new LinearLayout(context);
                LLM.setOrientation(LinearLayout.VERTICAL);
                LLM.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

                TextView textMenu = new TextView(context);
                textMenu.setText(menuName);
                textMenu.setTextSize((float) 24.0);
                textMenu.setGravity(Gravity.CENTER_HORIZONTAL);

                LLM.addView(textMenu);
                VF.addView(LLM);

                //layout for ingredient
                LinearLayout LLIM = new LinearLayout(context);
                LLIM.setOrientation(LinearLayout.VERTICAL);
                LLIM.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

                LinearLayout LLIS = new LinearLayout(context);
                LLIS.setOrientation(LinearLayout.HORIZONTAL);
                LLIS.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));


                //get ingredient by menuID
                ingredients = db.getIngredientByID(menuID);
                for (IngredientEntry ingredient : ingredients) {
                    String detail_i = ingredient.getName();
                    String amount = ingredient.getAmount();
                    String unit = ingredient.getUnit();
                    //name
                    TextView textDetail = new TextView(context);
                    textDetail.setText(detail_i);
                    textDetail.setPadding(5, 10, 5, 10);
                    textDetail.setTextSize(24);
                    //amount
                    TextView textAmount = new TextView(context);
                    textAmount.setText(amount);
                    textAmount.setPadding(5, 10, 5, 10);
                    textAmount.setTextSize(24);
                    //unit
                    TextView textUnit = new TextView(context);
                    textUnit.setText(unit);
                    textUnit.setPadding(5, 10, 5, 10);
                    textUnit.setTextSize(24);
                    //add Text view to linear layout sup
                    LLIS.addView(textDetail);
                    LLIS.addView(textAmount);
                    LLIS.addView(textUnit);
                    //add LLIS to LLIM
                    LLIM.addView(LLIS);
                }
                VF.addView(LLIM);

                //layout for recipe
                LinearLayout LLRM = new LinearLayout(context);
                LLRM.setOrientation(LinearLayout.VERTICAL);
                LLRM.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

                //get recipe by menuID
                List_recipe = db.getRecipeByID(menuID);
                for (RecipeEntry recipe : List_recipe) {
                    String detail_r  = recipe.getDetails();
                    TextView textRecipe = new TextView(context);
                    textRecipe.setText(detail_r);
                    LLRM.addView(textRecipe);   
                }

                VF.addView(LLRM);
            }

        }
    }       
}

}

这是我的 logcat 错误

03-01 20:59:27.876: E/AndroidRuntime(17778): FATAL EXCEPTION: main
03-01 20:59:27.876: E/AndroidRuntime(17778): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.splash.bookguk_project/category_viewbook_sub2_1.Dynamic_rb_Activity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
03-01 20:59:27.876: E/AndroidRuntime(17778):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
03-01 20:59:27.876: E/AndroidRuntime(17778):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
03-01 20:59:27.876: E/AndroidRuntime(17778):    at android.app.ActivityThread.access$700(ActivityThread.java:140)
03-01 20:59:27.876: E/AndroidRuntime(17778):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
03-01 20:59:27.876: E/AndroidRuntime(17778):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-01 20:59:27.876: E/AndroidRuntime(17778):    at android.os.Looper.loop(Looper.java:137)
03-01 20:59:27.876: E/AndroidRuntime(17778):    at android.app.ActivityThread.main(ActivityThread.java:4921)
03-01 20:59:27.876: E/AndroidRuntime(17778):    at java.lang.reflect.Method.invokeNative(Native Method)
03-01 20:59:27.876: E/AndroidRuntime(17778):    at java.lang.reflect.Method.invoke(Method.java:511)
03-01 20:59:27.876: E/AndroidRuntime(17778):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
03-01 20:59:27.876: E/AndroidRuntime(17778):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
03-01 20:59:27.876: E/AndroidRuntime(17778):    at dalvik.system.NativeStart.main(Native Method)
03-01 20:59:27.876: E/AndroidRuntime(17778): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
03-01 20:59:27.876: E/AndroidRuntime(17778):    at android.view.ViewGroup.addViewInner(ViewGroup.java:3620)
03-01 20:59:27.876: E/AndroidRuntime(17778):    at android.view.ViewGroup.addView(ViewGroup.java:3491)
03-01 20:59:27.876: E/AndroidRuntime(17778):    at android.view.ViewGroup.addView(ViewGroup.java:3436)
03-01 20:59:27.876: E/AndroidRuntime(17778):    at android.view.ViewGroup.addView(ViewGroup.java:3412)
03-01 20:59:27.876: E/AndroidRuntime(17778):    at category_viewbook_sub2_1.Dynamic_rb_Activity.onCreate(Dynamic_rb_Activity.java:125)
03-01 20:59:27.876: E/AndroidRuntime(17778):    at android.app.Activity.performCreate(Activity.java:5206)
03-01 20:59:27.876: E/AndroidRuntime(17778):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
03-01 20:59:27.876: E/AndroidRuntime(17778):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
03-01 20:59:27.876: E/AndroidRuntime(17778):    ... 11 more

【问题讨论】:

    标签: android view parent-child illegalstateexception


    【解决方案1】:

    尝试在类文件的第 125 行添加这一行

    VF.removeAllViews();
    

    【讨论】:

      猜你喜欢
      • 2013-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-06
      • 2014-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多