程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> tabhost-Activity 跳轉到 TabHost後 Tab標簽頁為空

tabhost-Activity 跳轉到 TabHost後 Tab標簽頁為空

編輯:編程綜合問答
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");
    }

}

最佳回答:


    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天 ,,我靠靠靠靠靠,太悲劇了!!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved