有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Android Java:启动活动时出现问题,错误导致类片段膨胀

我正在尝试建立一种恢复游戏的方法。我有一个小游戏。活动游戏中的xml。xml布局。我有一个错误:找不到符号变量fragment_game。有人能看到我在下面代码中提供的信息是怎么回事吗请参阅下面的更新

AndroidManifest。xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    package="org.example.whatstate" >

    <application
        安卓:allowBackup="true"
        安卓:icon="@mipmap/ic_launcher"
        安卓:label="@string/app_name"
        安卓:theme="@style/AppTheme" >
        <activity
            安卓:name=".MainActivity"
            安卓:label="@string/app_name" >
            <intent-filter>
                <action 安卓:name="安卓.intent.action.MAIN" />

                <category 安卓:name="安卓.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            安卓:name=".GameActivity" >
        </activity>

    </application>

</manifest>

我的活动是游戏。xml:

<FrameLayout
    xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:clipChildren="false"
    tools:context=".WhatStateActivity">
    <fragment
        安卓:id="@+id/game_fragment"
        class="org.example.whatstate.GameActivity"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:layout_gravity="center"
        tools:layout="@layout/fragment_game"/>
</FrameLayout>

我的碎片游戏。xml:

<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:background="@drawable/menu_background"
    安卓:elevation="@dimen/elevation_high"
    安卓:orientation="vertical"
    安卓:padding="@dimen/menu_padding"
    tools:context=".WhatStateActivity">

    <EditText
        安卓:id="@+id/ResponseText"
        安卓:layout_height="wrap_content"
        安卓:hint="@string/response_hint"
        安卓:inputType="textPersonName"
        安卓:layout_width="fill_parent"
        安卓:textSize="8pt">
    </EditText>

    <ImageView
        安卓:id="@+id/idImageViewPic"
        安卓:layout_width="match_parent"
        安卓:layout_height="342dp"
        安卓:layout_weight="1.04"
        安卓:adjustViewBounds="true"
        安卓:background="#66FFFFFF"
        安卓:maxHeight="91dip"
        安卓:maxWidth="47dip"
        安卓:padding="10dip"
        安卓:src="@drawable/alabama" />

    <Button
        安卓:id="@+id/guess_button"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:layout_margin="@dimen/menu_button_margin"
        安卓:padding="@dimen/menu_button_padding"
        安卓:text="@string/continue_label" />

</LinearLayout>

游戏活动。java代码:

public class GameActivity extends Activity {
    public static final String KEY_RESTORE = "key_restore";
    public static final String PREF_RESTORE = "pref_restore";
    private GameFragment mGameFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);
        // Restore game here...
        mGameFragment = (GameFragment) getFragmentManager().findFragmentById(R.id.fragment_game);
        boolean restore = getIntent().getBooleanExtra(KEY_RESTORE, false);
        if (restore) {
            String gameData = getPreferences(MODE_PRIVATE)
                    .getString(PREF_RESTORE, null);
            if (gameData != null) {
                mGameFragment.putState(gameData);
            }
        }
        Log.d("UT3", "restore = " + restore);
    }
}

更新1:我的问题可能完全来自其他地方。我简化了代码,并出现了以下运行时异常:

java.lang.RuntimeException: Unable to start activity ComponentInfo{org.example.whatstate/org.example.whatstate.GameActivity}: 安卓.view.InflateException: Binary XML file line #8: Error inflating class fragment

这是我的代码:

package org.example.whatstate;

import 安卓.support.v4.app.Fragment;
import 安卓.app.Activity;
import 安卓.app.AlertDialog;
import 安卓.app.Dialog;
import 安卓.content.DialogInterface;
import 安卓.os.Bundle;
import 安卓.support.v4.app.FragmentActivity;
import 安卓.util.Log;

public class GameActivity extends Activity {
    public static final String KEY_RESTORE = "key_restore";
    public static final String PREF_RESTORE = "pref_restore";
    private GameFragment mGameFragment;

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

如果我扩展FragmentActivity,同样的错误也会发生。 在activity_游戏中使用此选项时也会出现相同的错误。而不是xml:

安卓:name = "org.example.whatstate.MainFragment"

更新2 更新活动_游戏后不再出现运行时异常。xml,但解决符号的原始问题仍然存在

更新了游戏中的活动。xml:

<FrameLayout
    xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:clipChildren="false"
    tools:context=".WhatStateActivity">
    <fragment
        安卓:id="@+id/game_fragment"
        安卓:name ="org.example.whatstate.GameFragment"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:layout_gravity="center"
        tools:layout="@layout/fragment_game"/>
</FrameLayout>

共 (0) 个答案