所以在这里,我试图制作一个类似Instagram的应用程序,但仅用于汽车,并且我试图单击关注者和关注者,以查看我关注的人并了解关注我的人,但是我不确定为什么会有NPE ...
我可以成功跟踪ueser,用户也可以跟踪我,并且它们确实存在于我的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)
答案 0 :(得分:1)
您可以放置布局而不是工具栏
答案 1 :(得分:0)
在我看来,标题在这里的意图似乎为空:
title = intent.getStringExtra("title");
这就是为什么在试图在操作栏上设置空标题的行上显示空指针异常的原因:
Objects.requireNonNull(getSupportActionBar()).setTitle(title);
尝试一下,看看是否可以避免崩溃。
//title = intent.getStringExtra("title");
title = "Dummy Title";