2016-07-28 45 views
2

所以我通過拖放操作製作了一個TabHot,然後在選項卡中進行了編碼以使其工作並運行。不過,我不得不使用標籤線性佈局來處理不方便的內容。所以我決定爲每個標籤製作一個單獨的活動。現在當我將設置內容更改爲類的意圖時,我的項目崩潰了。我搜索了這個,並尋找修復,但沒有。當使用自定義活動時,TabHost崩潰

這裏是我的代碼:

public class MainActivity extends AppCompatActivity { 

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

private void InitTabHost(){ 

    //Setup Initial Tab Host 
    TabHost tbHost = (TabHost)findViewById(R.id.tabHost); 
    tbHost.setup(); 

    //Setup Tab 1 

    TabHost.TabSpec NFtab = tbHost.newTabSpec("one"); 
    NFtab.setIndicator("News Feed"); 
    Intent intent = new Intent(this,NewsFeed.class); 
    NFtab.setContent(intent); 
    tbHost.addTab(NFtab); 

    TabHost.TabSpec AGTab = tbHost.newTabSpec("two"); 
    AGTab.setIndicator("Art Gallery"); 
    AGTab.setContent(R.id.tab2); 
    tbHost.addTab(AGTab); 

    TabHost.TabSpec CreateTab = tbHost.newTabSpec("three"); 
    CreateTab.setIndicator("Create"); 
    CreateTab.setContent(R.id.tab3); 
    tbHost.addTab(CreateTab); 

    TabHost.TabSpec ProfileTab = tbHost.newTabSpec("Four"); 
    ProfileTab.setIndicator("Profile"); 
    ProfileTab.setContent(R.id.tab4); 
    tbHost.addTab(ProfileTab); 
}} 

這裏是我的主要佈局與tabhost:

<?xml version="1.0" encoding="utf-8"?> 

<TabHost 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/tabHost"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_alignParentBottom="true" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      /> 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <LinearLayout 
       android:id="@+id/tab1" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 

      </LinearLayout> 

      <LinearLayout 
       android:id="@+id/tab2" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 

      </LinearLayout> 

      <LinearLayout 
       android:id="@+id/tab3" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 

      </LinearLayout> 

      <LinearLayout 
       android:id="@+id/tab4" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 

      </LinearLayout> 
     </FrameLayout> 
    </RelativeLayout> 
</TabHost> 

和這裏的其他活動類:

public class NewsFeed extends TabActivity { 

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

編輯的logcat:

07/28 01:56:29: Launching app 
$ adb shell am start -n "com.example.akashbakshi.artnsketch/com.example.akashbakshi.artnsketch.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER 
Connected to process 3940 on device Nexus_6P_API_23 [emulator-5554] 
I/art: Not late-enabling -Xcheck:jni (already on) 
W/System: ClassLoader referenced unknown path: /data/app/com.example.akashbakshi.artnsketch-2/lib/x86 
W/System: ClassLoader referenced unknown path: /data/app/com.example.akashbakshi.artnsketch-2/lib/x86 
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 
D/AndroidRuntime: Shutting down VM 
E/AndroidRuntime: FATAL EXCEPTION: main 
        Process: com.example.akashbakshi.artnsketch, PID: 3940 
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.akashbakshi.artnsketch/com.example.akashbakshi.artnsketch.MainActivity}: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'? 
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
         at android.app.ActivityThread.-wrap11(ActivityThread.java) 
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
         at android.os.Handler.dispatchMessage(Handler.java:102) 
         at android.os.Looper.loop(Looper.java:148) 
         at android.app.ActivityThread.main(ActivityThread.java:5417) 
         at java.lang.reflect.Method.invoke(Native Method) 
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
        Caused by: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'? 
         at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:722) 
         at android.widget.TabHost.setCurrentTab(TabHost.java:388) 
         at android.widget.TabHost.addTab(TabHost.java:222) 
         at com.example.akashbakshi.artnsketch.MainActivity.InitTabHost(MainActivity.java:30) 
         at com.example.akashbakshi.artnsketch.MainActivity.onCreate(MainActivity.java:15) 
         at android.app.Activity.performCreate(Activity.java:6237) 
         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  
         at android.app.ActivityThread.-wrap11(ActivityThread.java)  
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  
         at android.os.Handler.dispatchMessage(Handler.java:102)  
         at android.os.Looper.loop(Looper.java:148)  
         at android.app.ActivityThread.main(ActivityThread.java:5417)  
         at java.lang.reflect.Method.invoke(Native Method)  
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  
+0

分享您的logcat – SaravInfern

+0

Tabhost是舊,,更好地利用tablayout –

+0

你爲什麼不推薦使用''TabActivity發佈 –

回答

0

你可以使用我的代碼:

There are 2 classes: 

MyTabs.java

public class MyTabs extends TabLayout { 
    String text; 
    public MyTabs(Context context) { 
     super(context); 
    } 

    public MyTabs(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public MyTabs(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    public void setText(String text) { 
     this.text = text; 
    } 

    @Override 
    public void setTabTextColors(int normalColor, int selectedColor) { 
     super.setTabTextColors(normalColor, selectedColor); 
    } 

    @Override 
    public void setBackgroundColor(int color) { 
     super.setBackgroundColor(color); 
    } 

    @Override 
    public void addTab(@NonNull Tab tab) { 
     super.addTab(tab); 
    } 

    @NonNull 
    @Override 
    public Tab newTab() { 
     return super.newTab(); 
    } 

    @Override 
    public void setSmoothScrollingEnabled(boolean smoothScrollingEnabled) { 
     super.setSmoothScrollingEnabled(smoothScrollingEnabled); 
    } 

    @Override 
    public void setTabMode(int mode) { 
     super.setTabMode(mode); 
    } 

} 

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 

<android.support.design.widget.AppBarLayout 
    android:id="@+id/appbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingTop="@dimen/appbar_padding_top" 
    android:theme="@style/AppTheme.AppBarOverlay"> 


    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:layout_scrollFlags="enterAlways" 
     app:popupTheme="@style/AppTheme.PopupOverlay"> 

    </android.support.v7.widget.Toolbar> 

    <com.mypackage.here.MyTabs 
     android:id="@+id/mytabs" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

</android.support.design.widget.AppBarLayout> 

<android.support.v4.view.ViewPager 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

MainActivity.java

ViewPager mViewPager; 
    MyTabs tabs; 
    TabLayout.Tab latestTab; 
    TabLayout.Tab allVideosTab; 


    mViewPager = (ViewPager) findViewById(R.id.container); 
    mViewPager.setAdapter(mSectionsPagerAdapter); 

    //Tab layout 
    tabs = (MyTabs) findViewById(R.id.mytabs); 
    tabs.setTabTextColors(Color.LTGRAY, Color.WHITE); 
    tabs.setTabGravity(TabLayout.GRAVITY_CENTER); 
    tabs.setSmoothScrollingEnabled(true); 
    tabs.setTabMode(TabLayout.MODE_SCROLLABLE); 
    latestTab = tabs.newTab(); 
    allVideosTab = tabs.newTab(); 

    //ADD TABS IN REVERSE ORDER OF APPEARANCE 
    tabs.addTab(allVideosTab); 
    tabs.addTab(latestTab); 

    //*********ADD ALL tabs above this*/ 
    tabs.setupWithViewPager(mViewPager); 

    //Manipulate tabs here 
    latestTab.setText("Latest"); 
    allVideosTab.setText("All Videos");