我正在處理帶有兩個(gè)不同片段的導(dǎo)航抽屜。不起作用的是下面的java文件中的“ first”和“ second”fragmentManager.beginTransaction().replace(R.id.fragment, first).commit();以及另一行中的“ ”和“ ” 。它引發(fā)的錯(cuò)誤是:Wrong 2nd argument type. Found: 'com.example.name_of_app.First', required: 'android.support.v4.app.Fragment' replace(int,android.support.v4.app.Fragment)in FragmentTransaction cannot be appliedto(int,com.example.name_of_app.First) 引發(fā)問題的 MainActivity.java 文件部分:public boolean onNavigationItemSelected(MenuItem item) { // Handle navigation view item clicks here. int id = item.getItemId(); if (id == R.id.nav_camera) { setTitle("First Fragment"); First first = new First(); FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.beginTransaction().replace(R.id.fragment, first).commit(); } else if (id == R.id.nav_gallery) { setTitle("Second Fragment"); Second second = new Second(); FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.beginTransaction().replace(R.id.fragment, second).commit(); } 這是 First.java 片段import android.os.Bundle;import android.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;/** * A simple {@link Fragment} subclass. */public class First extends Fragment { public First() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_first, container, false); }}
1 回答

慕的地10843
TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超8個(gè)贊
如果您查看錯(cuò)誤,您會(huì)看到android.support.v4.app.Fragment
. 但是,您的課程First
具有import android.app.Fragment;
導(dǎo)入功能。
他們不匹配。將導(dǎo)入更改為import android.support.v4.app.Fragment;
添加回答
舉報(bào)
0/150
提交
取消