我正在尝试根据数据库返回的数组创建标签。但是,我收到以下错误。
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setVisibility(int)' on a null object reference
我正在使用的代码如下,数组subAreas已有3个值。
public class ExtintorFragment extends Fragment {
private LinearLayout ll;
private TabHost tabs;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ll = (LinearLayout) inflater.inflate(R.layout.fragment_criterios, container, false);
setHasOptionsMenu(true);
tabs = (TabHost) ll.findViewById(R.id.tabHost);
tabs.setup();
String[] subAreas;
EquipamentosSubAreaDbAdapter mDb = new EquipamentosSubAreaDbAdapter(getContext());
mDb.open();
subAreas = mDb.getSubareasEquipamento("Extintores");
mDb.close();
for (int i = 0; i < subAreas.length; i++) {
TabHost.TabSpec spec = tabs.newTabSpec(subAreas[i]);
spec.setIndicator(subAreas[i]);
spec.setContent(new TabHost.TabContentFactory() {
@Override
public View createTabContent(String tag) {
return null;
}
});
tabs.addTab(spec);
}
return ll;
}
@Override
public void onActivityCreated (Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
}
}
另外,你能否给我一些提示,将listView创建为TabContent。
谢谢
答案 0 :(得分:0)
当所述视图中没有制表符时,您无法对视图进行充气。它将尝试将第一个选项卡设置为片段中应显示的内容,并且由于您尚未向视图添加任何选项卡,因此会生成空指针。
在onCreateView中尝试以下代码。当然,您必须将addTab参数化为您想要的值。
mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity(), getChildFragmentManager(), android.R.id.tabcontent);
mTabHost.addTab(
mTabHost.newTabSpec("Tab 1").setIndicator("Tab 1")
, DefaultContentFragment.class
, null);
mTabHost.addTab(
mTabHost.newTabSpec("Tab 2").setIndicator("Tab 2")
, DefaultContentFragment.class
, null);
mTabHost.addTab(
mTabHost.newTabSpec("Tab 3").setIndicator("Tab 3")
, DefaultContentFragment.class
, null);
return mTabHost;