android.view.InflateException:二进制XML文件行#9:错误膨胀类片段

时间:2014-02-20 18:41:18

标签: android

有很多文章指出了同样的错误,但似乎都没有解决我的问题。所以我发布了它

我有一个片段布局

<FrameLayout 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"
tools:context=".MyAppointmentFragment" >

<!-- TODO: Update blank fragment layout -->

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="@string/fragment_text" />

</FrameLayout>

以下的活动布局

 <LinearLayout 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:orientation="horizontal"
    android:baselineAligned="false"
    tools:context=".IndexActivity" 
   >
    <fragment 
    android:name="com.example.helloworld.MyAppointmentFragment" 
    android:id="@+id/myappointment_fragment"              
    android:layout_weight="1"              
    android:layout_width="0dp"              
    android:layout_height="match_parent" />
 </LinearLayout>

以下是引发此错误的活动的java源代码

package com.example.helloworld;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;

public class IndexActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_index);
      }
}

package com.example.helloworld;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
* A simple {@link android.support.v4.app.Fragment} subclass. Activities that
* contain this fragment must implement the
* {@link MyAppointmentFragment.OnFragmentInteractionListener} interface to
* handle interaction events. Use the {@link MyAppointmentFragment#newInstance}
* factory method to create an instance of this fragment.
* 
*/
public class MyAppointmentFragment 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 MyAppointmentFragment.
 */
// TODO: Rename and change types and number of parameters
public static MyAppointmentFragment newInstance(String param1, String param2) {
    MyAppointmentFragment fragment = new MyAppointmentFragment();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

public MyAppointmentFragment() {
    // 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_my_appointment, container,
            false);
}

// 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);
}

  }

在模拟器中启动时,我收到以下错误

02-20 13:21:42.530: E/AndroidRuntime(1290): FATAL EXCEPTION: main
02-20 13:21:42.530: E/AndroidRuntime(1290): java.lang.RuntimeException: Unable to start   
activity   
ComponentInfo{com.example.helloworld/com.example.helloworld.IndexActivity}:  
android.view.InflateException: Binary XML file line #9: Error inflating class fragment
02-20 13:21:42.530: E/AndroidRuntime(1290):     at   
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)

我使用的Android版本是4.3,minSdkVersion =“13”和targetSdkVersion =“18”

感谢您解决此问题的任何帮助

完成下面的堆栈跟踪

    FATAL EXCEPTION: main
 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloworld/com.example.helloworld.IndexActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
    at android.app.ActivityThread.access$600(ActivityThread.java:141)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5103)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
    at android.app.Activity.setContentView(Activity.java:1895)
    at com.example.helloworld.IndexActivity.onCreate(IndexActivity.java:11)
    at android.app.Activity.performCreate(Activity.java:5133)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
    ... 11 more
 Caused by: java.lang.ClassCastException: com.example.helloworld.IndexActivity@4174c9f8 must implement OnFragmentInteractionListener
    at com.example.helloworld.MyAppointmentFragment.onAttach(MyAppointmentFragment.java:85)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:883)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
    at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1184)
    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
    ... 21 more

3 个答案:

答案 0 :(得分:7)

java.lang.ClassCastException: com.example.helloworld.IndexActivity@4174c9f8 must implement OnFragmentInteractionListener

你的IndexActivity类应该实现一些OnFragmentInteractionListener接口,但它没有。

答案 1 :(得分:1)

我的回答很晚,因为我也遇到了同样的问题,没有解决办法对我有用。如果onFragmentInteractionListener()等其他区域都没有问题,请检查 public void onAttach(Activity activity) { super.onAttach(activity); try { mListener = (OnFragmentInteractionListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnFragmentInteractionListener"); } } 因为不推荐使用此方法,请将其替换为

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

答案 2 :(得分:0)

确保您有一个名为fragment_text

的字符串
android:text="@string/fragment_text" 

在values文件夹的strings.xml文件中。即使你有,也尝试做一个“干净”的项目。