SwipeRefreshLayout -Attempt to invoke virtual method on a null object reference

Stevetro picture Stevetro · Apr 20, 2015 · Viewed 8.3k times · Source

I try to realize a ListFragment with refresh his content by pulldown with the SwipeRefreshLayout

My Code is

 public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    SwipeRefreshLayout srl = (SwipeRefreshLayout) view.findViewById(R.id.srl);
    srl.setOnRefreshListener(this);
}

My XML is

<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/srl"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff" >

    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</android.support.v4.widget.SwipeRefreshLayout>

But i always get the Error

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.widget.SwipeRefreshLayout.setOnRefreshListener(android.support.v4.widget.SwipeRefreshLayout$OnRefreshListener)' on a null object reference
        at info.androidhive.slidingmenu.fragments.EventsOverview_pull.onViewCreated(EventsOverview_pull.java:32)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:961)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:454)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:211)
        at android.app.ActivityThread.main(ActivityThread.java:5321)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1016)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)

it looks view.findViewById(R.id.srl) returns null.

Someone has a Idea whats wrong?

Edit:

My OnCrateView

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {


    View rootView = inflater.inflate(R.layout.pulldown, container, false);


    return rootView;
}

Now i get this Error

java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
        at android.support.v4.app.ListFragment.ensureList(ListFragment.java:344)
        at android.support.v4.app.ListFragment.onViewCreated(ListFragment.java:145)
        at info.androidhive.slidingmenu.fragments.EventsOverview_pull.onViewCreated(EventsOverview_pull.java:29)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:961)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:454)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:211)
        at android.app.ActivityThread.main(ActivityThread.java:5321)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1016)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)

Answer

Braj Bhushan Singh picture Braj Bhushan Singh · Dec 21, 2018

Do not use SwipeRefreshLayout as the parent layout. It will work. Use it like this

  <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/srl"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff" >

  <android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/srl"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</android.support.v4.widget.SwipeRefreshLayout>

</LinearLayout>

It works for me. Happy coding :)