0

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>
  • You are calling `getView()` before the View has been created, therefore it is null. To fix, do `final View rootView = inflater.inflate`... then use `rootView.findViewById` in the onClick methods. – OneCricketeer Apr 14 '16 at 15:18
  • @BobMalooga - That duplicate does not address **how** to solve this problem. – OneCricketeer Apr 14 '16 at 15:19
  • @cricket_007 thanks mate I appreciate your help. Do you have any idea what I would do if i needed to reference the layouts in the onCreate method as I need to generate the appropriate textViews when the page is created, but to do that I need to reference the layout that I am using currently. Is there a way to automatically call a method after onCreate? – Jack Pickering Apr 14 '16 at 15:46
  • I don't understand the question. Just call a method at the end of onCreate... – OneCricketeer Apr 14 '16 at 16:03
  • @cricket_007 That duplicate teaches **what** a NPE is and **how** to handle it. – Phantômaxx Apr 14 '16 at 16:07
  • @BobMalooga I understand the intent of the duplicate, but is it constructive to flag as such when there is more of a lack of understanding of the API usage? That duplicate doesn't answer why `getView()` would return null in `onCreate`, it only points out that it is – OneCricketeer Apr 14 '16 at 16:22
  • @cricket_007 You can vote to reopen the post. – Phantômaxx Apr 14 '16 at 16:24
  • @cricket_007 thats the problem see, if i call a method in onCreate then the view hasn't been created yet as onCreate doesn't finish, not to worry I have found an 'after onCreate' method that runs after so the page no longer crashes. Thanks for your help mate – Jack Pickering Apr 14 '16 at 17:07

0 Answers0