1

只是为了说清楚,我根本没有使用谷歌!所有其他问题都在使用该 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

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

对不起,但我真的找不到答案!

4

1 回答 1

1

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

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

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

于 2013-11-03T18:26:20.197 回答