更新
我的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>
如何修复我的代码呢?此代码用于在应用开始之前显示徽标。
答案 0 :(得分:1)
this
中的 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();
}
};