tabactivity 无法工作,但我的代码没有错误

发布于 2024-09-19 08:56:57 字数 3949 浏览 0 评论 0原文

main.xml:

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

但这段代码没有错误,但我无法运行这段代码,它给了我 java.lang.RuntimeException:无法启动活动 ComponentInfo{com.test.TabbedListListActivity/com.test.TabbedListListActivity.TabbedListListActivity}:android.view.InflateException:二进制 XML 文件行 #2:错误膨胀类 tabhost

我不想使用 inflate ,每个人都可以帮助我,我不知道midtake在哪里

main.xml:

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

but this code not mistake but i canot run this code,it give me
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.TabbedListListActivity/com.test.TabbedListListActivity.TabbedListListActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class tabhost

i donot want o use inflate,every one can hlpe me ,i donot know where is midtake

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌上青苔 2024-09-26 08:57:15

您错过了布局 标签。这是正确的 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>

了解更多请查看这篇文章

you missed the layout <TabHost> Tag. this is the right 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>

For More Look out this Article

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文