为什么在 Android 中尝试设置 tabspec 的内容时会出现错误?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
而 Activity.setContentView 的 id 为布局, TabSpec.setContent 采用视图的 id。这意味着您需要传递一个类似于 R.id.something 而不是 R.layout.something 的 id。
要解决您的特定问题,请为布局中的顶级视图提供视图 ID:
并更新源:
有关详细信息,请参阅 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 notR.layout.something
.To solve your particular problem, give the top level view in your layout a view id:
and update your source:
For more information, see the tab examples in the ApiDemos:
如果您的选项卡布局位于不同的文件中,则必须扩充 XML。
If you have your tab layout in a different file, you have to inflate the XML.
您还必须在 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.