我刚刚使用Android Studio创建了一个空白片段。该片段称为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
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不会提供有关错误的更多信息。
提前致谢
答案 0 :(得分:1)
通过做两件事来解决问题。我更改了Fragment的类:
import android.app.Fragment;
到import android.support.v4.app.Fragment;
其次,我必须实现使用Fragment的Activity中Fragment定义的接口OnFragmentInteractionListener
。似乎没有显示onAttach方法中的ClassCastException。