android - 在 Android 中使用 Fragment 导致 VM 关闭

标签 android fragment android-appcompat

我刚刚使用 Android Studio 创建了一个空白 fragment 。该 fragment 称为 ContactList,如下所示:

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 如下

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

但是当我启动 Activity 时,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 不会提供有关错误的更多信息。

提前致谢

最佳答案

通过做两件事解决了这个问题。我将 Fragment 的类从: import android.app.Fragment;import android.support.v4.app.Fragment; 其次,我必须在使用 Fragment 的 Activity 中实现由 Fragment 定义的接口(interface) OnFragmentInteractionListener。似乎未显示方法 onAttach 中的 ClassCastException。

关于android - 在 Android 中使用 Fragment 导致 VM 关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31363326/

相关文章:

android - Android 应用程序中的系统身份验证

android - 在 Activity 之间滑动

java.lang.IllegalStateException : Fragment already added :

android - 如何在运行时检查支持库/AppCompat 库版本?

android - 向下滚动时 CollapsingToolbarLayout 闪烁

java - 初学者android帮助从可运行内部播放音频

android - AppCompat V21 spinner popup background is white with white text only in Android 4.0

java - "Field can be converted to a local variable"设置Android ActionBar颜色时出现消息

java - TCP 客户端不适用于 Android

android - 坚持从 Activity 移植到 fragment