Syed Raza Mehdi
Syed Raza Mehdi

Reputation: 4117

Null Pointer Exception when creation a fragment inside a fragment

I am creating fragments inside fragment but I got NullPointerException. I don't know why the same code is working in the class which extends the FragmentActivity but it's not working here in class which extends Fragment. Following is my code,

public class SecondLevel extends Fragment implements OnClickListener {
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        Button btn1, btn2, btn3, btn4;
        View view = inflater.inflate(R.layout.secondlevel, container, false);

        btn1 = (Button) view.findViewById(R.id.button11);
        btn1.setOnClickListener(this);

   ...
   ...
   ...
        return view;
    }

    @Override
    public void onClick(View view) {
        Fragment fr1 = null;
        switch (view.getId()) {
        case R.id.button11:
            Toast.makeText(this.getActivity(), "Button 1 is clicked!",
                    Toast.LENGTH_LONG).show();
            fr1 = new ThirdFragment();
            FragmentManager fm1 = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fm1.beginTransaction();
            fragmentTransaction.replace(R.id.fragment_place, fr1);
            fragmentTransaction.commit();
           .....

Now in the ThirdFragment what I am doing is as follows:

 package com.javacodegeeks.android.fragmentstest;

 import android.os.Bundle;
 import android.support.v4.app.Fragment;
 import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;

 public class ThirdFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    Log.e("F1", "F1");

    // Inflate the layout for this fragment

    return inflater.inflate(R.layout.thirdlevel, container, false);
}
}

Note that I have imported the same support libraries so it's not that problem.

Its my xml thirdlevel:

 <Button
    android:id="@+id/buttonlevel3"
     android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Button" />

 </LinearLayout>

Its my secondlevel xml:

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

 <TextView
     android:id="@+id/textView1"
     android:layout_width="wrap_content"
      android:layout_height="wrap_content"
     android:text="Level 1" />

 <Button
     android:id="@+id/button11"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:text="Button 1" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     android:text="Level 2" />

<Button
    android:id="@+id/button22"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Button 2" />

 <TextView
     android:id="@+id/textView33"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Level 3" />

<Button
    android:id="@+id/button33"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Button 3" />

 <TextView
     android:id="@+id/textView44"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Level 4" />

  <Button
    android:id="@+id/button44"
       android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:text="Button 4" />

   </LinearLayout>

LogCat:

E/AndroidRuntime(19842): FATAL EXCEPTION: main 
03-07 10:58:09.138: E/AndroidRuntime(19842): java.lang.NullPointerException 
03-07 10:58:09.138: E/AndroidRuntime(19842):at com.javacodegeeks.android.fragmentstest.SecondLevel.onClick(SecondLevel.java:50) 
03-07 10:58:09.138: E/AndroidRuntime(19842): at android.view.View.performClick(View.java:3549) 
E/AndroidRuntime(19842): at android.view.View$PerformClick.run(View.java:14393) 
E/AndroidRuntime(19842):at android.os.Handler.handleCallback(Handler.java:605) 
E/AndroidRuntime(19842):at android.os.Handler.dispatchMessage(Handler.java:92)

Upvotes: 0

Views: 277

Answers (1)

Piyush
Piyush

Reputation: 18933

Change this from

FragmentManager fm1 = getSupportFragmentManager();

to

FragmentManager fm1 = getFragmentManager();

Upvotes: 1

Related Questions