2014-02-20 55 views
2

有很多文章指向相同的錯誤,但似乎沒有解決我的問題。所以我張貼它android.view.InflateException:二進制XML文件行#9:錯誤充氣類片段

我有以下

<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: [email protected] 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 
+0

更換請張貼整個堆棧跟蹤。 – CommonsWare

+0

修改原始帖子以包含完整的堆棧跟蹤。我注意到從堆棧跟蹤它 – user1208616

+0

抱怨必須在com.example.helloworld.MyAppointmentFragment.onAttach – user1208616

回答

5
java.lang.ClassCastException: [email protected] must implement OnFragmentInteractionListener 

IndexActivity類應該實現一些OnFragmentInteractionListener接口,並且它沒有。

+0

實現OnFragmentInteractionListener感謝指出。 – user1208616

0

確保您有一個名爲fragment_text

android:text="@string/fragment_text" 

位於values文件夾中的strings.xml文件中。即使你有,也嘗試做一個'乾淨'的項目。

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"); 
    } 
} 
相關問題