2013-09-07 2 views
0

에 동적으로 TabHost를 생성하고 데이터로 채 웁니다 : 레이아웃 :동적 TabHost는 안드로이드 2.2에서 작동하지만 내 응용 프로그램에서 4.x의

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

안드로이드 2.2 (나는 진짜 장치에서 테스트한다.) 아무 문제없이 작동한다. 다 괜찮아.

09-07 20 : 35 : 45.428 : E/AndroidRuntime (847) : java.lang.RuntimeException가 : 시작할 수 없습니다 활동하지만, 나는 예외 다음 한 안드로이드 4에이 코드를 시작할 수 없습니다 ComponentInfo {com.viled.viledapp/com.viled.viledapp.ProductionCategories} : android.content.res.Resources $ NotFoundException : 자원 ID 번호 0x0으로

나는 코드를 추적하고, 예외는 라인 tabHost.addTab(tab);에서 발생

무엇이 잘못 될 수 있습니까?

답변

0

나는 스스로 문제를 해결했다. 은 그냥 변경 :

final TabHost tabHost = new TabHost(this); 

에 :

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

이제 모든 것이 괜찮습니다.