2014-01-23 30 views
1

我是Android新手,正在开发。我正在使用Tab主机部件。我在做什么是我做了3个Java类和相应的xml文件。每个Java类都可以加载它的xml。当有人按下该选项卡时,它会重定向到上面的Java类。请找到我使用的下面的代码。现在即时获取错误,请找到我使用的代码如下。错误:您的TabHost必须有一个TabWidget,其id属性为'android.R.id.tabs

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所有这些类中使用相同的stuture

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所有这些文件具有相同的stuture

<?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 

回答

3

你是h XML和public void intialiseTabWidgets()方法AVING错误

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(); 
+0

非常感谢你! –

相关问题