使用 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>
我怎样才能修复我的代码使其能够工作?此代码用于在应用程序启动之前显示徽标。
updated
I have a problem with xml file and my AsyncTask.
My problem is that I can't get some errors on my code on
the error is
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'
where my code is:
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);
}
}
And my xml is:
<?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>
How can I fix my code that it will work? This code for displaying logo before the app starts.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
onPostExecute
中的this
表示 AsyncTask 类型的对象。如果就地创建 AsyncTask,则必须使用this
作为外部类型,如下所示:this
inonPostExecute
means object of type AsyncTask. If you create AsyncTask inplace, you must usethis
for outer type, like this: