gpt4 book ai didi

java - 类转换异常 : fragment 1 cannot be cast to fragment 2

转载 作者:太空宇宙 更新时间:2023-11-04 11:08:07 25 4
gpt4 key购买 nike

我现在有一个项目需要使用 2 个 fragment 。在我的 mainActivity 中,我调用两个 fragment 中的两个方法来初始化它们的适配器。我现在面临的问题是,当我调用第二个 fragment 的方法时,我收到一个类转换异常,说我的 fragment 1 无法转换为 fragment 2。

我导入了正确的库import android.support.v4.app.Fragment,所以我不知道为什么会收到错误。

这是我的 Activity 的一部分,我在其中定义 fragment :

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//toolbar = (Toolbar) findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);

token = SharedPrefsHelper.getString(getApplicationContext(), Constants.TOKEN);

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);

tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);

SendNetworkRequest(0);
SendNetworkRequest(1);
}

private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new InternsFragment(), "Internos");
adapter.addFragment(new ExternsFragment(), "Externos");
viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();

public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}

@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}

@Override
public int getCount() {
return mFragmentList.size();
}

public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}

@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}

这是我从 Activity 内的第一个 Fragment 调用该方法的时候:

if (listUsers != null && !listUsers.isEmpty()) {
ExternsFragment fragment = (ExternsFragment) getSupportFragmentManager().findFragmentById(R.id.viewpager);
fragment.initAdapter(listUsers);
}

这是当我从 Activity 内的第二个 Fragment 调用该方法时:

if (!response.body().isEmpty()) {
listTeams = response.body();

InternsFragment fragment = (InternsFragment) getSupportFragmentManager().findFragmentById(R.id.viewpager);
fragment.initAdapter();

for ( int j = 0; j < listTeams.size(); j ++) {
System.out.println(listTeams.get(j).getName());
}
}

这是第一个 fragment 的代码:

public class ExternsFragment extends Fragment {

//@BindView(R.id.recycler_view) RecyclerView recyclerView;
private TeamAdapter teamAdapter;
private RecyclerView recyclerView;

public ExternsFragment() {
// Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_externs, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

return rootView;
}

public void initAdapter(List<UserProject> userList) {

teamAdapter = new TeamAdapter(getActivity(), userList);

recyclerView.setAdapter(teamAdapter);

}
}

这是第二个:

public class InternsFragment extends Fragment{

private RecyclerView recyclerView;
List<ListItem> consolidatedList = new ArrayList<>();
private GroupAdapter adapter;

public InternsFragment() {
// Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_interns, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

return rootView;
}

private HashMap<String, List<UserProject>> groupDataIntoHashMap(List<UserProject> listOfPojosOfJsonArray) {

HashMap<String, List<UserProject>> groupedHashMap = new HashMap<>();

for (UserProject pojoOfJsonArray : listOfPojosOfJsonArray) {

String hashMapKey = pojoOfJsonArray.getTeamId();

if (groupedHashMap.containsKey(hashMapKey)) {
// The key is already in the HashMap; add the pojo object
// against the existing key.
groupedHashMap.get(hashMapKey).add(pojoOfJsonArray);
} else {
// The key is not there in the HashMap; create a new key-value pair
List<UserProject> list = new ArrayList<>();
list.add(pojoOfJsonArray);
groupedHashMap.put(hashMapKey, list);
}
}


return groupedHashMap;
}

public void initAdapter() {
HashMap<String, List<UserProject>> groupedHashMap = groupDataIntoHashMap(ActivityTeams.listUsers);


for (String date : groupedHashMap.keySet()) {
TeamItem dateItem = new TeamItem();
dateItem.setTeam(date);
consolidatedList.add(dateItem);


for (UserProject pojoOfJsonArray : groupedHashMap.get(date)) {
GeneralItem generalItem = new GeneralItem();
generalItem.setPojoOfJsonArray(pojoOfJsonArray);//setBookingDataTabs(bookingDataTabs);
consolidatedList.add(generalItem);
}
}


adapter = new GroupAdapter(getActivity(), consolidatedList);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}

}

最佳答案

将 fragment 作为 Activity 的类变量。

InternsFragment mInternsFragment;
ExternsFragment mExternsFragment;

//inside oncreate
mInternsFragment = new InternsFragment ();
mExternsFragment = new ExternsFragment ();

adapter.addFragment( mInternsFragment, "Internos" );
adapter.addFragment( mExternsFragment, "Externos" );

然后直接从 Activity 中的任何位置使用它们。

关于java - 类转换异常 : fragment 1 cannot be cast to fragment 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46270209/

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