为什么我得到一个错误,而试图设置在Android的一则tabspec的内容? [英] Why do I get an error while trying to set the content of a tabspec in android?

查看:112
本文介绍了为什么我得到一个错误,而试图设置在Android的一则tabspec的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在哪,我使用的标签是机器人的活动。

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);
  }
}

然而,当我运行程序的时候,出现错误崩溃: 无法创建选项卡的内容,因为找不到视图ID 2130903042。我不明白是什么问题,因为R.layout.unit_data指的是一个布局文件在我的资源目录(RES /设计/ unit_data.xml)

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>

据我可以告诉unit_data.xml很好形成,我甚至引用它成功地在另一个活动

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?

推荐答案

在<一个href="http://developer.android.com/reference/android/app/Activity.html#setContentView%28int%29">Activity.setContentView需要布局的ID,<一个href="http://developer.android.com/reference/android/widget/TabHost.TabSpec.html#setContent%28int%29">TabSpec.setContent需要的视图的ID。这意味着你需要传递一个id,看起来像 R.id.something ,而不是 R.layout.something

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.

要解决的具体问题,给你的布局顶层视图视图id:

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>

和更新你的源代码:

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 标签的例子:

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

这篇关于为什么我得到一个错误,而试图设置在Android的一则tabspec的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆