我想使一個特定的嵌套FrameLayout動態可見。但是,當我嘗試訪問我的片段的onCreateView()
方法之外的視圖時,我得到NullPointerException
。所以這是我的實現。訪問onCreateView()方法以外的視圖
private View myView;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View fragmentView = inflater.inflate(R.layout.listView, container, false);
this.myView = fragmentView;
...
return fragmentView;
}
@Override
public void apiCompleted(ApiResult apiResult, HttpRequest httpRequest) {
if(myLocationManager.hasLocation()){
FrameLayout flayout = (FrameLayout) myView.findViewById(R.id.ldrawlayout);
flayout.setVisibility(View.VISIBLE);
}
}
listView.xml
<LinearLayout 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/background_gray"
tools:context=".fragment.MallListFragment"
android:orientation="vertical">
<ListView
android:id="@+id/lv_malls"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
rowlist.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/row_mallx"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:paddingBottom="7dp">
<FrameLayout
android:id="@+id/ldrawlayout"
>
...
</FrameLayout>
</RelativeLayout>
logcat的(onCreateView方法被稱爲第一)
01-15 18:40:16.905 7633-7633/? V/onCreateView METHOD CALLED﹕ onCreateView
01-15 18:40:17.280 7633-7633/? V/APICOMPLETED METHOD CALLED﹕ apiCompleted
一切看起來不錯,但一旦嘗試getView()而不是myView ... – duggu
您不需要存儲對根佈局的引用。你可以調用Fragments'getView()'方法。但是這不會導致問題。請檢查R.layout.listView中是否存在ID ldrawlayout – aschattney
@aschattney R.layout.listView是一個ListView,因此此特定的framelayout不存在。它存在於另一個用作列表視圖行的xml中 – stud91