尝试调用虚拟方法';布尔值java.lang.Object.equals(java.lang.Object)';关于空对象引用
我正在关注youtube上的一个制作tinder克隆的教程。我得到以下错误。可能的解决办法是什么?主活动的代码在android studio中没有显示任何错误,但在运行时显示错误 这是在android设备上测试应用程序时显示的错误尝试调用虚拟方法';布尔值java.lang.Object.equals(java.lang.Object)';关于空对象引用,java,android,firebase,Java,Android,Firebase,我正在关注youtube上的一个制作tinder克隆的教程。我得到以下错误。可能的解决办法是什么?主活动的代码在android studio中没有显示任何错误,但在运行时显示错误 这是在android设备上测试应用程序时显示的错误 java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Object.equals(java.lang.Object)' on a null object
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Object.equals(java.lang.Object)' on a null object reference
at com.techjd.hubu.MainActivity$6.onChildAdded(MainActivity.java:196)
at com.google.firebase.database.core.ChildEventRegistration.fireEvent(com.google.firebase:firebase-database@@19.2.0:79)
at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@19.2.0:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@19.2.0:55)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5740)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:766)
这是主要的活动
import android.content.Context;
import android.content.Intent;
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 androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
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("connections").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("connections").child("yes").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, "Item Clicked", Toast.LENGTH_SHORT).show();
}
});
}
private void isConnectionMatch(String userId) {
DatabaseReference currentUserConnectionsDB = usersDb.child(userSex).child(currentUId).child("connections").child("yes").child(userId);
currentUserConnectionsDB.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
Toast.makeText(MainActivity.this, "new Connection", Toast.LENGTH_LONG).show();
usersDb.child(oppositeUserSex).child(dataSnapshot.getKey()).child("connections").child("matches").child(currentUId).setValue(true);
usersDb.child(userSex).child(currentUId).child("connections").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("Male");
maleDb.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot.getKey().equals(user.getUid())){
userSex = "Male";
oppositeUserSex = "Female";
getOppositeSexUsers();
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
DatabaseReference femaleDb = FirebaseDatabase.getInstance().getReference().child("Users").child("Female");
femaleDb.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot.getKey().equals(user.getUid())){
userSex = "Female";
oppositeUserSex = "Male";
getOppositeSexUsers();
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
public void getOppositeSexUsers(){
DatabaseReference oppositeSexDb = FirebaseDatabase.getInstance().getReference().child("Users").child(oppositeUserSex);
oppositeSexDb.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot.exists() && !dataSnapshot.child("connections").child("nope").hasChild(currentUId) && !dataSnapshot.child("connections").child("yes").hasChild(currentUId)){
String profileImageUrl = "default";
if (!dataSnapshot.child("profileImageUrl").getValue().equals("default")){
profileImageUrl = dataSnapshot.child("profileImageUrl").getValue().toString();
}
cards item = new cards(dataSnapshot.getKey(), dataSnapshot.child("name").getValue().toString(), profileImageUrl);
rowItems.add(item);
arrayAdapter.notifyDataSetChanged();
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(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;
}
导入android.content.Context;
导入android.content.Intent;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
导入android.widget.Toast;
导入androidx.annotation.NonNull;
导入androidx.appcompat.app.appcompat活动;
导入com.google.firebase.auth.FirebaseAuth;
导入com.google.firebase.auth.FirebaseUser;
导入com.google.firebase.database.ChildEventListener;
导入com.google.firebase.database.DataSnapshot;
导入com.google.firebase.database.DatabaseError;
导入com.google.firebase.database.DatabaseReference;
导入com.google.firebase.database.FirebaseDatabase;
导入com.google.firebase.database.ValueEventListener;
导入com.lorentzos.flingswip.SwipeFlingAdapterView;
导入java.util.ArrayList;
导入java.util.List;
公共类MainActivity扩展了AppCompatActivity{
私人卡数据[];
专用阵列适配器阵列适配器;
私人互联网i;
私人消防队;
私有字符串currentUId;
私有数据库引用用户SDB;
列表视图列表视图;
列出项目;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
usersDb=FirebaseDatabase.getInstance().getReference().child(“用户”);
mAuth=FirebaseAuth.getInstance();
currentUId=mAuth.getCurrentUser().getUid();
checkUserSex();
rowItems=新的ArrayList();
arrayAdapter=新的arrayAdapter(this,R.layout.item,rowItems);
SwipeFlingDapterView flingContainer=(SwipeFlingDapterView)findViewById(R.id.frame);
flingContainer.setAdapter(阵列适配器);
flingContainer.setFlingListener(新的SwipeFlingAdapterView.onFlingListener(){
@凌驾
public void removeFirstObjectInAdapter(){
//这是从适配器(/AdapterView)中删除对象的最简单方法
Log.d(“列表”,“删除对象!”);
行项目。删除(0);
arrayAdapter.notifyDataSetChanged();
}
@凌驾
public void onLeftCardExit(对象dataObject){
卡片obj=(卡片)数据对象;
字符串userId=obj.getUserId();
usersDb.child(antipiteusersex).child(userId).child(“连接”).child(“nope”).child(currentUId).setValue(true);
Toast.makeText(MainActivity.this,“Left”,Toast.LENGTH_SHORT.show();
}
@凌驾
RightCardExit上的公共无效(对象dataObject){
卡片obj=(卡片)数据对象;
字符串userId=obj.getUserId();
usersDb.child(antipiteusersex).child(userId).child(“连接”).child(“是”).child(currentUId).setValue(true);
isConnectionMatch(userId);
Toast.makeText(MainActivity.this,“Right”,Toast.LENGTH_SHORT.show();
}
@凌驾
dapterabouttoempty上的公共void(int itemsInAdapter){
}
@凌驾
公共无效onScroll(浮动百分比){
}
});
//(可选)添加一个McClickListener
flingContainer.setOnItemClickListener(新的SwipeFlingAdapterView.OnItemClickListener(){
@凌驾
单击公共对象(int itemPosition,Object dataObject){
Toast.makeText(MainActivity.this,“单击项”,Toast.LENGTH_SHORT.show();
}
});
}
私有void isConnectionMatch(字符串userId){
DatabaseReference currentUserConnectionsDB=usersDb.child(userSex).child(currentUId).child(“连接”).child(“是”).child(userId);
currentUserConnectionsDB.addListenerForSingleValueEvent(新值EventListener()){
@凌驾
public void onDataChange(@NonNull DataSnapshot DataSnapshot){
if(dataSnapshot.exists()){
Toast.makeText(MainActivity.this,“新连接”,Toast.LENGTH_LONG.show();
usersDb.child(antipiteusersex).child(dataSnapshot.getKey()).child(“连接”).child(“匹配”).child(currentUId).setValue(true);
usersDb.child(userSex).child(currentUId).child(“连接”).child(“匹配”).child(dataSnapshot.getKey()).setValue(true);
}
}
@凌驾
已取消的公共void(@NonNull DatabaseError DatabaseError){
}
});
}
私有字符串userSex;
私有字符串反对用户性别;
public void checkUserSex(){
最终FirebaseUser用户=FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference maleDb=FirebaseDatabase.getInstance().getReference().child(“用户”).child(“男性”);
maleDb.addChildEventListener(新的ChildEventListener(){
@凌驾
公共void onChildAdded(DataSnapshot DataSnapshot,字符串s){
如果(dataSnapshot.getKey().equals(user.getUid())){
userSex=“男”;
异性用户性别=“女性”;
getObjectItemsUsers();
}
}
@凌驾
公共void onChildChanged(DataSnapshot DataSnapshot,字符串s){
}
@凌驾
ChildRemoved上的公共void(DataSnapshot DataSnapshot){
}
@凌驾
已移动ChildMoved上的公共void(DataSnapshot DataSnapshot,字符串s){
}
@凌驾
已取消的公共void(DatabaseError DatabaseError){
}
});
DatabaseReference femaleDb=FirebaseDatabase.getInstance().getReference().child(“用户”).child(“女性”);
femaleDb.addCh
Object profileImageUrlData = dataSnapshot.child("profileImageUrl").getValue();
if (profileImageUrlData != null && !profileImageUrlData.equals("default")){
profileImageUrl = profileImageUrlData.toString();
}
if (!dataSnapshot.child("profileImageUrl").getValue().equals("default")){
if (dataSnapshot.child("profileImageUrl") != null &&
!"default".equals(dataSnapshot.child("profileImageUrl").getValue())){