首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在java文件android中设置布局时出现非法状态异常

在java文件android中设置布局时出现非法状态异常
EN

Stack Overflow用户
提问于 2014-03-01 18:54:54
回答 1查看 208关注 0票数 0

我在数据库Sqlite中有数据,然后我想获取它并设置输出,看起来像book .And,我使用ViewFlipper,然后我创建一个线性布局来放置数据并将其放到viewFlipper.Now中,我有一些problem.Can,你可以告诉我,我该怎么做?谢谢

这是我的代码

代码语言:javascript
复制
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错误

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2014-03-01 18:58:25

尝试将此行添加到类文件中的125行

代码语言:javascript
复制
VF.removeAllViews();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22113397

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档