英文:
HomeFragment cannot be converted to Fragment
问题
以下是您提供的代码的翻译部分:
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();
}
}
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);
}
}
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 {}
,但是代码崩溃了。如果您仍然遇到问题,您可能需要检查代码中是否存在其他错误或冲突,以及您在 HomeFragment
类中是否实现了正确的接口和方法。如果需要更详细的帮助,请提供更多关于代码崩溃的信息。
英文:
I have problem with connecting FirestoreUI with BottomNavigationBar. I don't know how to implement OnCreateView to modified HomeFragment which look like this-
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();
}
}
To compare another working 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);
}
}
And BottomNavigation method-
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;
}
};
I tried to paste
> import androidx.fragment.app.Fragment;
into public class HomeFragment extends AppCompatActivity, Fragment {}
but then all code crash.
Please help, thanks.
答案1
得分: 2
问题在于你正在尝试用一个活动替换片段。
我将提取一些代码并指出问题所在:
//.....
switch (item.getItemId()) {
case R.id.nav_home:
selectedFragment = new HomeFragment(); // <-- HomeFragment的类型是:AppCompatActivity
break;
case R.id.nav_ranking:
selectedFragment = new RankingFragment(); // <-- RankingFragment的类型是:Fragment
break;
case R.id.nav_user:
selectedFragment = new UserFragment(); // <-- UserFragment的类型是:Fragment
break;
}
getSupportFragmentManager() // <-- 你正在要求SupportFragmentManager用另一个片段替换当前的片段
.beginTransaction()
.replace(R.id.fragment_container,
selectedFragment) // <-- 你可以用另一个片段替换片段,但不能用一个活动来替换
.commit();
//...
所以我的建议是,将HomeFragment
重命名为HomeActivity
,然后创建一个继承自Fragment的新片段,并像你处理其他片段那样使用它们。
我想要说明的是:
HomeActivity
继承自AppCompatActivity
-> 包括一个视图R.id.fragment_container
,同时在这里添加片段替换的代码,就像你已经做过的那样。HomeFragment
继承自Fragment
RankingFragment
继承自Fragment
UserFragment
继承自Fragment
英文:
The problem is that you are trying to replace a fragment with an activity.
I'm going to extract some code and point out what the problem is:
//.....
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();
//...
So what I suggest is that you rename HomeFragment
to HomeActivity
and make a new fragment that extends from Fragment and use it like you do with the other fragments you have.
To outline what I mean:
- HomeActivity extends AppCompatActivity -> with a view R.id.fragment_container, also add the code here for fragment replacement like you already have.
- HomeFragment extends Fragment
- RankingFragment extends Fragment
- UserFragment extends Fragment
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论