使用 AsyncTask 在 android 上找不到 ViewById

发布于 2025-01-08 18:21:24 字数 2690 浏览 2 评论 0原文

已更新

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世俗缘 2025-01-15 18:21:24

onPostExecute 中的 this 表示 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();
  }
};

this in onPostExecute means object of type AsyncTask. If you create AsyncTask inplace, you must use this for outer type, like 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();
  }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文