gpt4 book ai didi

java - 尝试使用 FireBase 查看 Activity 中的 "Followers"和 "Following"时出现 NullPointerException

转载 作者:行者123 更新时间:2023-12-02 09:25:52 29 4
gpt4 key购买 nike

所以我在这里尝试制作一个类似 Instagram 的应用程序,但仅适用于汽车,我尝试单击“关注”和“关注者”来查看我关注的人和关注我的人,但我不确定为什么会有NPE吗...

This is my profile activity where i click on following or followers

我成功地可以关注用户,用户也关注我,他们确实存在于我的 firebaseRT 中,一切正常。

错误出现在带星号的代码行中: ******Objects.requireNonNull(getSupportActionBar()).setTitle(title);******

package com.capstone.araba;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.widget.Toolbar;

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.capstone.araba.Adapter.UserAdapter;
import com.capstone.araba.Model.User;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class FollowersActivity extends AppCompatActivity {

String id;
String title;

private List<String> idList;

RecyclerView recyclerView;
UserAdapter userAdapter;
List<User> userList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_followers);

Intent intent = getIntent();
id = intent.getStringExtra("id");
title = intent.getStringExtra("title");

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
******Objects.requireNonNull(getSupportActionBar()).setTitle(title);******
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});

recyclerView = findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
userList = new ArrayList<>();
userAdapter = new UserAdapter(this, userList, false);
recyclerView.setAdapter(userAdapter);

idList = new ArrayList<>();


switch (title) {
case "likes":
getLikes();
break;
case "following":
getFollowing();
break;
case "followers":
getFollowers();
break;

}

}


private void getFollowers() {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Follow")
.child(id).child("followers");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
idList.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
idList.add(snapshot.getKey());
}
showUsers();
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});
}

private void getFollowing() {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Follow")
.child(id).child("following");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
idList.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
idList.add(snapshot.getKey());
}
showUsers();
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});
}

private void getLikes() {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Likes")
.child(id);
reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
idList.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
idList.add(snapshot.getKey());
}
showUsers();
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});
}

private void showUsers() {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
userList.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
User user = snapshot.getValue(User.class);
for (String id : idList){
assert user != null;
if (user.getId().equals(id)){
userList.add(user);
}
}
}
userAdapter.notifyDataSetChanged();
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});
}
}
2019-10-12 12:53:58.911 23299-23299/com.capstone.araba E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.capstone.araba, PID: 23299
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.capstone.araba/com.capstone.araba.FollowersActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
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)
Caused by: java.lang.NullPointerException
at java.util.Objects.requireNonNull(Objects.java:203)
at com.capstone.araba.FollowersActivity.onCreate(FollowersActivity.java:49)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
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)

最佳答案

您可以只放置布局而不是工具栏

关于java - 尝试使用 FireBase 查看 Activity 中的 "Followers"和 "Following"时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58352640/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com