2012-08-01 164 views
0

我有一個TabHost應用程序與4個選項卡。Android的TabHost /後退按鈕

標籤1 列表A>列表B>視圖

標籤2 相機活性的啓動相機和閃光燈的onCreate

標籤3 列表C>查看

標籤4 視圖d>查看E

因此,當我第一次啓動應用程序時,我可以在沒有任何問題的情況下來回訪問不同的標籤頁。但是,一旦我啓動了Tab 2(攝像機標籤),並從那裏進入其他標籤,然後沿着堆疊進入,當我按下後退按鈕時,即使我不在標籤2(即列表B中,按回來按鈕和相機開始,即使我只是回到列表A)...當我再次進入Tab 2時,應用程序將崩潰(對不起,Android相機遇到問題...等)

誰能告訴我問題是什麼?下面是我TabHost活動

package com.myapp.appname; 

import android.app.TabActivity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.widget.ImageView; 
import android.widget.TabHost; 
import android.widget.TextView; 
import android.view.Window; 


public class MainActivity extends TabActivity { 
    /** Called when the activity is first created. */ 

    private TabHost tabHost; 
@Override 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(null); 

    setContentView(R.layout.main); 
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.top_titlebar); 
    setTabs(); 

} 

private void setTabs() { 
    tabHost = getTabHost(); 

    listTab(R.string.tab_1, R.drawable.ic_tab_1); 
    cameraTab(R.string.tab_2, R.drawable.ic_tab_2); 
    list2Tab(R.string.tab_3, R.drawable.ic_tab_3); 
    viewTab(R.string.tab_4, R.drawable.ic_tab_4); 
} 

private void listTab(int labelId, int drawableId) { 
    Intent intent = new Intent(this,ListActivity.class);  
    TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);  

    View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false); 

    TextView title = (TextView) tabIndicator.findViewById(R.id.title); 
    title.setText(labelId); 
    ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon); 
    icon.setImageResource(drawableId); 

    spec.setIndicator(tabIndicator); 
    spec.setContent(intent); 
    tabHost.addTab(spec); 
} 
private void cameraTab(int labelId, int drawableId) { 

    Intent intent = new Intent(this,CameraActivity.class); 
    TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);  

    View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false); 

    TextView title = (TextView) tabIndicator.findViewById(R.id.title); 
    title.setText(labelId); 
    ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon); 
    icon.setImageResource(drawableId); 

    spec.setIndicator(tabIndicator); 
    spec.setContent(intent); 
    tabHost.addTab(spec); 
} 
private void list2Tab(int labelId, int drawableId) { 
    Intent intent = new Intent(this,List2Activity.class); 
    TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);  

    View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false); 

    TextView title = (TextView) tabIndicator.findViewById(R.id.title); 
    title.setText(labelId); 
    ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon); 
    icon.setImageResource(drawableId); 

    spec.setIndicator(tabIndicator); 
    spec.setContent(intent); 
    tabHost.addTab(spec); 
} 
private void viewTab(int labelId, int drawableId) { 
    Intent intent = new Intent(this,ViewActivity.class);  
    TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);  

    View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false); 

    TextView title = (TextView) tabIndicator.findViewById(R.id.title); 
    title.setText(labelId); 
    ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon); 
    icon.setImageResource(drawableId); 

    spec.setIndicator(tabIndicator); 
    spec.setContent(intent); 
    tabHost.addTab(spec); 
} 

}

回答

1

我不認爲這個問題是tabhost,但攝像頭。嘗試將onCreate中的代碼移動到onResume。另外,當您轉到另一個標籤時,您應該擔心釋放相機。