Skip to content Skip to sidebar Skip to footer

Swiperefreshlayout With Emptyview Nullpointerexception

Whenever I swipe down to refresh, I get a NullPointerException but it's not in my code, rather in the startRefresh() method from SwipeRefreshLayout. 07-14 11:55:06.714: E/AndroidRu

Solution 1:

You need to have

context = this;

in onCreate

In

myListAdapter = new MyListAdapter(context, R.layout.row, monitored);// here you are using context

Context is available once activity is created

Line 441 is

mListener.onRefresh(); 

Looking at the source

publicvoidsetOnRefreshListener(OnRefreshListener listener) {
    mListener = listener;
}

So have you implemented OnRefreshListener properly in your Activity?

As discussed in the chat

Remove the second SwipeRefreshLayout

To set a empty view to listview read the below instead of having the other swiperefreshlayout

http://cyrilmottier.com/2011/06/20/listview-tips-tricks-1-handle-emptiness/

Solution 2:

So, after a long talk with user Raghunandan here, he pointed out that my xml had two different SwipeRefreshLayouts. This workaround was used in order to properly set an EmptyView on the ListView (as shown on the onCreate() method), but was all rubbish. The EmptyView worked, but one of those SwipeRefreshLayouts had a null onRefreshListener (the one for the EmptyView, of course). THIS caused the NullPointerException, naturally. Every piece of code remains the same. The only thing I had to change was the activity_main.xml:

<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:text="@string/courses" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="2"
        android:cacheColorHint="#000000"
        android:divider="#D5DDDE"
        android:dividerHeight="1dp"
        android:drawSelectorOnTop="false"
        android:focusableInTouchMode="false"
        android:footerDividersEnabled="false"
        android:headerDividersEnabled="true"
        android:listSelector="@android:color/transparent" />
</android.support.v4.widget.SwipeRefreshLayout>

<TextView
    android:id="@android:id/empty"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical|center_horizontal"
    android:alpha="0.18"
    android:clickable="false"
    android:gravity="center|center_vertical"
    android:longClickable="false"
    android:text="@string/empty_list"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="@android:color/widget_edittext_dark"
    android:textSize="35sp"
    android:textStyle="bold"
    android:typeface="normal" />

Note: If you're having issues with ContextMenus not appearing, even after being correctly declared, this should fix it.

Be careful with your layouts!

Post a Comment for "Swiperefreshlayout With Emptyview Nullpointerexception"