HomeFragment 无法转换为 Fragment

huangapple go评论87阅读模式
英文:

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(&quot;text&quot;);

    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(&quot;title&quot;, Query.Direction.DESCENDING);

        FirestoreRecyclerOptions&lt;Note&gt; options = new FirestoreRecyclerOptions.Builder&lt;Note&gt;()
                .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(); // &lt;-- HomeFragment的类型是:AppCompatActivity
        break;
    case R.id.nav_ranking:
        selectedFragment = new RankingFragment(); // &lt;-- RankingFragment的类型是:Fragment
        break;
    case R.id.nav_user:
        selectedFragment = new UserFragment(); // &lt;-- UserFragment的类型是:Fragment
        break;
}

getSupportFragmentManager() // &lt;-- 你正在要求SupportFragmentManager用另一个片段替换当前的片段
    .beginTransaction()
    .replace(R.id.fragment_container,
    selectedFragment) // &lt;-- 你可以用另一个片段替换片段,但不能用一个活动来替换
    .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(); // &lt;-- HomeFragment is of type: AppCompatActivity
break;
case R.id.nav_ranking:
selectedFragment = new RankingFragment(); // &lt;-- RankingFragment is of type: Fragment
break;
case R.id.nav_user:
selectedFragment = new UserFragment(); // &lt;-- UserFragment is of type: Fragment
break;
}
getSupportFragmentManager() // &lt;-- You&#39;re asking the SupportFragmentManager to replace your current fragment with another
.beginTransaction()
.replace(R.id.fragment_container,
selectedFragment) // &lt;-- 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

huangapple
  • 本文由 发表于 2020年4月10日 17:45:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/61137703.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定