Search code examples
androidandroid-fragmentsfindviewbyid

findviewbyid returns null in fragment even though view inflated properly


I know there are alot of these cases, but my problem is that i did everything right (as much as I know) but the findviewbyid method still returns null

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

This is the stacktrace

rootView = (RelativeLayout) inflater.inflate(R.layout.activity_maps, container, false);

if (container == null) {
    return null;
}


mFloatingActionButton = (FloatingActionButton) rootView.findViewById(R.id.fab);
Logoutbutton = (Button) rootView.findViewById(R.id.button);


Logoutbutton.setOnClickListener(this);
mFloatingActionButton.setOnClickListener(this);


return rootView;}

the .setonclicklistener throws the error since the buttons are null

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    tools:context=".Fragments.MapsFragment">

<com.google.android.gms.maps.MapView
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="30dp"
        android:clickable="true"
        android:src="@drawable/ic_camera_alt_black_24dp"
        android:layout_gravity="end|bottom"
      />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Log Out"
        android:id="@+id/button"
        android:layout_marginTop="17dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginStart="17dp" />

</com.google.android.gms.maps.MapView>
</RelativeLayout>

and these are the buttons

E/UncaughtException: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                                             at com.example.sanchez.worldgramproject.Fragments.MapsFragment.onViewCreated(MapsFragment.java:224)
                                                                                             at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1315)
                                                                                             at android.support.v4.app.FragmentManagerImpl.moveFragmentsToInvisible(FragmentManager.java:2323)
                                                                                             at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2136)
                                                                                             at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2092)
                                                                                             at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1998)
                                                                                             at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:709)
                                                                                             at android.os.Handler.handleCallback(Handler.java:739)
                                                                                             at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                             at android.os.Looper.loop(Looper.java:158)
                                                                                             at android.app.ActivityThread.main(ActivityThread.java:7229)
                                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
01-31 21:33:00.746 14460-14939/com.example.sanchez.worldgramproject I/qtaguid: Tagging socket 91 with tag 3000110100000000{805310721,0} uid -1, pid: 14460, getuid(): 10248
01-31 21:33:00.876 14460-14939/com.example.sanchez.worldgramproject I/qtaguid: Untagging socket 77
01-31 21:33:00.966 14460-14460/com.example.sanchez.worldgramproject E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                      Process: com.example.sanchez.worldgramproject, PID: 14460
                                                                                      java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                                          at com.example.sanchez.worldgramproject.Fragments.MapsFragment.onViewCreated(MapsFragment.java:224)
                                                                                          at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1315)
                                                                                          at android.support.v4.app.FragmentManagerImpl.moveFragmentsToInvisible(FragmentManager.java:2323)
                                                                                          at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2136)
                                                                                          at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2092)
                                                                                          at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1998)
                                                                                          at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:709)
                                                                                          at android.os.Handler.handleCallback(Handler.java:739)
                                                                                          at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                          at android.os.Looper.loop(Looper.java:158)
                                                                                          at android.app.ActivityThread.main(ActivityThread.java:7229)
                                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

this is the stacktrace.

EDIT!

The problem has been found. It didnt work because the buttons arent necessarily under the "RelativeLayout" rootview but under the "MapsView" root. Since i was inflating the relativelayout, because of some reason it couldn't inflate the buttons.


Solution

  • You call super.onCreate in onCreateView. Those are wildly different methods, and definitely not correct. (You should NOT call super.onCreateView either though)

    Why can't container be null? Any specific reason? I've never seen this logic before, and don't know what it'll help (most likely nothing).

    This code will work:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = (RelativeLayout) inflater.inflate(R.layout.activity_maps, container, false);
    
        mFloatingActionButton = (FloatingActionButton) rootView.findViewById(R.id.fab);
        Logoutbutton = (Button) rootView.findViewById(R.id.button);
    
        Logoutbutton.setOnClickListener(this);
        mFloatingActionButton.setOnClickListener(this);
    
        return rootView;
    }
    

    EDIT:

    Try and read the stacktrace that you posted.

    This line clearly says it all:

    com.example.sanchez.worldgramproject.Fragments.MapsFragment.onViewCreated(MapsFragment.java:224)
    

    Your code is crashing in the "onViewCreated" method, not "onCreateView" (again, different methods).
    It even has a specific line number in the code for reference.

    Try and post your FULL Fragment class code, it's very hard to help you if you only give partial data.