2013-10-15 85 views
1

我是新來的android和嘗試移植一個iOS應用程序。 不幸的是我有一些麻煩讓我的基礎設置工作。Android的 - TabHost/TabWidget

我試圖執行一個類似的導航本教程: tutorial

它是一個包含若干選項卡 而是作爲教程或多或少簡單TabHost使用

tabHost.addTab(tabHost.newTabSpec("settings").setIndicator("settings").setContent(R.id.tab1)); 

和什麼工作我想要初始我的選項卡類似這樣:

tabHost.addTab(tabHost.newTabSpec("settings").setIndicator("settings").setContent(new Intent(this, SettingsActivity.class))); 

不幸的是當我點擊'設置 - 選項卡'時,該應用程序崩潰。

這是到目前爲止我的代碼:

MainActivity: 

package xxx; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.view.Menu; 
import android.widget.TabHost; 
import android.widget.TabHost.OnTabChangeListener; 

public class MainActivity extends Activity implements OnTabChangeListener 
{ 

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

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) 
    { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 


    private void initTabs() 
    { 
     TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost); 
     tabHost.setup(); 


     tabHost.addTab(tabHost.newTabSpec("Übersicht").setIndicator("Übersicht").setContent(R.id.tab1)); // <- is working fine 
     tabHost.addTab(tabHost.newTabSpec("Einstellungen").setIndicator("Einstellungen").setContent(new Intent(this, SettingsActivity.class))); <- crash 

     tabHost.setOnTabChangedListener(this); 

     tabHost.setCurrentTab(0); 
    } 


    @Override 
    public void onTabChanged(String tabId) 
    { 
     // TODO Auto-generated method stub 
    } 
} 

activity_main.xml中:

<RelativeLayout xmlns:android= 
    xmlns:tools="" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

    <TabHost 
     android:id="@android:id/tabhost" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" > 

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

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

      <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="horizontal" > 

       </LinearLayout> 

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

       </LinearLayout> 

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

       </LinearLayout> 

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

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

SettingsActivity:

package xxx; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.TextView; 

public class SettingsActivity extends Activity 
{ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     TextView tv = new TextView(this); 
     tv.setText("This is tab 2"); 
     setContentView(tv); 
    } 
} 

錯誤消息從logcat的:

10-15 03:52:22.711:W/dalvikvm(889):threadid = 1:線程退出 未捕獲異常(group = 0x41465700)10-15 03:52:22.851: E/AndroidRuntime(889)致命例外:main 10-15 03:52:22.851: E/AndroidRuntime(889):java.lang.IllegalStateException:您忘記了 來調用'public void setup(LocalActivityManager activityGroup)'嗎? 10-15 03:52:22.851:E/AndroidRuntime(889):at android.widget.TabHost $ IntentContentStrategy.getContentView(TabHost.java:747) 10-15 03:52:22.851:E/AndroidRuntime(889) ):at android.widget.TabHost.setCurrentTab(TabHost.java:413)10-15 03:52:22.851:E/AndroidRuntime(889):at android.widget.TabHost $ 2.onTabSelectionChanged(TabHost.java: 154)10-15 03:52:22.851:E/AndroidRuntime(889):at android.widget.TabWidget $ TabClickListener.onClick(TabWidget.java:546) 10-15 03:52:22.851:E/AndroidRuntime (889):at android.view.View.performClick(View.java:4240)10-15 03:52:22.851: E/AndroidRuntime(889):at android.view.View $ Perform Click.run(View.java:17721)10-15 03:52:22.851:E/AndroidRuntime(889):at android.os.Handler.handleCallback(Handler.java:730)10-15 03:52 :22.851:E/AndroidRuntime(889):at android.os.Handler.dispatchMessage(Handler.java:92)10-15 03:52:22.851:E/AndroidRuntime(889):at android.os.Looper .loop(Looper.java:137)10-15 03:52:22.851: E/AndroidRuntime(889):at android.app.ActivityThread.main(ActivityThread.java:5103)10-15 03:52: 22.851:E/AndroidRuntime(889):at java.lang.reflect.Method.invokeNative(Native Method)10-15 03:52:22.851:E/AndroidRuntime(889):at java.lang.reflect.Method .invoke(Method.java:525)10-15 03: 52:22.851: E/AndroidRuntime(889):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit。java:737) 10-15 03:52:22.851:E/AndroidRuntime(889):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)10-15 03:52:22.851 :E/AndroidRuntime(889):在 dalvik.system.NativeStart.main(本機方法)

我認爲這個問題是,我的MainActivity不從的ActivityGroup和/或我不使用擴展LocalActivityManager。問題是,兩者都被棄用。爲了在不使用已棄用的方法和類的情況下工作,我需要做些什麼?

對不起,這可能很簡單的問題,但我沒有發現任何通過谷歌,我是新的android編程:)。

+0

第一個解決方案來在任何人的頭腦,有妳'AndroidManifest'定義此設置的活動? –

+0

哦,我忘了發佈我的清單,但是,是的,它在清單 – user1567896

回答

2

正如你所說,你應該從擴大的ActivityGroup,但如果你不這樣做想要一個不贊成的類,所以你可以使用FragmentsFragmentManagerFragments

+0

我將我的類更改爲Fragment和FragmentActivity,但仍收到錯誤消息'10 -15 03:52:22.851:E/AndroidRuntime(889):java.lang.IllegalStateException:您忘記調用'public void setup(LocalActivityManager的ActivityGroup) '?' – user1567896

+0

終於讓它與FragmentTabHost,Fragment,FragmentActivity和FragmantManager一起工作,謝謝你的提示 – user1567896

0

試試這個

tabHost.addTab(tabHost.newTabSpec("Einstellungen").setIndicator("Einstellungen").setContent(R.id.tab2))); 

更改代碼這樣

TabHost.TabSpec spec; // Resusable TabSpec for each tab 
Intent intent; // Reusable Intent for each tab 

// Create an Intent to launch an Activity for the tab (to be reused) 
intent = new Intent().setClass(this, ArtistsActivity.class); 

// Initialize a TabSpec for each tab and add it to the TabHost 
spec = tabHost.newTabSpec("artists").setIndicator("Artists", 
        res.getDrawable(R.drawable.ic_tab_artists)).setContent(intent); 
tabHost.addTab(spec); 

// Do the same for the other tabs 
intent = new Intent().setClass(this, AlbumsActivity.class); 
spec = tabHost.newTabSpec("albums").setIndicator("Albums", 
        res.getDrawable(R.drawable.ic_tab_albums)).setContent(intent); 
tabHost.addTab(spec); 

查看尼斯的Android教程示例hello-tabwidget

+0

中定義這是有效的,但正如我所提到的,我不想使用它。我想通過一個活動/課程初始化該選項卡。 – user1567896

0

做這樣的事情,

TabHost tabHost = getTabHost(); 

    Intent intentHome = new Intent().setClass(MainScreen.this, Home.class); 
    TabSpec tabSpecHome = tabHost.newTabSpec("Home").setIndicator("Home").setContent(intentHome); 
    tabHost.addTab(tabSpecHome);   
    tabHost.setCurrentTab(0); 

希望這有助於.. :)

+0

getTabHost()屬於TabActivity,這已被棄用 – user1567896

+0

檢查[This](http://learnandroideasily.blogspot.in/2013/07/) android-tabwidget-example.html) –

0

刪除 「這個」 從tabHost.addTab(tabHost.newTabSpec("Einstellungen").setIndicator("Einstellungen").setContent(new Intent(this, SettingsActivity.class)));

與getApplicationContext替換();

這樣

tabHost.addTab(tabHost.newTabSpec("Einstellungen").setIndicator("Einstellungen").setContent(new Intent(getApplicationContext(), SettingsActivity.class))); 
+0

這是行不通的,你爲什麼會建議一些不能解決問題的東西!! –