gpt4 book ai didi

java - HomeFragment 无法转换为 Fragment

转载 作者:行者123 更新时间:2023-12-02 00:57:39 24 4
gpt4 key购买 nike

我在将 FirestoreUIBottomNavigationBar 连接时遇到问题。我不知道如何实现OnCreateView来修改HomeFragment,如下所示-

public class HomeFragment extends AppCompatActivity {
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference textRef = db.collection("text");

private NoteAdapter adapter;

@Nullable
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_home, container, false);
}

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

setUpRecyclerView();
}

private void setUpRecyclerView() {
Query query = textRef.orderBy("title", Query.Direction.DESCENDING);

FirestoreRecyclerOptions<Note> options = new FirestoreRecyclerOptions.Builder<Note>()
.setQuery(query, Note.class)
.build();

adapter = new NoteAdapter(options);
RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}

@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}

@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
}

比较另一个工作 fragment (GameFragment)-

public class GameFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_game, container, false);
}
}

和BottomNavigation方法-

private BottomNavigationView.OnNavigationItemSelectedListener navListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment = null;

switch (item.getItemId()) {
case R.id.nav_home:
selectedFragment = new HomeFragment();
break;
case R.id.nav_ranking:
selectedFragment = new RankingFragment();
break;
case R.id.nav_user:
selectedFragment = new UserFragment();
break;
}

getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container,
selectedFragment)
.commit();

return true;
}
};

我尝试粘贴

import androidx.fragment.app.Fragment;

进入 public class HomeFragment extends AppCompatActivity, Fragment {} 但随后所有代码崩溃。请帮忙,谢谢。

最佳答案

问题是您正在尝试用 Activity 替换 fragment 。

我将提取一些代码并指出问题所在:

//.....
switch (item.getItemId()) {
case R.id.nav_home:
selectedFragment = new HomeFragment(); // <-- HomeFragment is of type: AppCompatActivity
break;
case R.id.nav_ranking:
selectedFragment = new RankingFragment(); // <-- RankingFragment is of type: Fragment
break;
case R.id.nav_user:
selectedFragment = new UserFragment(); // <-- UserFragment is of type: Fragment
break;
}

getSupportFragmentManager() // <-- You're asking the SupportFragmentManager to replace your current fragment with another
.beginTransaction()
.replace(R.id.fragment_container,
selectedFragment) // <-- You can replace a fragment with another fragment, but not with an Activity
.commit();
//...

所以我建议您将 HomeFragment 重命名为 HomeActivity 并创建一个从 Fragment 扩展的新 fragment ,并像使用其他 fragment 一样使用它。

概述我的意思:

  • HomeActivity 扩展了 AppCompatActivity -> 使用 View R.id.fragment_container,还可以像您已有的那样在此处添加用于 fragment 替换的代码。
  • HomeFragment 扩展 Fragment
  • RankingFragment 扩展 Fragment
  • UserFragment 扩展 Fragment

关于java - HomeFragment 无法转换为 Fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61137703/

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