2012-02-25 28 views
1

我想在Android 2.1中使用AsyncTasks,但在我的代碼上得到錯誤。 我想顯示給用戶的標誌,然後顯示我的標籤,但得到我的AsyncTask錯誤。如果你可以看到我的代碼,並告訴我我的錯誤在哪裏。AsyncTasks和標籤

誤差

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' 

在我的代碼是:

public class tabs extends TabActivity { 

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) { 
     tabs.this.setContentView(R.layout.tabs); 
     //setContentView(R.layout.tabs); 

     TabHost tabHost= (TabHost)tabs.this.findViewById(android.R.id.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"?> 
<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> 

非常感謝!

+0

希望當你粘貼你的代碼時,你在結尾處留下了兩個閉合的大括號。如果沒有,那是你的問題。另外,當粘貼代碼時,確保所有標籤都處於良好狀態,當你不知道代碼屬於哪種方法時,很難閱讀代碼。 – dbDev 2012-02-25 18:02:31

回答

-1

你可以試試這個: 給ID爲:android:id="@+id/tabhost"

,並在代碼中使用:

TabHost tabHost= (TabHost)tabs.this.findViewById(R.id.tabhost); 

這樣做對你的XML文件中的所有聲明。

+0

做了你所說的但得到:引起:java.lang.RuntimeException:你的內容必須有一個TabHost的id屬性是'android.R.id.tabhost' – 2012-02-25 18:59:33

0

問題是,您正在使用TabActivity,但第一次設置內容視圖時,您沒有tabhost(位於R.layout.logoscreen文件中)。您可以將tabhost添加到您的徽標佈局(它不會受到傷害),它應該可以工作。或者,您可以將代碼拆分爲兩個活動,並在AsyncTask完成後啓動新活動。