2014-01-23 2 views
1

Android가 처음이며 개발 중입니다. 탭 호스트 위젯을 사용하고 있습니다. 내가하고있는 일은 3 개의 Java 클래스와 해당 XML 파일을 만들었다는 것입니다. 각 Java 클래스는 XML을로드 할 수 있습니다. 누군가 탭을 누르면 Java 클래스 위로 리디렉션됩니다. 아래 코드를 사용하십시오. 지금 사용하고있는 코드를 아래에서 찾으십시오.오류 : TabHost에는 id 속성이 'android.R.id.tabs'인 TabWidget이 있어야합니다.

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> 
(210)

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> 

로그 캣 오류를 가지고

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과 같이 변경되어야

XML과 public void intialiseTabWidgets() 방법 AVING 오류 :

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

및 널 언급 한 OnCreate 방법 있어야 intialiseTabWidgets() 방법을 사용

TabHost tabHost = getTabHost(); 

tabHost = getTabHost(); 
변경되어야
+0

대단히 감사합니다! –