为了简单起见,我根本没有使用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>


我不知道为什么会发生错误。完整日志:

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


我对片段很陌生,因此很抱歉提出一个简单的问题。如果您需要其他文件,只需在评论中提问。

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

最佳答案

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


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


QuestionFragment似乎将MainAppActivity用作实现onQuestionSelectedListener的接口的实例,但是没有,因此导致崩溃。

10-01 17:34