2012-05-04 53 views
0

我想創建一個tabbar和規範,但事情是我需要在asycn任務中做它,所以它沒有TabActivity。在下面運行此代碼後,我只能看到我的主佈局。最後,Im試圖做的是,我想向用戶展示一個進度條,它表示在安裝應用程序時必要的文件第一次被複制,同時複製文件,然後創建tabHost並添加規格。如果沒有asyncTask,它會很好運行,但是在第一次運行應用程序時,屏幕會被鎖定20或30秒,直到複製過程完成。任何想法 ?提前致謝。在Android中創建Tabhost並從AsyncTask添加規範

public class AsyncTest extends AsyncTask<Void, Void, Void> { 

Context context; 
DataBaseJSONFunctions json; 

TabHost tabHost; 

TabWidget tabWidget; 

Resources res; 

TabHost.TabSpec sp; 

Intent intent; 

ProgressDialog dialog; 

Activity ac; 

public AsyncTest(Context context, TabHost tabHost, TabHost.TabSpec sp, Bundle savedInstanceState) { 
    this.context = context; 
    json = new DataBaseJSONFunctions(context); 
    this.tabHost = tabHost; 
    tabWidget = tabHost.getTabWidget(); 
    this.sp = sp; 
    ac = (Activity) context; 
    res = context.getResources(); 

    LocalActivityManager mlam = new LocalActivityManager(ac, false); 
    mlam.dispatchCreate(savedInstanceState); 
    tabHost.setup(mlam); 

} 

@Override 
protected Void doInBackground(Void... params) { 
    initializeAll(); 
    return null; 
} 

@Override 
protected void onPostExecute(Void result) { 

    dialog.dismiss(); 

    // to go through to the another activity in the tab I need to initialize an intent. 
    // and I need to set the Tab bar and it's icon. 
    intent = new Intent().setClass(ac, Activities.class); 
    sp = tabHost.newTabSpec("activities").setIndicator("activities",res.getDrawable(R.drawable.tab_activities_selector)).setContent(intent); 
    tabHost.addTab(sp); 

    // doing the same things for Songs Activity. 
    intent = new Intent().setClass(ac, Promotions.class); 
    sp = tabHost.newTabSpec("promotions").setIndicator("promotions",res.getDrawable(R.drawable.tab_promotions_selector)).setContent(intent); 
    tabHost.addTab(sp); 


    // doing the same things for another Activity. 
    intent = new Intent().setClass(ac,Menu.class); 
    sp = tabHost.newTabSpec("menu").setIndicator("Menu",res.getDrawable(R.drawable.tab_menu_selector)).setContent(intent); 
    tabHost.addTab(sp); 


    intent = new Intent().setClass(ac, Gallery.class); 
    sp = tabHost.newTabSpec("gallery").setIndicator("Gallery",res.getDrawable(R.drawable.tab_gallery_selector)).setContent(intent); 
    tabHost.addTab(sp); 


    intent = new Intent().setClass(ac, Info.class); 
    sp = tabHost.newTabSpec("info").setIndicator("Info",res.getDrawable(R.drawable.tab_info_selector)).setContent(intent); 
    tabHost.addTab(sp); 

    for(int i = 0; i < tabWidget.getChildCount(); i++){ 
     tabWidget.getChildAt(i).setBackgroundResource(R.drawable.tab_bar); 
    } 

    tabHost.setCurrentTab(0); 

    // Starting location listener service. 
    ac.startService(new Intent(ac, LocationService.class)); 

    ac.setContentView(R.layout.tabbar_main); 
    super.onPostExecute(result); 
} 

@Override 
protected void onPreExecute() { 
    dialog = ProgressDialog.show(context, "", "Copying files please wait..."); 
    super.onPreExecute(); 
} 
+0

onPostExecute()在完成後調用doInBackground() –

+0

等等是什麼?有什麼不對? – osayilgan

+0

我認爲你使用單獨的威脅來製作tabActivity。 –

回答

0

我發現這樣做的方式,我有有能見度遠去,而onPreExecute()我設置這個圖像中可見的可見性的形象圖。還onPostExecute()我像它以前一樣使它變成了GONE。

0

for tab 佈局你的xml應該像這樣;

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <TabHost 
     android:id="@android:id/tabhost" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="vertical" > 

      <FrameLayout 
       android:id="@android:id/tabcontent" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" /> 

      <TabWidget 
       android:id="@android:id/tabs" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="0" /> 
     </LinearLayout> 
    </TabHost> 


</RelativeLayout> 
+1

我認爲它不是關於佈局。因爲我有相同的佈局,但在調用setContentView(R.layout.installation_screen)的行中,我得到了該錯誤。我正在執行PreExecute()。如果我刪除它,一切都很好。我只是想顯示一個背景圖像,而不是黑屏,而異步任務是runnig。 – osayilgan