我通过右键单击我的项目并选择新的>创建了一个新活动。片段>空白片段。
这是自动生成的片段。我添加的唯一内容是onCreateView()
方法下的行。我还在我要将此片段附加到的活动上实现了onFragmentInteractionListener。我确保在该Activity上实现onFragmentInteraction(...)
方法,但将其留空。
这里的问题是什么?
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.support.v4.app.Fragment;
public class FragmentHowToPlay extends Fragment {
// 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";
// TODO: Rename and change types of parameters
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 FragmentHowToPlay.
*/
// TODO: Rename and change types and number of parameters
public static FragmentHowToPlay newInstance(String param1, String param2) {
FragmentHowToPlay fragment = new FragmentHowToPlay();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
public FragmentHowToPlay() {
// 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
View v = inflater.inflate(R.layout.fragment_fragment_how_to_play, container, false);
TextView text1 = (TextView) v.findViewById(R.id.text1);
text1.setText(Html.fromHtml(getString(R.string.my_text)));
return v;
}
// 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 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);
}
}
这是logcat错误:
Process: mjj.fling, PID: 8965
java.lang.RuntimeException: Unable to start activity ComponentInfo{mjj.fling/mjj.fling.HowToPlayActivity}: android.view.InflateException: Binary XML file line #19: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2441)
at android.app.ActivityThread.access$800(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5431)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:913)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:706)
Caused by: android.view.InflateException: Binary XML file line #19: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:402)
at android.app.Activity.setContentView(Activity.java:2214)
at mjj.fling.HowToPlayActivity.onCreate(HowToPlayActivity.java:16)
at android.app.Activity.performCreate(Activity.java:6093)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class mjj.fling.FragmentHowToPlay that is not a Fragment
at android.app.Fragment.instantiate(Fragment.java:606)
at android.app.Fragment.instantiate(Fragment.java:582)
at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2108)
at android.app.Activity.onCreateView(Activity.java:5431)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
Caused by: java.lang.ClassCastException
对于将片段附加到的活动,XML LAYOUT
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="mjj.fling.HowToPlayActivity"
android:background="#01051D">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scrollView"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="42dp" >
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="mjj.fling.FragmentHowToPlay"
android:id="@+id/fragment"
tools:layout="@layout/fragment_fragment_how_to_play" />
</ScrollView>
答案 0 :(得分:2)
您片段扩展android.support.v4.app.Fragment
而不是android.app.Fragment
- 请参阅导入声明。前者应由支持库中的FragmentActivity
(或更好的新AppCompatActivity
}使用,而不是常规android.app.Activity
。您需要更改其中一个,并且通常与您在整个应用程序中使用的内容保持一致。