2 eyebrows cs Eyebrows_cs 于 2012.11.02 13:49 提问

Activity 跳转到 TabHost后 Tab标签页为空

// 情况描述 :  跳转后默认tab页面为空,切换下tab后,显示正常!!

public class ListContainActivity extends BaseActivity{

public static final String tag = "COOLSMILE_DEBUG";

private  TabHost tabHost ;

ContextTask contextTask ;

String mString="";

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 

    setContentView(R.layout.showcontain);

    ProgressRefresh();
}

private void InitUI(){  

        getIntentExtra();   // 得到Activity传递的信息

        Log.v(tag, "----------- InitUI start -----------");

        InitTabHost();
}

    private void InitTabHost(){

        Log.v(tag, "----------- InitTabHost start -----------");
        TabHost.TabSpec spec;  

        tabHost = (TabHost) findViewById(android.R.id.tabhost);
        tabHost.setup();

        spec=tabHost.newTabSpec("Refresh").setIndicator("Refresh").setContent(R.id.contain_tab0);
        tabHost.addTab(spec);  

        spec=tabHost.newTabSpec("Love").setIndicator("Love").setContent(R.id.contain_tab1);  
        tabHost.addTab(spec);  

        spec=tabHost.newTabSpec("Comment").setIndicator("Comment").setContent(R.id.contain_tab2);  
        tabHost.addTab(spec);  

        spec=tabHost.newTabSpec("Onward").setIndicator("Onward").setContent(R.id.contain_tab0);  
        tabHost.addTab(spec);  

        spec=tabHost.newTabSpec("Goback").setIndicator("Goback").setContent(R.id.contain_tab1); 
        tabHost.addTab(spec);  

        InitClickListener();
}
private void ProgressRefresh(){     

    Log.v(tag, "----------- progress_refresh start -----------");
    contextTask = new ContextTask(0);

    TaskHost taskHost = new TaskHost(){

        @Override
        public void onPreExecute(int requestCode) {
            // TODO Auto-generated method stub
            super.onPreExecute(requestCode);
            Log.v(tag, "----------- progress_refresh.onPreExecute() start -----------");
            InitUI();
        }

        @Override
        public void onPostExecute(int requestCode, String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(requestCode, result);

            Log.v(tag, "----------- progress_refresh.onPostExecute() start -----------"+result);

            TextView txt = (TextView)findViewById(R.id.text_Result);
            txt.setText("DoInBackground Result :"+result);
            TextView txt1 = (TextView)findViewById(R.id.text_Refresh);
            txt1.setText("Hello my name is "+mString);
        }
    };
    contextTask.setTaskHost(taskHost);
    contextTask.setProgress(new SimpleDialogProgress(this,"正在获取数据..."));
    contextTask.execute();      
}



    private void InitClickListener(){

        Log.v(tag, "----------- InitClickListener start -----------");

        RadioGroup radioGroup=(RadioGroup) this.findViewById(R.id.main_tab);  
        radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {  

            @Override  
            public void onCheckedChanged(RadioGroup group, int checkedId) {  
                // TODO Auto-generated method stub  
                switch (checkedId) {  
                    case R.id.main_tab_addHome:
                        tabHost.setCurrentTabByTag("Refresh");  
                        break;
                    case R.id.main_tab_addExam:  
                        tabHost.setCurrentTabByTag("Love");  
                        break;  
                    case R.id.main_tab_myExam:  
                        tabHost.setCurrentTabByTag("Comment");  
                        break;  
                    case R.id.main_tab_message: 
                        tabHost.setCurrentTabByTag("Onward");  
                        break;  
                    case R.id.main_tab_settings:
                        tabHost.setCurrentTabByTag("Goback");  
                        break;  
                    default:  
                        tabHost.setCurrentTabByTag("Refresh");  
                        break;  
                    }  
            }  
        });             
    }

    private void getIntentExtra(){
        Intent intent = getIntent();
        mString = intent.getExtras().getString("PRESIDENTS_CONTENT");
    }

}

3个回答

Eyebrows_cs
Eyebrows_cs   2012.11.02 16:55
已采纳
    spec=tabHost.newTabSpec("Refresh").setIndicator("Refresh").setContent(R.id.contain_tab0);
    tabHost.addTab(spec);  

    spec=tabHost.newTabSpec("Love").setIndicator("Love").setContent(R.id.contain_tab1);  
    tabHost.addTab(spec);  

    spec=tabHost.newTabSpec("Comment").setIndicator("Comment").setContent(R.id.contain_tab2);  
    tabHost.addTab(spec);  

    spec=tabHost.newTabSpec("Onward").setIndicator("Onward").setContent(R.id.contain_tab0);  
    tabHost.addTab(spec);  

    spec=tabHost.newTabSpec("Goback").setIndicator("Goback").setContent(R.id.contain_tab1); 
    tabHost.addTab(spec);  

问题就出现在这串代码里, 里面的TAG 与 setContent(R.id.contain_tab1);
不是一一对应的关系 , 原以为只要TAG值唯一就行了!..犯错了!!
之前为了图方便, 只在XML里写了3个tab 复用了2个tab用来测试,结果调了1天 ,,我靠靠靠靠靠,太悲剧了!!

ce_la_vie
ce_la_vie   2012.11.02 15:57

把这行代码加到onStart()onResume()中试试:

ProgressRefresh();

看看能不能解决,

对了,为什么InitUI()函数要在onPreExecute中调用?他只能从一个intent中获得一个string。所以我认为它应该放在oncreat()里面。

jiaojialulu
jiaojialulu   2015.05.20 20:47

我没有多对一的关系,但是也出现了这种问题……不知道怎么解决……

Csdn user default icon
上传中...
上传图片
插入图片