2016-04-03 35 views
0

我有MainActivity,這是導航欄,以不同的片段不能轉換到片段listfragment

public boolean onNavigationItemSelected(MenuItem item) { 

    // Handle navigation view item clicks here. 
    int id = item.getItemId();; 
    FragmentManager fragmentManager = getSupportFragmentManager(); 
    fragmentManager.beginTransaction(); 

    if (id == R.id.nav_camera) { 
     fragmentManager.beginTransaction().replace(R.id.container,fcafee).commit(); 
    } else if (id == R.id.nav_gallery) { 
     fragmentManager.beginTransaction().replace(R.id.container.fclubs).commit(); 
    } else if (id == R.id.nav_slideshow) { //erorr here 
     fragmentManager.beginTransaction().replace(R.id.container,feat).commit(); 
    } else if (id == R.id.nav_manage) { 
     fragmentManager.beginTransaction().replace(R.id.container,fsnack).commit(); 
    } else if (id == R.id.nav_share) { 
     fragmentManager.beginTransaction().replace(R.id.container,fsetting).commit(); 
    } 

而在我的片段fclubs我的ListView

public class FragmentClubs extends ListFragment { 

我檢查所有的進口,但所有看起來不錯。

但我得到該行的錯誤消息,我有ListFragment

Error:(108, 71) error: incompatible types: FragmentClubs cannot be converted to Fragment 

完全Fclubs:

public class FragmentClubs extends ListFragment { 
// TODO: Rename parameter arguments, choose names that match 
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER 
private static final String ARG_PARAM1 = "param1"; 
private static final String ARG_PARAM2 = "param2"; 

ListView listView; 
String[] names; 
final String LOG_TAG = "myLogs"; 

// TODO: Rename and change types of parameters 
private String mParam1; 
private String mParam2; 

private OnFragmentInteractionListener mListener; 

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

/** 
* Use this factory method to create a new instance of 
* this fragment using the provided parameters. 
* 
* @param param1 Parameter 1. 
* @param param2 Parameter 2. 
* @return A new instance of fragment FragmentClubs. 
*/ 
// TODO: Rename and change types and number of parameters 
public static FragmentClubs newInstance(String param1, String param2) { 
    FragmentClubs fragment = new FragmentClubs(); 
    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); 
    } 
} 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    ListView listView = getListView(); 
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); 
    listView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() { 

     @Override 
     public void onItemCheckedStateChanged(ActionMode mode, int position, 
               long id, boolean checked) { 
      // Here you can do something when items are selected/de-selected, 
      // such as update the title in the CAB 
     } 

     @Override 
     public boolean onActionItemClicked(ActionMode mode, MenuItem item) { 
      // Respond to clicks on the actions in the CAB 
      switch (item.getItemId()) { 
       case R.id.listView: 
        mode.finish(); // Action picked, so close the CAB 
        return true; 
       default: 
        return false; 
      } 
     } 

     @Override 
     public boolean onCreateActionMode(ActionMode mode, Menu menu) { 
      // Inflate the menu for the CAB 
      MenuInflater inflater = mode.getMenuInflater(); 
      inflater.inflate(R.menu.eatmenu, menu); 
      return true; 
     } 

     @Override 
     public void onDestroyActionMode(ActionMode mode) { 
      // Here you can make any necessary updates to the activity when 
      // the CAB is removed. By default, selected items are deselected/unchecked. 
     } 

     @Override 
     public boolean onPrepareActionMode(ActionMode mode, Menu menu) { 
      // Here you can perform updates to the CAB due to 
      // an invalidate() request 
      return false; 
     } 
    }); 



    return inflater.inflate(R.layout.fragment_clubs,container,false); // TODO: Rename method, update argument and hook method into UI event 
} 
public void onButtonPressed(Uri uri) { 
    if (mListener != null) { 
     mListener.onFragmentInteraction(uri); 
    } 
} 
    @Override 
public void onDetach() { 
    super.onDetach(); 
    mListener = null; 
} 

錯誤: Here what I get

UPDATED

+0

發佈完整的'FragmentClubs'代碼和完整的'logcat' – Vucko

+0

已經加入描述 –

回答

2

的錯誤是可能是因爲ListFragment您有來自android.app.ListFragment本地庫,而getSupportFragmentManager需要來自android.support.v4.app.ListFragment庫的片段。

在FragmentClubs類

所以,刪除import android.app.ListFragment;,並與import android.support.v4.app.ListFragment


更新替換:

您需要在onCreateView將所有的代碼到一個單獨的方法,如下圖所示:

@Override 
public void onActivityCreated (Bundle savedInstanceState) { 

    // your code 
} 

這是因爲onCreateView只負責返回視圖,其他任何歌劇tions應該在onActivityCreated

+0

謝謝,這對我很有用! 但現在啓動應用程序,我得到這一行中的錯誤: ListView listView = getListView(); –

+0

你得到了什麼錯誤? –

+0

該應用現在正在啓動,但後來我點擊導航欄上的該片段,該應用停止工作,我得到: com.example.vlad.navig2.fragments.FragmentClubs.onCreateView(FragmentClubs.java:79)就是這樣在代碼中的行: ListView listView = getListView(); –

相關問題