我正在尝试在片段中进行活动,该片段显示了您关注的人的信息,而该活动显示了任何人的最新信息。
我的片段。
public class HomeFragment extends Fragment {
private PostAdapter postAdapter;
private List<Post> postLists;
private List<String> followingList;
private ProgressBar progressBar;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_home, container,false);
RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getContext());
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(linearLayoutManager);
postLists=new ArrayList<>();
postAdapter=new PostAdapter(getContext(), postLists);
recyclerView.setAdapter(postAdapter);
ImageView globalPosts = view.findViewById(R.id.globalPosts);
globalPosts.setOnClickListener(v->{
Intent intent= new Intent(getContext(), GlobalPostsActivity.class);
startActivity(intent);
});
progressBar=view.findViewById(R.id.progress_circular);
checkFollowing();
return view;
}
private void checkFollowing(){
followingList=new ArrayList<>();
DatabaseReference reference=FirebaseDatabase.getInstance().getReference("Support")
.child(Objects.requireNonNull(FirebaseAuth.getInstance().getCurrentUser()).getUid())
.child("supporting");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
followingList.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
followingList.add(snapshot.getKey());
}
readPosts();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void readPosts(){
DatabaseReference reference= FirebaseDatabase.getInstance().getReference("Posts");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
postLists.clear();
for (DataSnapshot snapshot :dataSnapshot.getChildren()){
Post post=snapshot.getValue(Post.class);
for (String id: followingList){
assert post != null;
if (post.getPublisher().equals(id)){
postLists.add(post);
}
}
}
postAdapter.notifyDataSetChanged();
progressBar.setVisibility(View.GONE);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
我的活动..
public class GlobalPostsActivity extends AppCompatActivity {
private PostAdapter postAdapter;
private List<Post> postLists;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_global_posts);
RecyclerView recyclerView = findViewById(R.id.recycler_view1);
recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(linearLayoutManager);
postLists=new ArrayList<>();
postAdapter=new PostAdapter(this, postLists);
recyclerView.setAdapter(postAdapter);
readGlobalPosts();
}
private void readGlobalPosts(){
DatabaseReference reference= FirebaseDatabase.getInstance().getReference("Posts");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
postLists.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
Post post=snapshot.getValue(Post.class);
assert post !=null;
postLists.add(post);
}
postAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
我得到一个致命的例外:main,当我单击一个用户名进入其个人资料时,我的应用程序崩溃。我不确定为什么会发生这种情况,因此不胜感激。
答案 0 :(得分:1)
如果启用断言,我的代码中有一部分会崩溃:
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
Post post=snapshot.getValue(Post.class);
assert post !=null; // THIS
postLists.add(post);
}
您在此处写的内容等同于:
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
Post post=snapshot.getValue(Post.class);
if(post == null) {
throw new IllegalStateException("Post was null");
}
postLists.add(post);
}
,我确定您不希望在没有发布时崩溃整个应用程序吗?
如果您要测试它是否存在(如果不存在,请获取反馈),请尝试如下操作:
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
Post post=snapshot.getValue(Post.class);
if(post == null) {
Log.w("TUT", "Expected a post and didn't get one.");
} else {
postLists.add(post);
}
}
参考:
https://en.wikibooks.org/wiki/Java_Programming/Keywords/assert How to use assert in android?