SwipeRefreshLayout - 尝试在空对象引用上调用虚拟方法

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

我尝试通过使用 SwipeRefreshLayout

下拉刷新内容来实现一个 ListFragment

我的密码是

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

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

我的XML是

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

但我总是得到错误

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

看起来 view.findViewById(R.id.srl) returns 无效。

有人有想法怎么了?

编辑:

我的 OnCrateView

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


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


    return rootView;
}

现在我得到这个错误

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

尝试将代码放在 onActivityCreated(...) 而不是 onViewCreated(..)

不要使用 SwipeRefreshLayout 作为父布局。它会起作用。 像这样使用

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

对我有用。 编码愉快:)