2015-07-12 44 views
1

我剛剛使用Android Studio創建了一個空白片段。該片段稱爲ContactList和如下:在Android中使用片段導致虛擬機關閉

public class ContactList extends Fragment { 
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER 
    private static final String ARG_PARAM1 = "param1"; 
    private static final String ARG_PARAM2 = "param2"; 

    private String mParam1; 
    private String mParam2; 

    private OnFragmentInteractionListener mListener; 

    /** 
    * 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 ContactList. 
    */ 
    public static ContactList newInstance(String param1, String param2) { 
     ContactList fragment = new ContactList(); 
     Bundle args = new Bundle(); 
     args.putString(ARG_PARAM1, param1); 
     args.putString(ARG_PARAM2, param2); 
     fragment.setArguments(args); 
     return fragment; 
    } 

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

    @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) { 
     // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.fragment_contact_list, container, false); 
    } 

    public void onButtonPressed(Uri uri) { 
     if (mListener != null) { 
      mListener.onFragmentInteraction(uri); 
     } 
    } 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
     try { 
      mListener = (OnFragmentInteractionListener) activity; 
     } catch (ClassCastException e) { 
      throw new ClassCastException(activity.toString() 
        + " must implement OnFragmentInteractionListener"); 
     } 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 
     mListener = null; 
    } 

    /** 
    * This interface must be implemented by activities that contain this 
    * fragment to allow an interaction in this fragment to be communicated 
    * to the activity and potentially other fragments contained in that 
    * activity. 
    * <p/> 
    * See the Android Training lesson <a href= 
    * "http://developer.android.com/training/basics/fragments/communicating.html" 
    * >Communicating with Other Fragments</a> for more information. 
    */ 
    public interface OnFragmentInteractionListener { 
     // TODO: Update argument type and name 
     public void onFragmentInteraction(Uri uri); 
    } 

} 

我使用的片段在layout.xml如下

<fragment 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:name="com.betbro.bety.ContactList" 
     /> 

但是當我開始活動logcat的給我,而以下應用程序被凍結。

07-11 21:43:50.134 16532-16554/com.betbro.bety I/art﹕ Background partial concurrent mark sweep GC freed 5172(347KB) AllocSpace objects, 6(1403KB) LOS objects, 40% free, 9MB/15MB, paused 31.754ms total 170.494ms 
07-11 21:43:53.687 16532-16532/com.betbro.bety D/AndroidRuntime﹕ Shutting down VM 

它可能是什麼?實際上,logcat不會提供有關錯誤的更多信息。

在此先感謝

回答

1

問題是通過做兩件事來解決的。我將片段的類從 import android.app.Fragment;改爲import android.support.v4.app.Fragment; 其次,我必須實現由使用片段的活動中的片段定義的接口OnFragmentInteractionListener。似乎沒有顯示onAttach方法中的ClassCastException。