将 Tabhost 添加到 android 项目

Adding a Tabhost to an android project

我正在按照此 link 中提供的示例创建 TabHost。但是我得到 "Fatal Exception" 错误。 --> http://developer.android.com/reference/android/app/TabActivity.html

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="polimi.aap.yas.personalhealthrecord.MainActivity">


    <android.support.v4.app.FragmentTabHost
        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:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="0"/>

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

        </LinearLayout>
    </android.support.v4.app.FragmentTabHost>
</RelativeLayout>

MainActivity.java

    package polimi.aap.yas.personalhealthrecord;
    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentTabHost;


    public class MainActivity extends FragmentActivity {


        private FragmentTabHost mTabHost;

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

        mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
      mTabHost.addTab(mTabHost.newTabSpec("t1").setIndicator("Login"),Login.class,null);
        mTabHost.addTab(mTabHost.newTabSpec("t2").setIndicator("Register"),SignUp.class,null);


    }
}

build.gradle(模块:应用程序)

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    useLibrary 'org.apache.http.legacy'


    defaultConfig {
        applicationId "polimi.aap.yas.personalhealthrecord"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:support-v4:23.1.1'
}

最后是 logcat

12-09 14:53:14.184 3570-3570/? D/dalvikvm: Not late-enabling CheckJNI (already on) 12-09 14:53:14.314 3570-3570/? D/AndroidRuntime: Shutting down VM 12-09 14:53:14.314 3570-3570/? W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xb1777678) 12-09 14:53:14.334 3570-3570/? E/AndroidRuntime: FATAL EXCEPTION: main java.lang.ClassCastException: polimi.aap.yas.personalhealthrecord.Login cannot be cast to android.support.v4.app.Fragment at android.support.v4.app.Fragment.instantiate(Fragment.java:423) at android.support.v4.app.FragmentTabHost.doTabChanged(FragmentTabHost.java:343) at android.support.v4.app.FragmentTabHost.onAttachedToWindow(FragmentTabHost.java:280) at android.view.View.dispatchAttachedToWindow(View.java:12125) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2453) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2460) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2460) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2460) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2460) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1207) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749) at android.view.Choreographer.doCallbacks(Choreographer.java:562) at android.view.Choreographer.doFrame(Choreographer.java:532) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735) at android.os.Handler.handleCallback(Handler.java:730) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5103) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) at dalvik.system.NativeStart.main(Native Method) 12-09 14:58:15.149 3570-3570/? I/Process: Sending signal. PID: 3570 SIG: 9

你看到什么问题了吗?预先感谢您的帮助。 让我也通过编写此代码来添加我的 objective。我需要一个 tabhost 来显示两个选项卡(登录和注册)。并按相关布局(login.xml和signup.xml).

填充每个标签的内容

Login.java

    package polimi.aap.yas.personalhealthrecord;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class Login extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
    }
}

您的问题是您的 Login class 是 AppCompatActivity,而不是 Fragment

当您向 TabHost 添加标签时,您应该像以前一样传递 .class。但是,这个 .class 应该来自定义的 Fragment,而不是 AppCompatActivity.

所以应该是

mTabHost.addTab(mTabHost.newTabSpec("t1").setIndicator("Login"),Login.class,null);

哪里

public class Login extends Fragment {
    /**/
}