使用EmptyView nullPointerException的SwipeRefreshLayout

时间:2014-07-14 17:12:16

标签: android android-listview nullpointerexception swiperefreshlayout

每当我向下滑动刷新时,我都会得到一个NullPointerException,但它不在我的代码中,而是在SwipeRefreshLayout的startRefresh()方法中。

07-14 11:55:06.714: E/AndroidRuntime(20484): java.lang.NullPointerException
07-14 11:55:06.714: E/AndroidRuntime(20484):    at android.support.v4.widget.SwipeRefreshLayout.startRefresh(SwipeRefreshLayout.java:441)
07-14 11:55:06.714: E/AndroidRuntime(20484):    at android.support.v4.widget.SwipeRefreshLayout.onTouchEvent(SwipeRefreshLayout.java:399)
07-14 11:55:06.714: E/AndroidRuntime(20484):    at android.view.View.dispatchTouchEvent(View.java:7253)
07-14 11:55:06.714: E/AndroidRuntime(20484):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2168)
07-14 11:55:06.714: E/AndroidRuntime(20484):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1903)
07-14 11:55:06.714: E/AndroidRuntime(20484):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
07-14 11:55:06.714: E/AndroidRuntime(20484):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
07-14 11:55:06.714: E/AndroidRuntime(20484):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
07-14 11:55:06.714: E/AndroidRuntime(20484):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
07-14 11:55:06.714: E/AndroidRuntime(20484):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
07-14 11:55:06.714: E/AndroidRuntime(20484):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
07-14 11:55:06.714: E/AndroidRuntime(20484):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
07-14 11:55:06.714: E/AndroidRuntime(20484):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
07-14 11:55:06.714: E/AndroidRuntime(20484):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2215)
07-14 11:55:06.714: E/AndroidRuntime(20484):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1458)
07-14 11:55:06.714: E/AndroidRuntime(20484):    at android.app.Activity.dispatchTouchEvent(Activity.java:2410)
07-14 11:55:06.714: E/AndroidRuntime(20484):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2163)
07-14 11:55:06.714: E/AndroidRuntime(20484):    at android.view.View.dispatchPointerEvent(View.java:7433)
07-14 11:55:06.714: E/AndroidRuntime(20484):    at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220)
07-14 11:55:06.714: E/AndroidRuntime(20484):    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165)
07-14 11:55:06.714: E/AndroidRuntime(20484):    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4292)
07-14 11:55:06.714: E/AndroidRuntime(20484):    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4271)
07-14 11:55:06.714: E/AndroidRuntime(20484):    at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4363)
07-14 11:55:06.714: E/AndroidRuntime(20484):    at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
07-14 11:55:06.714: E/AndroidRuntime(20484):    at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
07-14 11:55:06.714: E/AndroidRuntime(20484):    at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:171)
07-14 11:55:06.714: E/AndroidRuntime(20484):    at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:4342)
07-14 11:55:06.714: E/AndroidRuntime(20484):    at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:4382)
07-14 11:55:06.714: E/AndroidRuntime(20484):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
07-14 11:55:06.714: E/AndroidRuntime(20484):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
07-14 11:55:06.714: E/AndroidRuntime(20484):    at android.view.Choreographer.doFrame(Choreographer.java:530)
07-14 11:55:06.714: E/AndroidRuntime(20484):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
07-14 11:55:06.714: E/AndroidRuntime(20484):    at android.os.Handler.handleCallback(Handler.java:725)
07-14 11:55:06.714: E/AndroidRuntime(20484):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-14 11:55:06.714: E/AndroidRuntime(20484):    at android.os.Looper.loop(Looper.java:137)
07-14 11:55:06.714: E/AndroidRuntime(20484):    at android.app.ActivityThread.main(ActivityThread.java:5227)
07-14 11:55:06.714: E/AndroidRuntime(20484):    at java.lang.reflect.Method.invokeNative(Native Method)
07-14 11:55:06.714: E/AndroidRuntime(20484):    at java.lang.reflect.Method.invoke(Method.java:511)
07-14 11:55:06.714: E/AndroidRuntime(20484):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
07-14 11:55:06.714: E/AndroidRuntime(20484):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
07-14 11:55:06.714: E/AndroidRuntime(20484):    at dalvik.system.NativeStart.main(Native Method)

`

这是我的onCreate和onRefresh我的主要活动,MonitoredCoursesActivity:

public class MonitoredCoursesActivity extends ListActivity implements OnRefreshListener {

private SwipeRefreshLayout swipeLayout;
private ArrayList<Course> monitored;
private String lastUpdate;
private ListView myList;
private MyListAdapter myListAdapter;
final Context context = this;
//  private int freqUpdate;
//  private String updateTime;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (loadState()==null) {
        monitored = new ArrayList<Course>();
    }
    myList=(ListView)findViewById(android.R.id.list);
    myListAdapter = new MyListAdapter(context, R.layout.row, monitored);
    View empty = findViewById(android.R.id.empty);
    myList.setEmptyView(empty);
    myList.setAdapter(myListAdapter);
    myList.setLongClickable(true);
    registerForContextMenu(myList);

    swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
    swipeLayout.setOnRefreshListener(this);
    swipeLayout.setColorScheme(android.R.color.holo_blue_bright, 
            android.R.color.holo_green_light, 
            android.R.color.holo_orange_light, 
            android.R.color.holo_red_light);
    swipeLayout.setHapticFeedbackEnabled(true);
@Override
public void onRefresh() {
    new Handler().postDelayed(new Runnable() {
        @Override public void run() {
            //Refresh all courses in the list
            updateCoursesInfo();
            swipeLayout.setRefreshing(false);
        }
    }, 10000);
}

我的activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<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>

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

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbarStyle="outsideOverlay" >

        <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|center_horizontal"
            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" />
    </ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>

我的ListView项目的row.xml:

`         

     <GridLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:columnCount="2" >

         <TextView
             android:id="@+id/txtCourseName"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_gravity="left"
             android:layout_marginBottom="2dp"
             android:paddingRight="5dp"
             android:text="@string/section"
             android:textSize="25sp" />

         <TextView
             android:id="@+id/txtSection"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_column="1"
             android:layout_gravity="center"
             android:layout_row="0"
             android:text="@string/section"
             android:textAppearance="?android:attr/textAppearanceMedium" />
     </GridLayout>

     <TextView
         android:id="@+id/txtRemaining"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_marginRight="68dp"
         android:text="@string/remaining"
         android:textAppearance="?android:attr/textAppearanceMedium" />

     <TextView
         android:id="@+id/txtCRN"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="@string/crn"
         android:textAppearance="?android:attr/textAppearanceMedium" />

     <TextView
         android:id="@+id/txtFecha"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_gravity="center_horizontal|top"
         android:textAppearance="?android:attr/textAppearanceSmall" />
 </LinearLayout>`

我搜索了this是唯一类似的问题,但解决方案并不适用于我的情况(或者至少我认为是这样)。有线索吗?提前谢谢。

2 个答案:

答案 0 :(得分:1)

你需要

context = this;
onCreate

中的

myListAdapter = new MyListAdapter(context, R.layout.row, monitored);// here you are using context
创建活动后

Context可用

第441行是

mListener.onRefresh(); 

查看来源

public void setOnRefreshListener(OnRefreshListener listener) {
    mListener = listener;
}

您是否已在活动中正确实施OnRefreshListener

正如聊天中所述

删除第二个SwipeRefreshLayout

要将空视图设置为listview,请阅读以下内容,而不是使用其他swiperefreshlayout

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

答案 1 :(得分:1)

所以,经过与用户Raghunandan的长谈,他指出我的xml有两个不同的SwipeRefreshLayouts。使用此解决方法是为了在ListView上正确设置EmptyView(如onCreate()方法所示),但都是垃圾。 EmptyView工作,但其中一个SwipeRefreshLayouts有一个null onRefreshListener(当然是一个用于EmptyView)。这自然导致了NullPointerException。每一段代码都是一样的。我唯一需要改变的是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" />

注意:如果您的ContextMenus出现问题,即使在正确声明之后,也应该修复它。

小心布局!