简体   繁体   中英

Dynamic TabHost working in Android 2.2, but not in 4.x

In my application, I create TabHost dynamically and fill it by data: The layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/productionLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".ProductionCategories" >

</LinearLayout>

Activity creation code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_production_categories);

    application = (ViledApp) this.getApplication();

    LinearLayout relLay = (LinearLayout) findViewById(R.id.productionLayout);

    final TabHost tabHost = new TabHost(this);
    tabHost.setLayoutParams(new LinearLayout.LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    relLay.addView(tabHost);

    final TabWidget tabWidget = new TabWidget(this);
    tabWidget.setId(android.R.id.tabs);
    tabHost.addView(tabWidget, new LinearLayout.LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

    final FrameLayout frameLayout = new FrameLayout(this);
    frameLayout.setId(android.R.id.tabcontent);
    frameLayout.setPadding(0, 65, 0, 0);
    tabHost.addView(frameLayout, new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    tabHost.setup();

    ViledDB dbHelper = new ViledDB(this);

    int categoriesCount = dbHelper.getCategoriesCount();
    ArrayList<SimpleRecord> categories = dbHelper.getCategories();
    dbHelper.close();

    for (int i = 0; i < categoriesCount; i++) {
        final int categoryId = categories.get(i).id;

        final ArrayList<ViledProduct> productList = dbHelper
                .getProducts(categoryId);

        final TabSpec tab = tabHost.newTabSpec(String.format("tag_%d", categoryId));
        tab.setIndicator(categories.get(i).title);
        tab.setContent(new TabHost.TabContentFactory() {
            public View createTabContent(String tag) {
                final ListView lstGoods = new ListView(ProductionCategories.this);

                final ProductListAdapter adapter = new ProductListAdapter(
                        ProductionCategories.this,
                        R.layout.product_item_layout, productList);

                lstGoods.setAdapter(adapter);
                lstGoods.setOnCreateContextMenuListener(ProductionCategories.this);

                lstGoods.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> adapterView,
                            View view, int itemIndex, long arg3) {

                        ViledProduct product = productList.get(itemIndex);

                        Intent activity = new Intent (ProductionCategories.this, ProductItem.class);
                        application.setProduct(product);
                        startActivity(activity);
                    }
                });

                return lstGoods;
            }
        });

        tabHost.addTab(tab);
    }
}

In Android 2.2 (I test on real device) it working without any problems, everything is OK. But I can't start this code on Android 4, I have following exception:

09-07 20:35:45.428: E/AndroidRuntime(847): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.viled.viledapp/com.viled.viledapp.ProductionCategories}: android.content.res.Resources$NotFoundException: Resource ID #0x0

I traced the code, and exception happens on line tabHost.addTab(tab);

What can be wrong?

I solved the problem myself. Just changed:

final TabHost tabHost = new TabHost(this);

to:

final TabHost tabHost = new TabHost(ProductionCategories.this, null);

And now everything is ok.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM