gpt4 book ai didi

java - 我得到 fragment id 无 View

转载 作者:行者123 更新时间:2023-12-02 08:58:46 30 4
gpt4 key购买 nike

我正在尝试在我的 fragment 中进行 Activity , fragment 显示您关注的人的帖子, Activity 显示任何人的最新帖子。

我的 fragment ..

 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) {
}
});
}
}

我的 Activity ..

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) {
}
});
}
}

我遇到致命异常:当我单击用户名转到他们的个人资料时,主应用程序和我的应用程序崩溃了。我不确定为什么会发生这种情况,因此非常感谢任何帮助。

最佳答案

我可以看到,如果启用断言,您的代码中有一点将会崩溃:

            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?

关于java - 我得到 fragment id 无 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60327532/

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