2015-04-20 69 views
2

我試圖實現與下拉與SwipeRefreshLayout刷新他的內容ListFragmentSwipeRefreshLayout -Attempt調用虛擬方法在空對象引用

我的代碼是

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

它看起來view.findViewById(R.id.srl)返回null。

有人有一個想法什麼是錯的?

編輯:

我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$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) 
+0

清理項目和重建一切。如果在源代碼級別看起來都正確,可能編譯的內容不同步。 –

+1

發佈你的'oncreateView' – SilentKnight

+0

如果你真的在使用'ListFragment',那麼你的'ListView'必須有'android.R.id.list'。其次,你必須膨脹並在'onCreateView'中返回你的自定義佈局。 –

回答

0

嘗試把代碼onActivityCreated(...)而不是onViewCreated(..)

+1

一些解釋會有所幫助 – Ian

相關問題