不兼容的类型:RecViewFragment 无法转换为 Fragment
Posted
技术标签:
【中文标题】不兼容的类型:RecViewFragment 无法转换为 Fragment【英文标题】:incompatible types: RecViewFragment cannot be converted to Fragment 【发布时间】:2021-12-20 04:58:56 【问题描述】:早些时候,我的代码运行良好,但在 android studio gradle build 中进行了一些更新后,出现错误 - “错误: getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new RecViewFragment()).commit();" 我试过 import androidx.fragment.app.Fragment
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationView;
public class MainActivityBuses extends AppCompatActivity
NavigationView nav;
ActionBarDrawerToggle toggle;
DrawerLayout drawerLayout;
Toolbar toolbar;
BottomNavigationView btmNav;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_buses);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
nav = findViewById(R.id.drawernav);
drawerLayout = findViewById(R.id.drawerlayout);
toggle = new ActionBarDrawerToggle(this,drawerLayout, toolbar,R.string.open,R.string.close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
nav.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener()
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item)
Fragment selectdnav = null;
switch (item.getItemId())
case R.id.dnavprofile:
selectdnav = new MyProfileFragment();
drawerLayout.closeDrawer(GravityCompat.START);
break;
case R.id.dnavmap:
selectdnav = new MapFragment();
drawerLayout.closeDrawer(GravityCompat.START);
break;
case R.id.dnavmybus:
selectdnav = new BusFragment();
drawerLayout.closeDrawer(GravityCompat.START);
break;
case R.id.dnavinfo:
selectdnav = new InfoFragment();
drawerLayout.closeDrawer(GravityCompat.START);
break;
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,selectdnav).commit();
return true;
);
btmNav = (BottomNavigationView) findViewById(R.id.btmnav);
btmNav.setOnNavigationItemSelectedListener((navListner));
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new RecViewFragment()).commit();
btmNav.getMenu().findItem(R.id.navhome).setChecked(true);
private BottomNavigationView.OnNavigationItemSelectedListener navListner = new BottomNavigationView.OnNavigationItemSelectedListener()
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item)
Fragment selectedFragment = null;
switch (item.getItemId())
case R.id.navprofile:
selectedFragment = new MyProfileFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new MyProfileFragment()).commit();
break;
case R.id.navmap:
Intent intent = new Intent(MainActivityBuses.this, MapActivity.class);
startActivity(intent);
finish();
break;
case R.id.navhome:
selectedFragment = new RecViewFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new RecViewFragment()).commit();
break;
case R.id.navmybus:
selectedFragment = new BusFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new BusFragment()).commit();
break;
case R.id.navinfo:
selectedFragment = new InfoFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new InfoFragment()).commit();
break;
return true;
;
RecViewFragment 类
package com.example.trackmybusstudent;
import android.app.Fragment;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
import com.google.firebase.firestore.QuerySnapshot;
import java.util.ArrayList;
import java.util.List;
/**
* A simple @link Fragment subclass.
* Use the @link RecViewFragment#newInstance factory method to
* create an instance of this fragment.
*/
public class RecViewFragment extends Fragment
RecyclerView recyclerView;
ArrayList datalist;
FirebaseFirestore db;
myadapter myadap;
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
public RecViewFragment()
public static RecViewFragment newInstance(String param1, String param2)
RecViewFragment fragment = new RecViewFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
if (getArguments() != null)
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_rec_view, container, false);
recyclerView = (RecyclerView) v.findViewById(R.id.recycleview);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
db = FirebaseFirestore.getInstance();
datalist = new ArrayList<>();
myadap = new myadapter(datalist);
recyclerView.setAdapter(myadap);
db.collection("Bus_Database").orderBy("Route_Number", Query.Direction.ASCENDING).get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>()
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots)
List<DocumentSnapshot> list = queryDocumentSnapshots.getDocuments();
for(DocumentSnapshot d:list)
model obj = d.toObject(model.class);
datalist.add(obj);
myadap.notifyDataSetChanged();
);
return v;
【问题讨论】:
【参考方案1】:在您的开关中没有默认情况,也许您没有在菜单文件中正确设置您的 id,因此 selectdnav 保持为空。 建议你加个断点快速分析一下。
【讨论】:
我已经交叉检查了我的身份证,他们很好。看,其他片段工作正常。因此,RecViewFragment 类可能存在一些问题。让我也分享一下 RecViewFragment 类。 我收到了错误。现在它工作正常以上是关于不兼容的类型:RecViewFragment 无法转换为 Fragment的主要内容,如果未能解决你的问题,请参考以下文章
不兼容的类型:条件表达式 void 中的错误类型无法转换为布尔值。请协助
AIDL 中的 Parcelize 注释:不兼容的类型:Object 无法转换为 MyCustomObject
无法处理托管/反向引用“defaultReference”:反向引用类型 (java.util.List) 与托管类型不兼容