2012-10-05 54 views
0

在Androd_Tab_view.java文件,我有以下代碼:標籤視圖拋出異常

選項卡=(TabHost)findViewById(R.id.TabHost1); tabs.setup();

TabHost.TabSpec first_tab = tabs.newTabSpec("tag1"); 
first_tab.setContent(new Intent(this,FirstTab.class)); 
first_tab.setIndicator("Book"); 
tabs.addTab(first_tab); 
//SecondTab.class 
TabHost.TabSpec second_tab = tabs.newTabSpec("tag2"); 
second_tab.setContent(new Intent(this,SecondTab.class)); 
second_tab.setIndicator("Authors"); 
tabs.addTab(second_tab); 

在FirstTab.java文件我有以下代碼:

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

/* First Tab Content */ 
     TextView textView = new TextView(this); 
     textView.setText("First Tab"); 
     setContentView(textView); 

    } 
} 

在SecondTab.java文件我有以下代碼:

 public class SecondTab extends Activity { 
/** Called when the activity is first created. */ 
@Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); /* Second Tab Content */ 

     TextView textView = new TextView(this); 
     textView.setText("Second Tab"); 
     setContentView(textView); 

    } 
} 

但是當我嘗試運行此代碼我得到一個異常。什麼可以解決這個問題?

+0

請張貼logcat的。它會快速解決問題 – Simon

回答

0

你喜歡這個

void tabCreation() { 

    tabHost.setup(); 

    TabSpec spec1 = tabHost.newTabSpec("Accounts"); 
    spec1.setIndicator(createTabView(tabHost.getContext(), "Accounts", R.drawable.tab_home)); 
    Intent inte = new Intent(TabsActivity.this, Activityone.class); 
    inte.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    spec1.setContent(inte); 
    // spec1.setContent(R.id.tab1); 

    TabSpec spec2 = tabHost.newTabSpec("Contacts"); 
    spec2.setIndicator(createTabView(tabHost.getContext(), "Contacts", R.drawable.tab_account)); 
    Intent _int = new Intent(TabsActivity.this, Activitytwo.class); 
    _int.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    spec2.setContent(_int); 
    // /spec2.setContent(R.id.tab2); 

    TabSpec spec3 = tabHost.newTabSpec("Chats");   

    tabHost.addTab(spec1); 
    tabHost.addTab(spec2);  

    tabHost.setCurrentTab(0); 
} 

private static View createTabView(final Context context, final String text, final int id) { 

    View view = LayoutInflater.from(context).inflate(R.layout.tab_indicator, null); 
    TextView tv = (TextView) view.findViewById(R.id.title); 
    tv.setText(text); 
    ImageView icon = (ImageView) view.findViewById(R.id.icon); 
    icon.setImageResource(id); 
    return view; 
} 
} 

創建選項卡,你的活動是

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


    /* First Tab Content */ 
     TextView textView = new TextView(this); 
     textView.setText("First Tab"); 
     setContentView(textView); 

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


    /* First Tab Content */ 
     TextView textView = new TextView(this); 
     textView.setText("First Tab"); 
     setContentView(textView); 

    } 
} 

和TIS http://www.androidhive.info/2011/08/android-tab-layout-tutorial/

+0

我不能理解你想通過「spec1.setIndicator(createTabView(tabHost.getContext(),」Accounts「,R.drawable.tab_home));」; ??你能解釋一下嗎? –

+0

該行表明我們想要像這樣的http://www.mkyong.com/android/android-tablayout-example/這樣的標籤佈局設置圖像,這取決於我們的要求。 – NagarjunaReddy

+0

一次看到更新的代碼一點點的例子它是有用的... – NagarjunaReddy