我对Android开发应用程序完全不了解,并且我正尝试以自己的技能来创建一个小应用程序,而今天我面临着一个昨天未出现的nex错误...
应用程序正常启动,但是当我单击注册或连接按钮时,应用程序崩溃,并且在logcat上看到以下错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at fr.app.faridsissaoui.myapplicationtinder.MainActivity$6.onChildAdded(MainActivity.java:207)
下面是完整的代码,如果有人可以给我一些帮助,我只是理解那是来自“ .toSting()”的消息,但无法纠正。
软件包fr.app.faridsissaoui.myapplicationfinder;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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.google.firebase.database.ValueEventListener;
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();
currentUId = mAuth.getCurrentUser().getUid();
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);
arrayAdapter.notifyDataSetChanged();
}
@Override
public void onLeftCardExit(Object dataObject) {
cards obj = (cards) dataObject;
String userId = obj.getUserId();
usersDb.child(oppositeUserSex).child(userId).child("connexions").child("nope").child(currentUId).setValue(true);
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("connexions").child("yep").child(currentUId).setValue(true);
isConnectionMatch (userId);
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 void isConnectionMatch(String userId) {
DatabaseReference currentUserConnectionsDb = usersDb.child(userSex).child(currentUId).child("connexions").child("yep").child(userId);
currentUserConnectionsDb.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
Toast.makeText(MainActivity.this, "Nouveau Match!", Toast.LENGTH_LONG).show();
usersDb.child(oppositeUserSex).child(dataSnapshot.getKey()).child("connexions").child("matches").child(currentUId).setValue(true);
usersDb.child(userSex).child(currentUId).child("connexions").child("matches").child(dataSnapshot.getKey()).setValue(true);
}
}
@Override
public void onCancelled (@NonNull DatabaseError databaseError){ }
});
}
private String userSex;
private String oppositeUserSex;
public void checkUserSex(){
final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference maleDb = FirebaseDatabase.getInstance().getReference().child("Users").child("Homme");
maleDb.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
if (dataSnapshot.getKey().equals (user.getUid())){
userSex = "Homme";
oppositeUserSex = "Femme";
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("Femme");
femaleDb.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
if (dataSnapshot.getKey().equals(user.getUid())){
userSex = "Femme";
oppositeUserSex = "Homme";
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("connexions").child("nope").hasChild(currentUId) && !dataSnapshot.child("connexions").child("yep").hasChild(currentUId)) {
String profileImageUrl = "default";
if (dataSnapshot.child("profileImageUrl").getValue() != "default"){
profileImageUrl = dataSnapshot.child("profileImageUrl").getValue().toString();
}
cards item = new cards(dataSnapshot.getKey(), (String) dataSnapshot.child("name").getValue().toString(), profileImageUrl);
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);
startActivity(intent);
finish();
return;
}
public void goToSettings(View view) {
Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
intent.putExtra("userSex", userSex);
startActivity(intent);
return;
}
}
非常感谢您的帮助。