我正在使用片段方法构建导航视图。在此片段中,我创建了一些显示视图的方法,例如,在微调器中单击值时,微调器将显示在回收视图中。但是问题出在适配器中,显示空对象引用。
public class LRPFragment extends Fragment {
private String mInstituteLocation = "";
private Spinner sp;
DatabaseReference db;
FirebaseHelper helper;
RecyclerView recyclerView;
LRPAdapter adapter;
List<Region> routesList;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
db = FirebaseDatabase.getInstance().getReference();
helper=new FirebaseHelper(db);
View RootView = inflater.inflate(R.layout.fragment_lrp, container, false);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView = RootView.findViewById(R.id.rv_rec_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(llm);
sp = (Spinner) RootView.findViewById(R.id.subreg);
loadRoute("Region");
return RootView;
}
private void loadRoute(final String referenceKey){
db = FirebaseDatabase.getInstance().getReference(referenceKey);
db.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
final List<String> subregionMark = new ArrayList<>();
subregionMark.add(0, "Choose Subregion");
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
for (DataSnapshot dataSnapshot1: snapshot.getChildren()){
subregionMark.add(dataSnapshot1.child("subregionMark").getValue(Region.class).getSubregionMark());//null object references on getSubregionMark
}
}
ArrayAdapter <String> subAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_dropdown_item, subregionMark);
subAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(subAdapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getActivity().setTitle("Menu LRPFragment");
}
}
这是构造函数
public class Region {
String name;
String start;
String end;
String distance;
String regionMark;
String subregionMark;
public String getStart() {
return start;
}
public void setStart(String start) {
this.start = start;
}
public String getEnd() {
return end;
}
public void setEnd(String end) {
this.end = end;
}
public String getDistance() {
return distance;
}
public void setDistance(String distance) {
this.distance = distance;
}
public String getSubregionMark() {
return subregionMark;
}
public void setSubregionMark(String subregionMark) {
this.subregionMark = subregionMark;
}
public Region() {
}
public String getName() {
return name;
}
public String getRegionMark() {
return regionMark;
}
public void setName(String name) {
this.name = name;
}
public void setRegionMark(String regionMark) {
this.regionMark = regionMark;
}
这是我称之为片段的布局
<?xml version="1.0" encoding="utf-8"?>
<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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar"
android:id="@+id/rel"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@id/tvreg"
android:layout_centerVertical="true"
android:layout_marginTop="-57dp"
android:text="LRP"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="25dp" />
<TextView
android:id="@+id/tvreg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="17dp"
android:layout_marginTop="49dp"
android:layout_marginEnd="23dp"
android:layout_toStartOf="@+id/regionMark"
android:text="REGION" />
<Spinner
android:id="@+id/regionMark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="47dp"
android:layout_marginEnd="13dp"
android:contextClickable="true"
android:entries="@array/regionMark"
android:spinnerMode="dropdown" />
<TextView
android:layout_width="113dp"
android:layout_height="wrap_content"
android:layout_below="@+id/tvreg"
android:layout_alignParentStart="true"
android:layout_marginStart="20dp"
android:layout_marginTop="17dp"
android:layout_marginEnd="20dp"
android:layout_toStartOf="@+id/subreg"
android:text="SUBREGION" />
<Spinner
android:id="@+id/subreg"
android:layout_width="230dp"
android:layout_height="wrap_content"
android:layout_below="@+id/regionMark"
android:layout_alignParentEnd="true"
android:layout_marginTop="13dp"
android:layout_marginEnd="20dp"
android:spinnerMode="dropdown"
android:visibility="visible" />
<TextView
android:id="@+id/or"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OR"
android:layout_below="@+id/subreg"
android:layout_centerInParent="true"/>
<SearchView
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:layout_centerInParent="true"
android:layout_below="@+id/or"
android:imeOptions="actionSearch"
android:inputType="textAutoComplete|textAutoCorrect"
android:textColor="#FFFFFF"
>
<requestFocus />
</SearchView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/filterTCBtn"
android:text="Filter"
android:layout_below="@+id/editText1"/>
</RelativeLayout>
<TextView
android:layout_below="@+id/rel"
android:id="@+id/route_view_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Route View"
android:textSize="20dp"
android:layout_centerHorizontal="true" />
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_rec_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/route_view_menu"
android:layout_marginTop="-2dp"
android:padding="3dp" />
</RelativeLayout>