android.view.InflateException:错误膨胀类片段?

时间:2013-11-03 17:14:43

标签: android android-fragments

为了说清楚,我使用Google !所有其他问题一直在使用该API,与我无关。

总之:

我的布局(风景):

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical" >

     <fragment
        android:id="@+id/questionFragment"
        android:name="com.liamw.school.flashcards.QuestionFragment"
        android:layout_width="186dp"
        android:layout_height="match_parent"
        tools:layout="@android:layout/list_content" />

    <fragment
        android:id="@+id/answerFragment"
        android:name="com.liamw.school.flashcards.AnswerFragment"
        android:layout_width="214dp"
        android:layout_height="209dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/questionFragment" />

</RelativeLayout>

布局(人像):

<?xml version="1.0" encoding="utf-8"?>
<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="vertical" >

    <fragment
        android:id="@+id/questionFragment"
        android:name="com.liamw.school.flashcards.QuestionFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout="@android:layout/list_content" />

</LinearLayout>

主要活动:

package com.liamw.school.flashcards;

import android.app.Activity;
import android.os.Bundle;

public class MainAppActivity extends Activity {

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

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.liamw.school.flashcards"
    android:installLocation="auto"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:restoreNeedsApplication="true"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.liamw.school.flashcards.LauncherActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.liamw.school.flashcards.MainAppActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="com.liamw.school.flashcards.MAINAPPACTIVITY" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我无法弄清楚错误发生的原因。完整的logcat:

11-03 17:01:28.117: E/AndroidRuntime(14140): FATAL EXCEPTION: main
11-03 17:01:28.117: E/AndroidRuntime(14140): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.liamw.school.flashcards/com.liamw.school.flashcards.MainAppActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
11-03 17:01:28.117: E/AndroidRuntime(14140):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
11-03 17:01:28.117: E/AndroidRuntime(14140):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
11-03 17:01:28.117: E/AndroidRuntime(14140):    at android.app.ActivityThread.access$600(ActivityThread.java:153)
11-03 17:01:28.117: E/AndroidRuntime(14140):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)
11-03 17:01:28.117: E/AndroidRuntime(14140):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-03 17:01:28.117: E/AndroidRuntime(14140):    at android.os.Looper.loop(Looper.java:137)
11-03 17:01:28.117: E/AndroidRuntime(14140):    at android.app.ActivityThread.main(ActivityThread.java:5289)
11-03 17:01:28.117: E/AndroidRuntime(14140):    at java.lang.reflect.Method.invokeNative(Native Method)
11-03 17:01:28.117: E/AndroidRuntime(14140):    at java.lang.reflect.Method.invoke(Method.java:525)
11-03 17:01:28.117: E/AndroidRuntime(14140):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
11-03 17:01:28.117: E/AndroidRuntime(14140):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
11-03 17:01:28.117: E/AndroidRuntime(14140):    at dalvik.system.NativeStart.main(Native Method)
11-03 17:01:28.117: E/AndroidRuntime(14140): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
11-03 17:01:28.117: E/AndroidRuntime(14140):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
11-03 17:01:28.117: E/AndroidRuntime(14140):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
11-03 17:01:28.117: E/AndroidRuntime(14140):    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
11-03 17:01:28.117: E/AndroidRuntime(14140):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
11-03 17:01:28.117: E/AndroidRuntime(14140):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
11-03 17:01:28.117: E/AndroidRuntime(14140):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:320)
11-03 17:01:28.117: E/AndroidRuntime(14140):    at android.app.Activity.setContentView(Activity.java:1895)
11-03 17:01:28.117: E/AndroidRuntime(14140):    at com.liamw.school.flashcards.MainAppActivity.onCreate(MainAppActivity.java:11)
11-03 17:01:28.117: E/AndroidRuntime(14140):    at android.app.Activity.performCreate(Activity.java:5133)
11-03 17:01:28.117: E/AndroidRuntime(14140):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-03 17:01:28.117: E/AndroidRuntime(14140):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2293)
11-03 17:01:28.117: E/AndroidRuntime(14140):    ... 11 more
11-03 17:01:28.117: E/AndroidRuntime(14140): Caused by: java.lang.ClassCastException: com.liamw.school.flashcards.MainAppActivity@41bad178 must implement onQuestionSelectedListener
11-03 17:01:28.117: E/AndroidRuntime(14140):    at com.liamw.school.flashcards.QuestionFragment.onAttach(QuestionFragment.java:61)
11-03 17:01:28.117: E/AndroidRuntime(14140):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:883)
11-03 17:01:28.117: E/AndroidRuntime(14140):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
11-03 17:01:28.117: E/AndroidRuntime(14140):    at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1184)
11-03 17:01:28.117: E/AndroidRuntime(14140):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:291)
11-03 17:01:28.117: E/AndroidRuntime(14140):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
11-03 17:01:28.117: E/AndroidRuntime(14140):    ... 21 more

我对片段很陌生,对于一个可能很简单的问题感到抱歉。如果您需要任何其他文件,请在评论中提问。

很抱歉,但我真的找不到答案!

1 个答案:

答案 0 :(得分:1)

堆栈跟踪的最后一部分是关键信息:

  

Caused by: java.lang.ClassCastException: com.liamw.school.flashcards.MainAppActivity@41bad178 must implement onQuestionSelectedListener

QuestionFragment似乎使用MainAppActivity作为实现onQuestionSelectedListener的接口的实例,但它没有,因此崩溃。