2012-02-25 65 views
1

我想創造歡迎屏幕,並在一段時間後顯示我的標籤。 但得到這樣的一些錯誤:試圖創建歡迎屏幕與AsyncTask

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.News/startPakage.tabs}: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost' 

我在一個XML文件,即之前的作品以及我所做的AsyncTask標籤。 我的代碼是:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.logoscreen); 
    new GetDataTask(this).execute();} 

    private class GetDataTask extends AsyncTask<Void, Void, Integer> { 
    Context  context; 
    GetDataTask(Context context){this.context=context;} 
    protected Integer doInBackground(Void... params) { 

      int waited = 0; 
      while (waited < 5000) { 
      try { 
      this.wait(100); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
      waited += 100; 
      } 
      return 1; 
    } 

    protected void onPostExecute(Integer result) { 

     setContentView(R.layout.tabs); 

     Resources res = getResources(); // Resource object to get Drawables 
     TabHost tabHost = getTabHost(); // The activity TabHost 
     TabHost.TabSpec spec; // Resusable TabSpec for each tab 
     Intent intent; // Reusable Intent for each tab 

     // Create an Intent to launch an Activity for the tab (to be reused) 
     intent = new Intent().setClass(context,start.class); 

     // Initialize a TabSpec for each tab and add it to the TabHost 
     spec = tabHost.newTabSpec("Heb news").setIndicator("Heb news") 

         .setContent(intent); 
     tabHost.addTab(spec); 

     // Do the same for the other tabs 
     intent = new Intent().setClass(context, rusNewsP.ListRusNews.class); 

     spec = tabHost.newTabSpec("Rus News").setIndicator("Rus News").setContent(intent); 
     tabHost.addTab(spec); 

     tabHost.setCurrentTab(0); 
    } 

} 

哪裏我的歡迎XML是:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/icon" /> 

</LinearLayout> 

和標籤XML

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@android:id/tabhost" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="5dp"> 
    <TabWidget 
     android:id="@android:id/tabs" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 
    <FrameLayout 
     android:id="@android:id/tabcontent" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:padding="5dp" /> 
</LinearLayout> 
</TabHost> 

感謝您的幫助!

+0

TabHost的結束標記在哪裏? – 2012-02-25 10:04:36

回答

1

您的AsyncTask沒有個你應該重寫這個部分e gettabhost()方法。 Gettabhost僅適用於擴展Tabactivity的類,而不是您的asynctask。

乾杯

1

你tabhost必須android.R.id.tabhost ID看到這個例子... http://dewful.com/?p=15

+0

確實沒有這個標籤=(TabHost)this.findViewById(android.R.id.tabhost);但我得到錯誤此錯誤的方法findViewById(INT)是未定義的類型tabs.GetDataTask tred刪除R但仍然得到這個 – 2012-02-25 11:33:31

+0

哦...... yar看到的鏈接和它的XML文件中的變化.... – 2012-02-25 11:37:28