因此,我试图在数据库片段中使用FirebaseUi作为数据库listView,我试图搜索与我在这里没有发现的问题相同的人--这是我的代码。
我尝试从另一个类引用它,它不像ex那样工作,创建一个新类,调用它FirebaseDatabase,并导入整个ui,没有任何错误,但是当我试图在片段中调用时,我会得到一个错误
public BlankFragment() {
// Required empty public constructor
}
private ListView mlistView;
public View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mlistView = (ListView) view.findViewById(R.id.listView);
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReferenceFromUrl("https://kalby-3428f.firebaseio.com/");
FirebaseListAdapter<String> firebaseListAdapter = new FirebaseListAdapter<String>(
this,
String.class,
android.R.layout.simple_list_item_1,
databaseReference
) {
@Override
protected void populateView(View v, String model, int position) {
}
};
return inflater.inflate(R.layout.fragment_blank, container, false);
}
and that's the XML Code`<FrameLayout 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"
tools:context="com.kalby.kalby.BlankFragment">
<!-- TODO: Update blank fragment layout -->
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</FrameLayout>这就是错误

发布于 2017-04-10 19:25:18
FirebaseListAdapter<String> firebaseListAdapter = new FirebaseListAdapter<String>(
this,
String.class,
android.R.layout.simple_list_item_1,
databaseReference
)在这里,"this“需要一个活动,而您是在片段中使用它,所以这里的"this”是片段而不是活动。
尝试使用"getActivity()“而不是"this",代码应该如下所示:
FirebaseListAdapter<String> firebaseListAdapter = new FirebaseListAdapter<String>(
getActivity(),
String.class,
android.R.layout.simple_list_item_1,
databaseReference
)试试看,根据你的屏幕截图,我认为这是可行的。
https://stackoverflow.com/questions/43330188
复制相似问题