2017-09-18 136 views
0

我現在有一個項目,我需要使用2個片段。在我的mainActivity裏面,我調用兩個片段的兩個方法來初始化它們的適配器。我現在面臨的問題是,當我調用第二個片段的方法時,我得到一個類別轉換異常,說我的片段1不能轉換爲片段2.類轉換異常:片段1不能轉換爲片段2

我輸入了正確的庫import android.support.v4.app.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); 
     } 
    } 

這是當我請從第一片段的方法我的活動內:

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

這是當我從我的活動中的第二個片段調用方法:

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()); 
         } 
        } 

這是c第一個片段頌歌:

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); 
    } 

} 
+0

您可以顯示您從片段中調用方法的詳細代碼嗎?@Marcos Guimaraes – KeLiuyue

+0

您正嘗試將'R.id.viewpager'轉換爲'ExternsFragment'和'InternsFragment',其中一個演員將拋出異常。 – Oleg

+0

感謝您的回覆。所以我這樣做是因爲我只想在我從我的活動中得到結果後,片段就初始化它們的適配器。有沒有更好的方法來做到這一點? –

回答

0

讓你的片段爲您的活動類變量。

InternsFragment mInternsFragment; 
ExternsFragment mExternsFragment; 

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

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

然後直接從活動中的任何地方使用它們。