Я создаю приложение для знакомств. Я кодировал ту часть, где движение влево или вправо регистрировало бы его в базе данных Firebase. Приложение работает и все, но теперь, когда я пытаюсь войти со страницы входа, приложение вылетает. Единственное, что я изменил до сбоев, было в MainActivty, поэтому я включаю это. Так как я новичок в этом, я действительно не знаю, как это исправить.
Ниже приводится MainActivity. java
package com.example.macdate;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.lorentzos.flingswipe.SwipeFlingAdapterView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private cards cards_data[];
private ArrayAdapter arrayAdapter;
private int i;
private FirebaseAuth mAuth;
private String currentUId;
private DatabaseReference usersDb;
ListView listView;
List<cards> rowItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
usersDb = FirebaseDatabase.getInstance().getReference().child("Users");
mAuth = FirebaseAuth.getInstance();
checkUserSex();
rowItems = new ArrayList<cards>();
arrayAdapter = new arrayAdapter(this, R.layout.item, rowItems);
SwipeFlingAdapterView flingContainer = (SwipeFlingAdapterView) findViewById(R.id.frame);
flingContainer.setAdapter(arrayAdapter);
flingContainer.setFlingListener(new SwipeFlingAdapterView.onFlingListener() {
@Override
public void removeFirstObjectInAdapter() {
// this is the simplest way to delete an object from the Adapter (/AdapterView)
Log.d("LIST", "removed object!");
rowItems.remove(0); // Every time the swipe is done completely the card is removed
arrayAdapter.notifyDataSetChanged();
}
@Override
public void onLeftCardExit(Object dataObject) {
//Do something on the left!
//You also have access to the original object.
//If you want to use it just cast it (String) dataObject
cards obj = (cards) dataObject;
String userId = obj.getUserId();
usersDb.child(oppositeUserSex).child(userId).child("connections").child("nope").child(currentUId).setValue(true); //registering the swipes to the database
Toast.makeText(MainActivity.this, "left", Toast.LENGTH_SHORT).show();
}
@Override
public void onRightCardExit(Object dataObject) {
cards obj = (cards) dataObject;
String userId = obj.getUserId();
usersDb.child(oppositeUserSex).child(userId).child("connections").child("yeps").child(currentUId).setValue(true);
Toast.makeText(MainActivity.this, "right", Toast.LENGTH_SHORT).show(); }
@Override
public void onAdapterAboutToEmpty(int itemsInAdapter) {
}
@Override
public void onScroll(float scrollProgressPercent) {
}
});
// Optionally add an OnItemClickListener
flingContainer.setOnItemClickListener(new SwipeFlingAdapterView.OnItemClickListener() {
@Override
public void onItemClicked(int itemPosition, Object dataObject) {
Toast.makeText(MainActivity.this, "click", Toast.LENGTH_SHORT).show(); }
});
}
private String userSex;
private String oppositeUserSex;
public void checkUserSex(){
final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference maleDb = FirebaseDatabase.getInstance().getReference().child("Users").child("Male");
maleDb.addChildEventListener((new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
if(dataSnapshot.getKey().equals(user.getUid())){
userSex = "Male";
oppositeUserSex = "Female";
getOppositeSexUsers();
}
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
}));
DatabaseReference femaleDb = FirebaseDatabase.getInstance().getReference().child("Users").child("Female");
femaleDb.addChildEventListener((new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
if(dataSnapshot.getKey().equals(user.getUid())){
userSex = "Female";
oppositeUserSex = "Male";
getOppositeSexUsers();
}
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
}));
}
public void getOppositeSexUsers(){
DatabaseReference oppositeSexDb = FirebaseDatabase.getInstance().getReference().child("Users").child(oppositeUserSex);
oppositeSexDb.addChildEventListener((new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
if(dataSnapshot.exists() && !dataSnapshot.child("connections").child("nope").hasChild(currentUId) && !dataSnapshot.child("connections").child("yeps").hasChild(currentUId)){
cards item = new cards(dataSnapshot.getKey(),dataSnapshot.child("name").getValue().toString());
rowItems.add(item);
arrayAdapter.notifyDataSetChanged();
}
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
}));
}
public void logoutUser(View view){
mAuth.signOut();
Intent intent = new Intent(MainActivity.this,ChooseLoginRegistrationActivity.class); //takes out to the choose page
startActivity(intent);
finish();
return;
}
}
Ниже приводится Logcat
2020-04-11 00:23:27.262 12387-12431/com.example.macdate D/EGL_emulation: eglMakeCurrent: 0xaf6b2ca0: ver 3 0 (tinfo 0xa5f308f0)
2020-04-11 00:23:27.301 12387-12488/com.example.macdate D/FA: Connected to remote service
2020-04-11 00:23:27.303 12387-12488/com.example.macdate V/FA: Processing queued up service tasks: 4
2020-04-11 00:23:27.684 12387-12387/com.example.macdate D/AndroidRuntime: Shutting down VM
2020-04-11 00:23:27.685 12387-12387/com.example.macdate E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.macdate, PID: 12387
java.lang.NullPointerException: Attempt to invoke interface method 'int java.lang.CharSequence.length()' on a null object reference
at java.util.regex.Matcher.reset(Matcher.java:1052)
at java.util.regex.Matcher.<init>(Matcher.java:180)
at java.util.regex.Pattern.matcher(Pattern.java:1006)
at com.google.firebase.database.core.utilities.Validation.isValidPathString(com.google.firebase:firebase-database@@19.2.1:40)
at com.google.firebase.database.core.utilities.Validation.validatePathString(com.google.firebase:firebase-database@@19.2.1:44)
at com.google.firebase.database.DataSnapshot.hasChild(com.google.firebase:firebase-database@@19.2.1:80)
at com.example.macdate.MainActivity$5.onChildAdded(MainActivity.java:165)
at com.google.firebase.database.core.ChildEventRegistration.fireEvent(com.google.firebase:firebase-database@@19.2.1:79)
at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@19.2.1:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@19.2.1:55)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)