0

I'm trying to create tabs based on an array returned by the database. However, I'm getting the following error.

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setVisibility(int)' on a null object reference

The code that I'm using is below, the array subAreas has already 3 values.

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

}

Also, could you give me tips to create a listView as TabContent.

Thank you

Tarcisiofl
  • 151
  • 4
  • 12
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – 323go Dec 18 '15 at 19:39
  • Not really, the android documentation would lead you to believe the above is the correct way to do this, but it weirdly results in a null pointer in generated code. It has to do with the fact that the documentation describes how to do this in an activity and he wants to do it in a fragment. – Jason Bray Dec 18 '15 at 19:40
  • "the android documentation would lead you to believe the above is the correct way to do this" -- link to an example, please? – 323go Dec 18 '15 at 20:47
  • I already got it working. And no one of the answers were correct. I only need now to fill the content of the tabs. – Tarcisiofl Dec 21 '15 at 14:21

1 Answers1

0

You can't inflate the view while there are no tabs in said view. It will attempt to set the first tab as what should be shown in the fragment and since you haven't added any tabs to the view, it results in a null pointer.

Try the following code inside onCreateView. Of course, you'll have to parameterize addTab to the values you want.

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;
Jason Bray
  • 502
  • 1
  • 4
  • 15