为什么在尝试在 android 中设置 tabspec 的内容时出现错误? [英] Why do I get an error while trying to set the content of a tabspec in android?

查看:20
本文介绍了为什么在尝试在 android 中设置 tabspec 的内容时出现错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用选项卡的 android 活动.

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/layout/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?

推荐答案

虽然 Activity.setContentView 需要一个布局的 id,TabSpec.setContent 接受一个视图的 id.这意味着您需要传递一个看起来像 R.id.something 而不是 R.layout.something 的 id.

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天全站免登陆