我是Android和开发新手。我正在使用Tab主机小部件。我正在做的是我制作了3个Java类和相应的xml文件。每个Java类都可以加载其xml。当有人按下选项卡时,它会重定向到上面的Java类。请找到我使用的以下代码。现在我遇到错误,请找到我使用的以下代码。
CompareActivity.java
package com.example.comparison.lk;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.app.NotificationCompat.Action;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import com.tabs.*;
public class CompareActivity extends TabActivity {
TabHost tabHost;
Intent intent;
TabHost.TabSpec spec;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_compare);
ActionBar actionbr= getActionBar();
actionbr.setBackgroundDrawable(new ColorDrawable(0XFF1c3c4d));
intialiseTabWidgets();
tabHost = getTabHost();
tabHost.setOnTabChangedListener(new OnTabChangeListener(){
@Override
public void onTabChanged(String tabId) {
}
});
}
public void intialiseTabWidgets()
{
//TabHost main_tab=(TabHost)findViewById(R.id.tabhost);
//main_tab.setup();
/************* TAB1 ************/
// Create Intents to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, Login.class);
spec = tabHost.newTabSpec("First").setIndicator("").setContent(intent);
// Add intent to tab
tabHost.addTab(spec);
/************* TAB2 ************/
// Create Intents to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, compareNow.class);
spec = tabHost.newTabSpec("second").setIndicator("").setContent(intent);
// Add intent to tab
tabHost.addTab(spec);
/************* TAB3 ************/
intent = new Intent().setClass(this, Search.class);
spec = tabHost.newTabSpec("third").setIndicator("").setContent(intent);
tabHost.addTab(spec);
}
comapreNow.java/Login.java/Search.java 所有这些使用相同结构的类
package com.tabs;
import com.example.comparison.lk.R;
import android.app.Activity;
import android.app.TabActivity;
import android.os.Bundle;
import android.widget.TabHost.OnTabChangeListener;
public class compareNow extends TabActivity implements OnTabChangeListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_compare);
}
@Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
}
}
activity_compare.xml
<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout 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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/topbar" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" >
</RelativeLayout>
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</TabHost>
</LinearLayout>
</FrameLayout>
compare_now.xml / login.xml / search.xml 所有这些文件具有相同的结构
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
AndroidManifest文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.comparison.lk"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:label="@string/app_name"
android:theme="@style/AppBaseTheme"
>
<activity
android:name="com.example.comparison.lk.CompareActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.tabs.compareNow"></activity>
<activity android:name="com.tabs.Login"></activity>
<activity android:name="com.tabs.Search"></activity>
</application>
</manifest>
Logcat错误
01-23 12:13:08.968: E/AndroidRuntime(27433): FATAL EXCEPTION: main
01-23 12:13:08.968: E/AndroidRuntime(27433): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.comparison.lk/com.example.comparison.lk.CompareActivity}: java.lang.RuntimeException: Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'
01-23 12:13:08.968: E/AndroidRuntime(27433): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
01-23 12:13:08.968: E/AndroidRuntime(27433): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
01-23 12:13:08.968: E/AndroidRuntime(27433): at android.app.ActivityThread.access$600(ActivityThread.java:127)
01-23 12:13:08.968: E/AndroidRuntime(27433): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
01-23 12:13:08.968: E/AndroidRuntime(27433): at android.os.Handler.dispatchMessage(Handler.java:99)
01-23 12:13:08.968: E/AndroidRuntime(27433): at android.os.Looper.loop(Looper.java:137)
01-23 12:13:08.968: E/AndroidRuntime(27433): at android.app.ActivityThread.main(ActivityThread.java:4511)
01-23 12:13:08.968: E/AndroidRuntime(27433): at java.lang.reflect.Method.invokeNative(Native Method)
01-23 12:13:08.968: E/AndroidRuntime(27433): at java.lang.reflect.Method.invoke(Method.java:511)
01-23 12:13:08.968: E/AndroidRuntime(27433): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
01-23 12:13:08.968: E/AndroidRuntime(27433): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
01-23 12:13:08.968: E/AndroidRuntime(27433): at dalvik.system.NativeStart.main(Native Method)
01-23 12:13:08.968: E/AndroidRuntime(27433): Caused by: java.lang.RuntimeException: Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'
01-23 12:13:08.968: E/AndroidRuntime(27433): at android.widget.TabHost.setup(TabHost.java:124)
01-23 12:13:08.968: E/AndroidRuntime(27433): at android.widget.TabHost.setup(TabHost.java:178)
01-23 12:13:08.968: E/AndroidRuntime(27433): at android.app.TabActivity.onContentChanged(TabActivity.java:135)
01-23 12:13:08.968: E/AndroidRuntime(27433): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:275)
01-23 12:13:08.968: E/AndroidRuntime(27433): at android.app.Activity.setContentView(Activity.java:1835)
01-23 12:13:08.968: E/AndroidRuntime(27433): at com.example.comparison.lk.CompareActivity.onCreate(CompareActivity.java:25)
01-23 12:13:08.968: E/AndroidRuntime(27433): at android.app.Activity.performCreate(Activity.java:4470)
01-23 12:13:08.968: E/AndroidRuntime(27433): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
01-23 12:13:08.968: E/AndroidRuntime(27433): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
01-23 12:13:08.968: E/AndroidRuntime(27433): ... 11 more
答案 0 :(得分:3)
您在使用xml和public void intialiseTabWidgets()
方法
xml应更改为:
<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout 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" >
<TabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
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"
android:background="#160203" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
</TabHost>
</FrameLayout>
并且使用intialiseTabWidgets()
方法应该具有空引用oncreate方法
TabHost tabHost = getTabHost();
应改为
tabHost = getTabHost();