我正在尝试在Cloud Firestore中的集合内的文档字段内添加多个卷号(唯一)。我要如何实现它,还要限制用户输入的值不能超过5个。我可以在这里得到一些帮助吗?
Groupformation_fragment.java
public class groupformation_fragment extends Fragment {
private Button submit;
EditText ET1,ET2,ET3,ET4,ET5;
private FirebaseFirestore mfirestore;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v= inflater.inflate(R.layout.fragment_groupformation,null);
ET1=(EditText)v.findViewById(R.id.ET1);
ET2=(EditText)v.findViewById(R.id.ET2);
ET3=(EditText)v.findViewById(R.id.ET3);
ET4=(EditText)v.findViewById(R.id.ET4);
ET5=(EditText)v.findViewById(R.id.ET5);
submit=(Button)v.findViewById(R.id.submit);
return v;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String roll1=ET1.getText().toString();
String roll2=ET2.getText().toString();
String roll3=ET3.getText().toString();
String roll4=ET4.getText().toString();
String roll5=ET5.getText().toString();
Map<String,String> usermap= new HashMap<>();
usermap.put("Student1",roll1);
usermap.put("Student2",roll2);
usermap.put("Student3",roll3);
usermap.put("Student4",roll4);
usermap.put("Student5",roll5);
mfirestore.collection("group").add(usermap).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
Toast.makeText(getActivity(), "Group Has been Registered For The Project", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
String error=e.getMessage();
Toast.makeText(getActivity(), "Error :"+error, Toast.LENGTH_SHORT).show();
}
});
}
});
}
}
fragement_groupformation.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:background="@drawable/bkg3"
android:layout_height="match_parent">
<EditText
android:id="@+id/ET1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Roll number"
android:layout_marginTop="50dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:inputType="number"
android:textColorHint="@color/common_google_signin_btn_text_dark_default"/>
<EditText
android:id="@+id/ET2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Roll number"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:inputType="number"
android:textColorHint="@color/common_google_signin_btn_text_dark_default" />
<EditText
android:id="@+id/ET3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Roll number"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:inputType="number"
android:textColorHint="@color/common_google_signin_btn_text_dark_default" />
<EditText
android:id="@+id/ET4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Roll number"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:inputType="number"
android:textColorHint="@color/common_google_signin_btn_text_dark_default" />
<EditText
android:id="@+id/ET5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Roll number"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:inputType="number"
android:textColorHint="@color/common_google_signin_btn_text_dark_default" />
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:layout_marginTop="40dp"
android:background="@color/common_google_signin_btn_text_dark_default"
android:text="Submit"/>
</LinearLayout>
显示Logcat错误
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.kiit.projectmanager, PID: 7163
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.firestore.CollectionReference com.google.firebase.firestore.FirebaseFirestore.collection(java.lang.String)' on a null object reference
at com.kiit.projectmanager.groupformation_fragment$1.onClick(groupformation_fragment.java:62)
at android.view.View.performClick(View.java:5215)
at android.view.View$PerformClick.run(View.java:21196)
at android.os.Handler.handleCallback(Handler.java:742)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5603)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
I/Process: Sending signal. PID: 7163 SIG: 9
Application terminated.
答案 0 :(得分:0)
在NullPointerException
里面,这意味着您没有在代码中的任何地方初始化mfirestore
对象。要解决此问题,请更改以下代码行:
private FirebaseFirestore mfirestore;
到
private FirebaseFirestore mfirestore = FirebaseFirestore.getInstance();