【问题标题】:Dynamic TabHost working in Android 2.2, but not in 4.x动态 TabHost 在 Android 2.2 中工作,但在 4.x 中不工作
【发布时间】:2013-09-07 20:40:19
【问题描述】:

在我的应用程序中,我动态创建 TabHost 并用数据填充它: 布局:

<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>

活动创建代码:

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

在 Android 2.2 中(我在真实设备上测试)它可以正常工作,一切正常。但我无法在 Android 4 上启动此代码,我有以下异常:

09-07 20:35:45.428: E/AndroidRuntime(847): java.lang.RuntimeException: 无法开始活动 组件信息{com.viled.viledapp/com.viled.viledapp.ProductionCategories}: android.content.res.Resources$NotFoundException:资源 ID #0x0

我跟踪了代码,发现异常发生在tabHost.addTab(tab);

有什么问题?

【问题讨论】:

    标签: java android android-tabhost


    【解决方案1】:

    我自己解决了这个问题。 刚刚改变:

    final TabHost tabHost = new TabHost(this);
    

    到:

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

    现在一切正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-01
      • 2014-10-29
      • 1970-01-01
      • 1970-01-01
      • 2013-10-12
      • 2012-10-29
      相关资源
      最近更新 更多