是的,我意識到有非常類似於這個問題,但不像其他人,我的onCreateView()
方法膨脹的片段的看法。當我不使用findViewById()
,一切膨脹和預期我的應用程序工作(除了因爲我想在findViewById()
得到的意見的功能Android - 片段findViewById()總是空?
這裏是我的代碼:
@Override
public View onCreateView(LayoutInflater p_inflater, ViewGroup p_container, Bundle p_prev_state)
{
return p_inflater.inflate(R.layout.fragment_main, p_container, false);
}
每當我想我的佈局中訪問一個ListView,我這樣做:
private ListView getListView()
{
return (ListView)getView().findViewById(R.id.main_events);
}
,但這始終是空爲什麼我fragment_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:background="@color/white"
tools:context="me.k.cal.MainActivity$PlaceholderFragment" >
<ListView android:id="@+id/main_events"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:dividerHeight="1dp"
android:divider="@color/background_color" />
</RelativeLayout>
My full fragment code can be found here.
'findViewById'可以在片段的view..So被連接,你需要使用它作爲'view.findViewById' – 2014-10-28 05:33:58
那到底什麼時候你調用'getListView()'或調用它的方法,關於片段生命週期? – laalto 2014-10-28 06:23:14