2012-08-01 121 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。另外,当您转到另一个标签时,您应该担心释放相机。