2015-07-12 16 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。