我正在尝试动态显示特定的嵌套FrameLayout。但是,当我尝试访问片段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
答案 0 :(得分:2)
好的,我看到了问题。您想要访问行布局,但列表视图有此布局的多个版本,这意味着您无法从活动访问行布局,您必须在listadapter中执行此操作,或者在listadapter中为您的单行提供唯一ID,例如row.setId(row.getId + positionOfRow)
答案 1 :(得分:1)
LayoutInflater Inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = Inflater.inflate(R.layout.row_mallx, null);
FrameLayout flayout = (FrameLayout) view .findViewById(R.id.ldrawlayout);
flayout.setVisibility(View.VISIBLE);
在apiCompleted上尝试此代码
答案 2 :(得分:0)
您确定在apiCompleted()方法之前调用onCreateView()方法。
如果是,您应该能够访问代码中的任何位置,只要您持有对它的引用。
您可以添加一些日志以查看函数调用的顺序