Android getRecyclerView返回空对象
我试图从代码中为一个片段创建一个视图,我让它以标准方式膨胀。在我更改的片段的onCreateView中Android getRecyclerView返回空对象,android,layout,android-recyclerview,Android,Layout,Android Recyclerview,我试图从代码中为一个片段创建一个视图,我让它以标准方式膨胀。在我更改的片段的onCreateView中 View view = inflater.inflate(R.layout.list_layout, container, false); mDragListView = (DragListView) view.findViewById(dlid); //R.id.drag_list_view mDragListView.getRecyclerView().setVer
View view = inflater.inflate(R.layout.list_layout, container, false);
mDragListView = (DragListView) view.findViewById(dlid); //R.id.drag_list_view
mDragListView.getRecyclerView().setVerticalScrollBarEnabled(true);
到
在列表_布局为
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:id ="@+id/viewer_layout">
<com.woxthebox.draglistview.DragListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drag_list_view"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_height="match_parent"/>
在第二个版本中,它抛出声明的错误,即getRecyclerView()返回一个nullobject,它在第一个版本中工作
谢谢你的帮助,干杯
编辑:当我在ClassToBeImported.class中的“方法”中对布局进行充气时,是否可以通过加载布局?您没有调用
inflater.inflate(R.layout.list_layout, container, false);
在新版本中。没有它,所有布局元素都将为空
编辑:
由于已经有了对DragListView的引用,因此可以使用而不是调用
view.findViewById(dlid);
还可以从onCreateView
方法返回rl
EDIT2
如果您有DragListView
实现的本地副本,请尝试在构造函数中添加对onFinishInflate
的调用,以便它在从代码创建时也能工作
public DragListView(Context context) {
super(context);
onFinishInflate();
}
从
int-dlid=View.generateViewId()更改
toint-dlid=dlv.generateViewId()代码>我不想叫它,因为我不想使用列表布局。对不起,我不明白你在编辑中想说什么。我知道你说的关于返回rl的事。当我尝试在RecyclerView上调用某些内容时,会抛出错误。在DragListView
类中,RecyclerView在onFinishInflate
方法中实例化。因为在第二种情况下,布局没有从xml中膨胀,所以永远不会调用此方法。因此,RecyclerView仍然是null
ahh这是一个有充分根据的信息:)有没有其他方法可以启动onFinishInflate,而不从xml中膨胀它?我想不会。-@JacobusConradi尝试一下我在编辑中提到的方式。值得一试。
public DragListView(Context context) {
super(context);
onFinishInflate();
}