注册 登录
编程论坛 Android开发

关于TabHost的问题

wkz2012 发布于 2013-04-13 21:26, 2029 次点击
想做一个简单的标签页,即TabHost,可编译完后,运行模拟器时,总是出现如下的情况。(我用的版本时2.3.3,开发工具是ADT)
只有本站会员才能查看附件,请 登录

下面是我的activity_main.xml文件


只有本站会员才能查看附件,请 登录
程序代码:
<?xml version="1.0"  encoding="utf-8"?>
  <TabHost  xmlns:android="http://schemas. android:id="@android:id/tabhost"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
    >
    <LinearLayout
        xmlns:android="http://schemas. android:id="@+id/textView1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/text1" />
    </LinearLayout>
   
     <LinearLayout
        android:id="@+id/tab2"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
       <TextView
                android:id="@+id/textView2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/text2"/>
    </LinearLayout>
</TabHost>




下面则是Mainfest.xml文件
 
   <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas. package="com.example.stone"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="10" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.stone.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
下面是java主程序


程序代码:
package com.example.stone;

import android.os.Bundle;
//import android.app.Activity;
import android.app.TabActivity;
//import android.content.Intent;
//import android.view.Menu;
import android.view.LayoutInflater;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.Toast;

public class MainActivity extends TabActivity {

    private TabHost mTabHost;
    //private Adapter1 ad1;   
    //private ListView ListView1;   
    //private Intent mIntent;
   
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        //ad1 = new Adapter1(MainActivity.this,4);
        //mIntent = new Intent();
        TabHost tabHost = this.getTabHost();
        LayoutInflater.from(this).inflate(R.layout.activity_main, mTabHost.getTabContentView(), true);

         tabHost.addTab(tabHost.newTabSpec("1")   
                        .setIndicator("OneTab",null)   
                        .setContent(R.id.tab1));
      

         tabHost.addTab(tabHost.newTabSpec("2")   
                      .setIndicator("TwoTab",null)   
                      .setContent(R.id.tab2));
         mTabHost.setOnTabChangedListener(new OnTabChangeListener(){
             public void onTabChanged(String tabId){
                 
                if(tabId == "1"){
                     DisplayToast("Tab1");
                 }
                else{
                    DisplayToast("Tab2");
                }
             }
            
         });




    }


    protected void DisplayToast(String s) {
        // TODO Auto-generated method stub
        Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
    }

    /*@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
*/
}
4 回复
#2
wkz20122013-04-13 21:27
补充一下,R。java文件时没问题的,编译没有错误,就是运行模拟器出现图中的情况。麻烦大神们了
#3
huang798_1862013-04-14 10:17
注册否??
#4
wkz20122013-04-14 17:26
回复 3楼 huang798_186
你问的是软件的注册吗?
#5
见面say_hi2014-09-16 11:18
应该是TabHost的配置文件错误吧,你可以参照下面的:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.
     android:id="@android:id/tabhost"
     android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_alignParentBottom="true"
            
            ></TabWidget>
        
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@android:id/tabs"
            />
    </RelativeLayout>
   
    </TabHost>
1