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.