2016-08-20 9 views
1

を、私は、次の活動- java.lang.IllegalStateException

public class MainActivity extends AppCompatActivity { 

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

     TabHost mainTabHost = (TabHost) findViewById(R.id.mainTabHost); 
     mainTabHost.setup(); 

     mainTabHost.addTab(mainTabHost.newTabSpec("Tab 1").setIndicator("", ContextCompat.getDrawable(this, R.drawable.d1)).setContent(new Intent(this, Activity1.class))); 

     mainTabHost.addTab(mainTabHost.newTabSpec("Tab 2").setIndicator("", ContextCompat.getDrawable(this, R.drawable.d2)).setContent(new Intent(this, Activity2.class))); 

     mainTabHost.addTab(mainTabHost.newTabSpec("Tab 3").setIndicator("", ContextCompat.getDrawable(this, R.drawable.d3)).setContent(new Intent(this, Activity3.class))); 
    } 
} 

を持っており、このボタンをタップすると、これを起動する他の活動は、あるXML

<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@id/mainTabHost" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingLeft="@dimen/activityHorizontalMargin" 
    android:paddingRight="@dimen/activityHorizontalMargin" 
    android:paddingTop="@dimen/activityVerticalMargin" 
    tools:context=".MainActivity"> 

    <RelativeLayout 
     android:id="@id/mainHomeLayout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

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

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

    </RelativeLayout> 

</TabHost> 

ですアクティビティ。しかし、活動は起動しないと、私は次のエラーを取得:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nirvanapass/com.nirvanapass.MainActivity}: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'? 

私はオンラインで検索しました - しかし、私はActivityGroupまたはTabActivityが使用見つけることができるものは何でも解決 - どちらも推奨されていませんが。このエラーに取り組み、この問題を解決するには?

+0

http://stackoverflow.com/questions/3272500/android-exception-:ここ

は、サンプルアプリケーションですあなたを忘れてしまったことを忘れてしまいましたpublic-void-setup-localactivitymanag –

+0

That sugg ests 'ActivityGroup'は推奨されていません。より良い方法を探していても、私のコードを書き直さなければならないという意味です。 – pratnala

+0

さて、私はViewPagerを調べます。 –

答えて

1

最適な解決策は、最新のタブソリューションを使用することです。 TabHostは、しばらくの間、時代遅れです。タブのコンテンツにIntentを使用すると、5年で非推奨になりました。それまでは決して良いアイデアではありませんでした。

ほとんどの現代のタブの実装では、ある種のタブ付きインジケータを使用してViewPagerを使用しています。 PagerTabStripTabLayoutはどちらもAndroid SDKにあり、Androidアーセナルには多くの他のタブインジケータがtheir ViewPager categoryにリストされています。

TabWidgetのレトロスタイリングを使用する場合は、FragmentTabHostを使用するか、タブが表示されるTabHostを使用します。

+0

すばらしい。私はこれを試して戻ってくるだろう(数日後)。ありがとう! – pratnala

+0

これは正しいと思われるので、今すぐ受け入れてください – pratnala

関連する問題