![](/img/trans.png)
[英]Where can I find ID of a fragment. And how can I get access to the view of a fragment from MainActivity class?
[英]I get a No view for id for 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) {
}
});
}
}
我的活动..
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 和我的应用程序崩溃。 我不确定为什么会发生这种情况,因此非常感谢任何帮助。
如果启用断言,我可以看到您的一小部分代码会崩溃:
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);
}
我确定您不想在没有 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 如何在 android 中使用断言?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.