为什么在 Android 中尝试设置 tabspec 的内容时会出现错误?

发布于 2024-09-04 06:32:38 字数 2212 浏览 5 评论 0原文

我有一个 Android 活动,其中使用了选项卡。

public class UnitActivity extends TabActivity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.unit_view);

    TabHost tabHost = getTabHost();
    TabSpec spec;

    spec = tabHost.newTabSpec("controls");
    spec.setIndicator("Control");
    spec.setContent(R.layout.unit_control);
    tabHost.addTab(spec);

    spec = tabHost.newTabSpec("data");
    spec.setIndicator("Data");
    spec.setContent(R.layout.unit_data);
    tabHost.addTab(spec);
  }
}

但是,当我运行该程序时,它崩溃并出现错误: “无法创建选项卡内容,因为找不到 ID 为 2130903042 的视图”。我不明白问题是什么,因为

<?xml version="1.0" encoding="utf-8"?>
  <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">
    <TableLayout
        android:stretchColumns="*"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    <Spinner android:id="@+id/unit_num"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:prompt="@string/choose_unit"/>
    <TableRow android:padding="2dp">
      <TextView
          android:gravity="right"
          android:padding="5dp"
          android:text="@string/Power"/>
      <TextView android:id="@+id/unit_power"
          android:layout_span="3"
          android:gravity="center"
          android:padding="5dp"
          android:background="@android:color/white"
          android:textColor="@android:color/black"
          android:text="AUTO"/>
    </TableRow>
    ...
  </TableLayout>
</ScrollView>

据我所知, R.layout.unit_data 引用了我的资源目录(res/layout/unit_data.xml)中的布局文件,unit_data.xml 格式良好,我什至在另一个活动中成功引用它

class UnitData extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.unit_data);
    Toast.makeText(this, "Hi from UnitData.onCreate", 5);
  }
}

,该活动没有给出错误并且渲染布局很好。

这是怎么回事?为什么我在创建选项卡时无法引用此布局?

I have an android activity in which I'm using tabs.

public class UnitActivity extends TabActivity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.unit_view);

    TabHost tabHost = getTabHost();
    TabSpec spec;

    spec = tabHost.newTabSpec("controls");
    spec.setIndicator("Control");
    spec.setContent(R.layout.unit_control);
    tabHost.addTab(spec);

    spec = tabHost.newTabSpec("data");
    spec.setIndicator("Data");
    spec.setContent(R.layout.unit_data);
    tabHost.addTab(spec);
  }
}

However when I run the program it crashes with the error:
"Could not create tab content because could not find view with id 2130903042". I don't understand what the problem is because R.layout.unit_data refers to a layout file in my resource directory (res/layout/unit_data.xml)

<?xml version="1.0" encoding="utf-8"?>
  <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">
    <TableLayout
        android:stretchColumns="*"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    <Spinner android:id="@+id/unit_num"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:prompt="@string/choose_unit"/>
    <TableRow android:padding="2dp">
      <TextView
          android:gravity="right"
          android:padding="5dp"
          android:text="@string/Power"/>
      <TextView android:id="@+id/unit_power"
          android:layout_span="3"
          android:gravity="center"
          android:padding="5dp"
          android:background="@android:color/white"
          android:textColor="@android:color/black"
          android:text="AUTO"/>
    </TableRow>
    ...
  </TableLayout>
</ScrollView>

as far as I can tell unit_data.xml is well formed and I've even referenced it successfully in another activity

class UnitData extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.unit_data);
    Toast.makeText(this, "Hi from UnitData.onCreate", 5);
  }
}

which does not give an error and renders the layout just fine.

What's going on? Why can't I reference this layout when creating a tab?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

魂ガ小子 2024-09-11 06:32:38

Activity.setContentView 的 id 为布局, TabSpec.setContent 采用视图的 id。这意味着您需要传递一个类似于 R.id.something 而不是 R.layout.something 的 id。

要解决您的特定问题,请为布局中的顶级视图提供视图 ID:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:id="@+id/unit_data">  <!-- NOTE THE CHANGE -->
  ...
</ScrollView>

并更新源:

public class UnitActivity extends TabActivity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.unit_view);

    TabHost tabHost = getTabHost();
    TabSpec spec;

    spec = tabHost.newTabSpec("controls");
    spec.setIndicator("Control");
    spec.setContent(R.id.unit_control);  // NOTE THE CHANGE
    tabHost.addTab(spec);

    spec = tabHost.newTabSpec("data");
    spec.setIndicator("Data");
    spec.setContent(R.id.unit_data);   // NOTE THE CHANGE
    tabHost.addTab(spec);
  }
}

有关详细信息,请参阅 ApiDemos

While Activity.setContentView takes an id of a Layout, TabSpec.setContent takes an id of a View. This means you need to pass an id that looks like R.id.something and not R.layout.something.

To solve your particular problem, give the top level view in your layout a view id:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:id="@+id/unit_data">  <!-- NOTE THE CHANGE -->
  ...
</ScrollView>

and update your source:

public class UnitActivity extends TabActivity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.unit_view);

    TabHost tabHost = getTabHost();
    TabSpec spec;

    spec = tabHost.newTabSpec("controls");
    spec.setIndicator("Control");
    spec.setContent(R.id.unit_control);  // NOTE THE CHANGE
    tabHost.addTab(spec);

    spec = tabHost.newTabSpec("data");
    spec.setIndicator("Data");
    spec.setContent(R.id.unit_data);   // NOTE THE CHANGE
    tabHost.addTab(spec);
  }
}

For more information, see the tab examples in the ApiDemos:

停顿的约定 2024-09-11 06:32:38

如果您的选项卡布局位于不同的文件中,则必须扩充 XML。

spec = tabHost.newTabSpec("data");
spec.setIndicator("Data");
// Add the layout to your tab view
getLayoutInflater().inflate(R.layout.unit_data, tabHost.getTabContentView(), true);
spec.setContent(R.id.unit_data);   
tabHost.addTab(spec);

If you have your tab layout in a different file, you have to inflate the XML.

spec = tabHost.newTabSpec("data");
spec.setIndicator("Data");
// Add the layout to your tab view
getLayoutInflater().inflate(R.layout.unit_data, tabHost.getTabContentView(), true);
spec.setContent(R.id.unit_data);   
tabHost.addTab(spec);
可爱咩 2024-09-11 06:32:38

您还必须在 TabHost

LayoutInflater.from(this).inflate(R.layout.unit_data, tabHost.getTabContentView(), true);

之后使用 LayoutInflater我也被这个问题困扰了,最后终于搞清楚了。

You also have to use LayoutInflater after TabHost

LayoutInflater.from(this).inflate(R.layout.unit_data, tabHost.getTabContentView(), true);

I also got stuck with this and finally figure it out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文