我尝试调整一个简单的适配器来填充我的Recycler cardview。我开始将firebase数据保存到一个ArrayList中,然后我将其转移到一个虚拟类(保存变量)。但是,当我运行应用程序时,它说
Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference...at
ArrayRecyclerAdapter.getItemCount(ArrayRecyclerAdapter.java:26)
我测试过以确保通过添加行
正确地保存/检索GlobalDatatest = ((GlobalData) getActivity().getApplication()).getMachineData();
所以我认为适配器有问题,但我不知道它是什么。 感谢
card.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:padding="16dp"
>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cv"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/machine_name"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="16dp"
android:textSize="30sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/machine_info"
android:layout_below="@+id/machine_name"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
main .xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/machinie_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="@string/bar_machine"
android:textAlignment="center"
android:textSize="50sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"/>
</RelativeLayout>
</LinearLayout>
适配器.java
public class ArrayRecyclerAdapter extends
RecyclerView.Adapter<ArrayRecyclerAdapter.mViewHolder> {
private ArrayList<MachineClass> machineClassList;
public ArrayRecyclerAdapter(ArrayList<MachineClass> mList) {
this.machineClassList = mList;
}
@Override
public int getItemCount() {
return machineClassList.size();
}
@Override
public void onBindViewHolder(mViewHolder mViewHolder, int i) {
MachineClass ci = machineClassList.get(i);
mViewHolder.vName.setText(ci.getmNAME());
mViewHolder.vSurname.setText(ci.getmSTATUS());
}
@Override
public mViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.
from(viewGroup.getContext()).
inflate(R.layout.machine_card, viewGroup, false);
return new mViewHolder(itemView);
}
public static class mViewHolder extends RecyclerView.ViewHolder {
protected TextView vName;
protected TextView vSurname;
public mViewHolder(View v) {
super(v);
vName = (TextView) v.findViewById(R.id.machine_name);
vSurname = (TextView) v.findViewById(R.id.machine_info);
}
}
}
main.java
public class MachinePage extends Fragment {
public MachinePage() {
// Required empty public constructor
}
public static MachinePage newInstance() {
MachinePage fragment = new MachinePage();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_machine_page, container, false);
RecyclerView recyclerView = (RecyclerView)rootView.findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(llm);
FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference myRef = database.getReference("RESOURCES/MACHINES");
final ArrayList<MachineClass> machineList = new ArrayList<>();
ArrayList<MachineClass> test;
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot mchlist: dataSnapshot.getChildren()){
MachineClass mach = mchlist.getValue(MachineClass.class);
machineList.add(mach);
}
((GlobalData) getActivity().getApplication()).setMachineData(machineList);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
test = ((GlobalData) getActivity().getApplication()).getMachineData();
ArrayRecyclerAdapter adapter = new ArrayRecyclerAdapter(test);
recyclerView.setAdapter(adapter);
return rootView;
}
}
和GlobalData.java
public class GlobalData extends Application {
//saves user ID globally + get user specific path
private String UserID;
private Tag UserTag;
private String UserPath;
private ArrayList<MachineClass> MachineData;
public String getUserID() {
UserID = toHexString(UserTag.getId());
return UserID;
}
public String getUserPath(){
UserPath = "USERS/" + UserID + "/";
return UserPath;
}
public void setUserTag(Tag tag) {
this.UserTag = tag;
}
public static String toHexString(byte[] bytes) {
char[] hexArray = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
char[] hexChars = new char[bytes.length * 2];
int v;
for (int j = 0; j < bytes.length; j++) {
v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v / 16];
hexChars[j * 2 + 1] = hexArray[v % 16];
}
return new String(hexChars);
}
//end of save user ID
public ArrayList<MachineClass> getMachineData() {
return MachineData;
}
public ArrayList setMachineData(ArrayList<MachineClass> machineData) {
return MachineData = machineData;
}
}
答案 0 :(得分:0)
尝试这样做。首先初始化测试,如
List<MachineClass> test = new ArrayList<>();
然后,在测试时分配值时执行此操作。
test.addAll(((GlobalData) getActivity().getApplication()).getMachineData());
答案 1 :(得分:0)
您应该检查,如果OnDayClickedListener
中的persianCalendarView
为空,如果null返回0,则传递null ArrayList,检查片段中是否正确填充了Arraylist
getItemCount()
答案 2 :(得分:0)