2010-09-15 57 views
0

的main.xml:
tabactivity不能工作,但我的代碼搞錯

<tabwidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content"/> 
    <framelayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" > 
     <linearlayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> 
     <listview android:id="@+id/list1" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1"/> 
     </linearlayout> 
     <linearlayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> 
     <listview android:id="@+id/list2" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1"/> 

     </linearlayout> 
    </framelayout> 

public class TabbedListListActivity extends TabActivity implements OnTabChangeListener { 

private static final String LIST1_TAB_TAG = "List1"; 
private static final String LIST2_TAB_TAG = "List2"; 

// The two views in our tabbed example 
private ListView listView1; 
private ListView listView2; 

private TabHost tabHost; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 


    tabHost = getTabHost(); 
    // LayoutInflater.from(this).inflate(R.layout.main, tabHost.getTabContentView(), true); 


    tabHost.setOnTabChangedListener((OnTabChangeListener) this); 

    // setup list view 1 
    listView1 = (ListView) findViewById(R.id.list1); 

    // create some dummy strings to add to the list 
    List<String> list1Strings = new ArrayList<String>(); 
    list1Strings.add("Item 1"); 
    list1Strings.add("Item 2"); 
    list1Strings.add("Item 3"); 
    list1Strings.add("Item 4"); 
    listView1.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list1Strings)); 

    // setup list view 2 
    listView2 = (ListView) findViewById(R.id.list2); 

    // create some dummy strings to add to the list (make it empty initially) 
    List<String> list2Strings = new ArrayList<String>(); 
    listView2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list2Strings)); 

    // add an onclicklistener to add an item from the first list to the second list 
    listView1.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      String item = (String) listView1.getAdapter().getItem(position); 
      if(item != null) { 
       ((ArrayAdapter<String>) listView2.getAdapter()).add(item); 
       Toast.makeText(TabbedListListActivity.this, item + " added to list 2", Toast.LENGTH_SHORT).show(); 
      } 
     } 
    }); 

    // add views to tab host 
    tabHost.addTab(tabHost.newTabSpec(LIST1_TAB_TAG).setIndicator(LIST1_TAB_TAG).setContent(new TabContentFactory() { 
     public View createTabContent(String arg0) { 
      return listView1; 
     } 
    })); 
    tabHost.addTab(tabHost.newTabSpec(LIST2_TAB_TAG).setIndicator(LIST2_TAB_TAG).setContent(new TabContentFactory() { 
     public View createTabContent(String arg0) { 
      return listView2; 
     } 
    })); 

} 

/** 
* Implement logic here when a tab is selected 
*/ 
public void onTabChanged(String tabName) { 
    if(tabName.equals(LIST2_TAB_TAG)) { 

    } 
    else if(tabName.equals(LIST1_TAB_TAG)) { 
     //do something 
    } 
} 
} 

但是這個代碼不誤,但我canot運行這段代碼,它給我的java .lang.RuntimeException:無法啓動活動ComponentInfo {com.test.TabbedListListActivity/com.test.TabbedListListActivity.TabbedListListActivity}:android.view.InflateException:二進制XML文件行#2:錯誤膨脹類選項卡主辦

我想DONOT o使用膨脹,每個人都可以HLPE我,我DONOT知道哪裏是midtake

+0

你最好多放些精力在問你的問題,如果你想讓任何人知道你想要什麼...粘貼代碼,指定什麼不起作用等。 – Maragues 2010-09-15 13:39:43

回答

0

你錯過了佈局<TabHost>標籤。這是正確的main.xml

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
<tabwidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content"/> 
    <framelayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" > 
     <linearlayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> 
     <listview android:id="@+id/list1" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1"/> 
     </linearlayout> 
     <linearlayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> 
     <listview android:id="@+id/list2" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1"/> 

     </linearlayout> 
    </framelayout> 
</TabHost> 

有關詳細看出來這Article

+0

謝謝你的答覆,但你的答案也無法運行。 – pengwang 2010-09-17 01:14:57

+0

我加標籤,但它沒有出現在這篇文章中,我不知道爲什麼 – pengwang 2010-09-17 01:24:36

相關問題