Whenever I run this code and go to change to my lesson fragment (code below), I get a fatal exception 'java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference'. This must be something to do with the fragment as if I run this code in a single activity it works fine. The error comes when I reference RelativeLayout and try to findViewById(R.id.rl), however android studio itself does recognise that it is there as it is highlighted purple.
LessonFragment.class:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/* Set the layout to use the fragment_lesson file */
View rootView = inflater.inflate(R.layout.fragment_lesson, container, false);
//Find and initalise the buttons on the page
nextBtn = (ImageButton) rootView.findViewById(R.id.NextButton);
previousBtn = (ImageButton) rootView.findViewById(R.id.PreviousButton);
nextBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Simply change the lessonNumber to the next lesson, or to the first lesson if the user is on the last one.
//This creates a loop effect so the user can not run over the lessons.
//------ THIS NEEDS TO CHANGE SO THAT THEY GET THE PUBLIC LESSON NUMBER (PERHAPS FROM USER CLASS)
if (lessonNumber == 10) {
lessonNumber = 0;
}
nextLesson();
createPage(lessonNumber);
}
});
previousBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Simply change the lessonNumber to the previous lesson, or to the last lesson if the user is on the first one.
//This creates a loop effect so the user can not run over the lessons.
//------ THIS NEEDS TO CHANGE SO THAT THEY GET THE PUBLIC LESSON NUMBER (PERHAPS FROM USER CLASS)
if (lessonNumber == 1) {
lessonNumber = 10;
}
previousLesson();
createPage(lessonNumber);
}
});
//Find and reference the layouts that we will be adding our text to
RelativeLayout rl=(RelativeLayout) getView().findViewById(R.id.rl);
ScrollView sv = (ScrollView)getView().findViewById(R.id.sl);
LinearLayout ll = (LinearLayout)getView().findViewById(R.id.ll);
createPage(1);
return rootView;
}
lesson_xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rl"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/nav_header_height"
android:paddingBottom="@dimen/activity_vertical_margin" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/sl"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_above="@+id/PreviousButton" >
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ll" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/HTMLTitle"
android:id="@+id/LessonTitle"
android:textColor="#0d9445"
android:textSize="30sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/Lorem"
android:id="@+id/p1"
android:layout_marginTop="74dp" />
</LinearLayout>
</ScrollView>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/PreviousButton"
android:src="@drawable/previous"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:theme="@android:style/Holo.ButtonBar"
android:contentDescription="@string/buttonName" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/NextButton"
android:src="@drawable/next"
android:theme="@android:style/Holo.ButtonBar"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:contentDescription="@string/buttonName2" />
</RelativeLayout>