【问题标题】:Android- Tab Activity giving ERRORAndroid-标签活动给出错误
【发布时间】:2014-02-16 02:47:32
【问题描述】:

我必须创建两个选项卡。“TabHost tabHost = getTabHost();”给我一个错误,说方法 getTabHost() 在 MainActivity 中未定义。

我的 mainActivity.java 文件: 包 com.example.bckup;

import android.os.Bundle;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.content.Context;

public class MainActivity extends Activity {

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

        Resources ressources = getResources(); 

       TabHost tabHost = getTabHost();        

        // Android tab
        Intent intentAndroid = new Intent().setClass(this, line1.class);
        TabSpec tabSpecAndroid = tabHost.newTabSpec("Android")
                                .setContent(intentAndroid);

        //Linux tab
        Intent intentLinux = new Intent().setClass(this, line2.class);
        TabSpec tabSpecLinux = tabHost.newTabSpec("Linux")
                   .setContent(intentLinux);    



        // add all tabs 
        tabHost.addTab(tabSpecAndroid);
        tabHost.addTab(tabSpecLinux);


        //set Windows tab as default (zero based)
        tabHost.setCurrentTab(0);      
    }

}

   /* @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

*/

line1.java

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class line1 extends Activity {

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

   /* @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.friends_list, menu);
        return true;
    }*/
} 

acivity_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="match_parent"
        android:layout_height="match_parent" >

       <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0"
                android:orientation="horizontal" />

            <FrameLayout
                android:id="@+id/tabFrameLayout"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1" />
        </LinearLayout>

    </TabHost>

谁能帮我解决这个问题。

【问题讨论】:

    标签: java android eclipse android-intent


    【解决方案1】:

    您的主 Activity 应该扩展 TabActivity

    public class MainActivity extends TabActivity 
    

    【讨论】:

    • 我扩展 TabActivity 时出错。听说用 FragmentActivity 替换已弃用的 TabActivity
    【解决方案2】:

    使用

    TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);

    而不是

    TabHost tabHost = getTabHost();

    当然还有扩展TabActivity

    【讨论】:

    • 我扩展 TabActivity 时出错。听说用 FragmentActivity 替换已弃用的 TabActivity
    • 你不必使用 FragmentActivity
    • 我无法扩展 TabActivity。那么在这种情况下会有什么解决方案,你能回答吗?
    • 为什么不能扩展它?
    • 我扩展 TabActivity 时出错。 Android 将 TabActivity 替换为 FragmentActivity
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-15
    • 1970-01-01
    • 2018-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多