我正在关注此链接中提供的示例以创建TabHost。但我得到了“致命异常”错误。 - > 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(module:app)
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:没有迟到的CheckJNI(已经开启) 12-09 14:53:14.314 3570-3570 /? D / AndroidRuntime:关闭VM 12-09 14:53:14.314 3570-3570 /? W / dalvikvm:threadid = 1:线程退出未捕获的异常(组= 0xb1777678) 12-09 14:53:14.334 3570-3570 /? E / AndroidRuntime:致命异常:主要 java.lang.ClassCastException:polimi.aap.yas.personalhealthrecord.Login无法强制转换为android.support.v4.app.Fragment 在android.support.v4.app.Fragment.instantiate(Fragment.java:423) 在android.support.v4.app.FragmentTabHost.doTabChanged(FragmentTabHost.java:343) 在android.support.v4.app.FragmentTabHost.onAttachedToWindow(FragmentTabHost.java:280) 在android.view.View.dispatchAttachedToWindow(View.java:12125) 在android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2453) 在android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2460) 在android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2460) 在android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2460) 在android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2460) 在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1207) 在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004) 在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5481) 在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:749) 在android.view.Choreographer.doCallbacks(Choreographer.java:562) 在android.view.Choreographer.doFrame(Choreographer.java:532) 在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:735) 在android.os.Handler.handleCallback(Handler.java:730) 在android.os.Handler.dispatchMessage(Handler.java:92) 在android.os.Looper.loop(Looper.java:137) 在android.app.ActivityThread.main(ActivityThread.java:5103) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:525) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737) 在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 /处理:发送信号。 PID:3570 SIG:9
你看到有什么问题吗?提前谢谢你的帮助。 让我通过编写此代码来添加我的目标。我需要一个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);
}
}
答案 0 :(得分:0)
您的问题是public void x()
{
Class1.abc.Add(2);
}
班级是Login
,而不是AppCompatActivity
。
当您向Fragment
添加标签时,您应该像您一样传递TabHost
。但是,此.class
应来自定义的.class
,而不是Fragment
。
所以它应该是
AppCompatActivity
,其中
mTabHost.addTab(mTabHost.newTabSpec("t1").setIndicator("Login"),Login.class,null);