gpt4 book ai didi

java - Android tabhost错误空指针tabwidget

转载 作者:行者123 更新时间:2023-12-01 21:08:26 25 4
gpt4 key购买 nike

主要 Activity :

 public class MainActivity extends AppCompatActivity {
TabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
setupTab(new TextView(this), "Tab 1");
setupTab(new TextView(this), "Tab 2");
setupTab(new TextView(this), "Tab 3");

}
private void setupTab(final View view, final String tag) {
View tabview = createTabView(mTabHost.getContext(), tag);
TabHost.TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(new TabHost.TabContentFactory() {

public View createTabContent(String tag) {return view;}
});
mTabHost.addTab(setContent);
}
private static View createTabView(final Context context, final String text) {
View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
return view;
}

}

Activity 主

<?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">

<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">

</FrameLayout>
</LinearLayout>
</TabHost>

我正在尝试使用选项卡来制作没有 Material 设计库的底部导航栏。如何修复这个错误? (尝试在空对象引用上调用虚拟方法“void android.widget.TabWidget.setStripEnabled(boolean)”)

最佳答案

您需要调用 mTab​​Host.setup(...);

原因是您的 Activity 没有扩展 TabActivity,您应该在 TabHost.addTab(...) 方法之前调用 TabHost.setup(...) 。

image

关于java - Android tabhost错误空指针tabwidget,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41810244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com