1

I am trying to create a notepad app that has three tabs at the top, each linking to a different view.

The first two tabs will simply contain a form to fill out while the third will be where the actual writing is done. The problem is whenever I try to try to inflate this third view I am getting a null pointer exception for this line:

((LinearLayout) findViewById(R.id.drawRoot)).addView(v,0);

Here is my code:

public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, show the tab contents in the container
    Fragment fragment = new Section3Fragment();
    Bundle args = new Bundle();
    args.putInt(Section3Fragment.ARG_SECTION_NUMBER, tab.getPosition() + 1);
    fragment.setArguments(args);
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.container3, fragment)
            .commit();
}


public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}


public  class Section3Fragment extends Fragment {
    public Section3Fragment() {
    }

    int section;
    public static final String ARG_SECTION_NUMBER = "section_number";

    @Override
    public  View  onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        Bundle args = getArguments();
       section = args.getInt(ARG_SECTION_NUMBER);
       View view;

       if (section == 1)
       {
            view = inflater.inflate(R.layout.section3_page1, container,false);

           return view;
       }
       if (section == 2){

        view = inflater.inflate(R.layout.section3_page2, container, false);
        return view;
       }
       else {


            if(v != null)
                Log.v("not null", "not null");

            view = inflater.inflate(R.layout.section3_page3, container, false);
            ((LinearLayout) findViewById(R.id.drawRoot)).addView(v,0); //null pointer exception here!!


           return view;

       }
    }
}

The object v is an instance on the class I use to do the actual drawing.

And the section3_page3 layout file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawRoot"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>

Any insight into this issue is greatly appreciated.

1
  • Can you paste here the exception message, please? Also in your code we can't see where v is initialized. Commented Jul 9, 2012 at 15:49

3 Answers 3

2

At a quick glance, are you trying to do:

 ((LinearLayout) view.findViewById(R.id.drawRoot)).addView(v,0);

Your looking for the view on your fragment. not on the view you inflated within the if statement.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, that was my problem. Thank you. I would have never figured that one out
1

You posted section3_page3.xml but you open inflater.inflate(R.layout.section3_page2, container, false). Is this a typo or the root cause of your problem?

Opening the wrong XML file will cause findViewById() to return null here:

view = inflater.inflate(R.layout.section3_page2, container, false);
((LinearLayout) findViewById(R.id.drawRoot)).addView(v,0);

Hence a NullPointerException... I'm guessing you meant:

view = inflater.inflate(R.layout.section3_page3, container, false);
((LinearLayout) findViewById(R.id.drawRoot)).addView(v,0);

1 Comment

That was a typo, I apologize. it should have been R.layout.section3_page3
1

Since you're using the ActionBar, and Fragments, I suggest changing this:

((LinearLayout) findViewById(R.id.drawRoot)).addView(v,0);

into this:

((LinearLayout) getActivity().findViewById(R.id.drawRoot)).addView(v,0);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.