2011-12-19 1 views
3

私はタブバーを持つAndroidアプリケーションを開発しています。私はタブをクリックすると新しいクラスが開くはずです。これをどのように実装すればよいですか?私の主な問題は、タブをクリックする意図を渡すときです。その後、それはクラッシュしています。Androidアプリケーションのタブをクリックすると新しいクラスを開く際にエラーが発生する

私は自分のJavaを掲載しています:

package com.solataire; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.widget.TabHost; 
import android.widget.TabHost.TabSpec; 

public class MainActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

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

     TabSpec tabspec1 = tab_host.newTabSpec("TAB_1"); 
     tabspec1.setIndicator("Tab 1"); 
     tabspec1.setContent(R.id.first_tab); 
     tab_host.addTab(tabspec1); 

     TabSpec tabspec2 = tab_host.newTabSpec("TAB_2"); 
     tabspec2.setIndicator("Tab 2"); 
     tabspec2.setContent(R.id.second_tab); 
     tab_host.addTab(tabspec2); 

     TabSpec tabspec3 = tab_host.newTabSpec("TAB_3"); 
     tabspec3.setIndicator("Tab 3"); 
     tabspec3.setContent(R.id.third_tab); 
     tab_host.addTab(tabspec3); 

     tab_host.setCurrentTab(0); 
    } 
} 

私のXMLファイルは、次のとおりです。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TabHost android:id="@+id/tab_host" 
     android:layout_width="fill_parent" android:layout_height="fill_parent"> 
     <TabWidget android:layout_width="fill_parent" 
      android:layout_height="wrap_content" android:id="@android:id/tabs" 
      android:layout_gravity="bottom" /> 
     <FrameLayout android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" android:layout_height="fill_parent" > 
      <LinearLayout android:id="@+id/first_tab" 
       android:orientation="vertical" android:layout_width="fill_parent" 
       android:layout_height="fill_parent" > 
       <TextView android:layout_width="fill_parent" 
        android:layout_height="fill_parent" android:text="First Tab" /> 
       <!-- Replace TextView with your layout content for this tab --> 
      </LinearLayout> 
      <LinearLayout android:id="@+id/second_tab" 
       android:orientation="vertical" android:layout_width="fill_parent" 
        android:layout_height="fill_parent" > 
       <TextView android:layout_width="fill_parent" 
        android:layout_height="fill_parent" android:text="Second Tab" /> 
       <!-- Replace TextView with your layout content for this tab --> 
      </LinearLayout> 
      <LinearLayout android:id="@+id/third_tab" 
       android:orientation="vertical" android:layout_width="fill_parent" 
       android:layout_height="fill_parent" > 
       <TextView android:layout_width="fill_parent" 
        android:layout_height="fill_parent" android:text="One More Tab" /> 
       <!-- Replace TextView with your layout content for this tab --> 
      </LinearLayout> 
      <LinearLayout android:id="@+id/edit_item_text_tab" 
       android:orientation="vertical" android:layout_width="fill_parent" 
       android:layout_height="fill_parent" /> 
     </FrameLayout> 
    </TabHost> 
</LinearLayout> 

誰もが、この時点で私を助けることができますか?

+0

ベターはあなたのlogcatエラーを投稿することでしょう。 –

+0

あなたのマニフェストファイルに新しいクラスを追加しましたか? – Aamirkhan

答えて

3

これを試すことができます。

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Resources res = getResources(); // Resource object to get Drawables 
    TabHost tabHost = getTabHost(); // The activity TabHost 
    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); 

    intent = new Intent().setClass(this, SongsActivity.class); 
    spec = tabHost.newTabSpec("songs").setIndicator("Songs", 
         res.getDrawable(R.drawable.ic_tab_songs)) 
        .setContent(intent); 
    tabHost.addTab(spec); 

    tabHost.setCurrentTab(2); 
} 

これはxmlです:

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    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" 
     android:padding="5dp"> 
     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" /> 
     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:padding="5dp" /> 
    </LinearLayout> 
</TabHost> 
+0

この回答はあなたを助けてくれませんか? –

関連する問題