是的,我意识到有非常类似于这个问题,但不像其他人,我的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