2012-02-25 29 views
0

已更新使用AsyncTask无法在Android上找到ViewById

我有一个xml文件和我的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' 

在我的代码是:

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> 

我该如何解决我的代码,它会工作?此代码在应用程序启动之前显示徽标。

回答

1

this in onPostExecute表示AsyncTask类型的对象。如果您创建的AsyncTask就地,您必须使用this为外类型,像这样:

class MyActivity : extends Activity { 
    // ... 

    void foo() { 
    my asyncTask = new AsyncTask<Void, Void, Int >() { 
     // ... 

     protected void onPostExecute(Integer result) { 
     MyActivity.this.setContentView(R.layout.tabs); 
     tabs = (TabHost) MyActivity.this.findViewById(android.R.id.tabhost); 
     } 
    }; 
    asyncTask.execute(); 
    } 
}; 
+1

我更新我的职务问题,请看看吧 – 2012-02-25 17:29:22

相关问题