片段和活动问题

时间:2013-05-08 12:43:27

标签: android android-fragments android-activity

我正在尝试在Android应用中创建一组简单的屏幕。该应用程序以带按钮的菜单屏幕开始。在做出选择之后,我想要发起一个用2个片段构建的活动(一个用于播放器1,另一个用于播放器2)。但是,当我尝试使用片段启动活动时,我在android模拟器中出现错误。

到目前为止,这是我的代码:

主菜单代码

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;

public class MainMenuActivity extends Activity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main_menu, menu);
    return true;
}

// load up 1v1 duel activity
public void beginRegularDuel(View view)
{
    Intent intent = new Intent(this, OneVsOneDuelActivity.class);

    startActivity(intent);
}

}

1v1决斗代码:

package com.PigRam.magichelper;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class OneVsOneDuelActivity extends FragmentActivity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.one_vs_one_view);
}
}

Player One片段代码:

package com.PigRam.magichelper;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class PlayerOneDuelFragment extends Fragment {

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    return inflater.inflate(R.layout.one_vs_one_view, container, false);
}
}

和玩家二:

package com.PigRam.magichelper;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class PlayerTwoDuelFragment extends Fragment{

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    return inflater.inflate(R.layout.one_vs_one_view, container, false);
}
}

主菜单的布局

<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:gravity="center_horizontal"
android:orientation="vertical" >

<TextView
    android:id="@+id/main_menu_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/main_menu_title"
    android:textSize="50sp" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <Button
        android:id="@+id/regular_duel"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:text="@string/regular_duel"
        android:onClick="beginRegularDuel" />

    <Button
        android:id="@+id/two_headed_dragon"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="@string/two_headed_dragon" />

</LinearLayout>

</LinearLayout>

决斗画面布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" 
android:baselineAligned="false">

<fragment android:name="com.PigRam.magichelper.PlayerOneDuelFragment"
    android:id="@+id/player_one_fragment"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="match_parent"/>

<fragment android:name="com.PigRam.magichelper.PlayerTwoDuelFragment"
    android:id="@+id/player_two_fragment"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="match_parent" />
</LinearLayout>

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.PigRam.magichelper"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity android:name="com.PigRam.magichelper.MainMenuActivity"
        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.PigRam.magichelper.OneVsOneDuelActivity"
        android:label="@string/app_name">
    </activity>

</application>

</manifest>

最后但并非最不重要的是错误日志:

05-09 09:40:18.696: D/gralloc_goldfish(1521): Emulator without GPU emulation detected.
05-09 09:40:22.746: D/AndroidRuntime(1521): Shutting down VM
05-09 09:40:22.756: W/dalvikvm(1521): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
05-09 09:40:22.916: D/dalvikvm(1521): GC_CONCURRENT freed 191K, 11% free 2634K/2956K, paused 75ms+122ms, total 303ms
05-09 09:40:22.926: E/AndroidRuntime(1521): FATAL EXCEPTION: main
05-09 09:40:22.926: E/AndroidRuntime(1521): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.PigRam.magichelper/com.PigRam.magichelper.OneVsOneDuelActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
05-09 09:40:22.926: E/AndroidRuntime(1521):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-09 09:40:22.926: E/AndroidRuntime(1521):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-09 09:40:22.926: E/AndroidRuntime(1521):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-09 09:40:22.926: E/AndroidRuntime(1521):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-09 09:40:22.926: E/AndroidRuntime(1521):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-09 09:40:22.926: E/AndroidRuntime(1521):     at android.os.Looper.loop(Looper.java:137)
05-09 09:40:22.926: E/AndroidRuntime(1521):     at android.app.ActivityThread.main(ActivityThread.java:5041)
05-09 09:40:22.926: E/AndroidRuntime(1521):     at java.lang.reflect.Method.invokeNative(Native Method)
05-09 09:40:22.926: E/AndroidRuntime(1521):     at java.lang.reflect.Method.invoke(Method.java:511)
05-09 09:40:22.926: E/AndroidRuntime(1521):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-09 09:40:22.926: E/AndroidRuntime(1521):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-09 09:40:22.926: E/AndroidRuntime(1521):     at dalvik.system.NativeStart.main(Native Method)
05-09 09:40:22.926: E/AndroidRuntime(1521): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
05-09 09:40:22.926: E/AndroidRuntime(1521):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
05-09 09:40:22.926: E/AndroidRuntime(1521):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
05-09 09:40:22.926: E/AndroidRuntime(1521):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
05-09 09:40:22.926: E/AndroidRuntime(1521):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-09 09:40:22.926: E/AndroidRuntime(1521):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
05-09 09:40:22.926: E/AndroidRuntime(1521):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
05-09 09:40:22.926: E/AndroidRuntime(1521):     at android.app.Activity.setContentView(Activity.java:1881)
05-09 09:40:22.926: E/AndroidRuntime(1521):     at com.PigRam.magichelper.OneVsOneDuelActivity.onCreate(OneVsOneDuelActivity.java:13)
05-09 09:40:22.926: E/AndroidRuntime(1521):     at android.app.Activity.performCreate(Activity.java:5104)
05-09 09:40:22.926: E/AndroidRuntime(1521):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-09 09:40:22.926: E/AndroidRuntime(1521):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-09 09:40:22.926: E/AndroidRuntime(1521):     ... 11 more
05-09 09:40:22.926: E/AndroidRuntime(1521): Caused by: java.lang.ClassCastException: com.PigRam.magichelper.PlayerOneDuelFragment cannot be cast to android.support.v4.app.Fragment
05-09 09:40:22.926: E/AndroidRuntime(1521):     at android.support.v4.app.Fragment.instantiate(Fragment.java:394)
05-09 09:40:22.926: E/AndroidRuntime(1521):     at android.support.v4.app.Fragment.instantiate(Fragment.java:369)
05-09 09:40:22.926: E/AndroidRuntime(1521):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
05-09 09:40:22.926: E/AndroidRuntime(1521):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
05-09 09:40:22.926: E/AndroidRuntime(1521):     ... 21 more

对不起所有的代码,但这真的很烦我。我是Android编程的新手,所以请帮帮我!

感谢。

2 个答案:

答案 0 :(得分:1)

在您的片段类中,您应该导入

android.support.v4.app.Fragment

而不是

android.app.Fragment;

这就是它告诉你java.lang.ClassCastException: com.PigRam.magichelper.PlayerOneDuelFragment cannot be cast to android.support.v4.app.Fragment

的原因

您的活动应该延长android.support.v4.app.FragmentActivity

答案 1 :(得分:1)

您的Fragments使用的one_vs_one_view布局与您的Activity相同。你不能这样做,片段不能无限期地扩展自己。

这是你唯一的问题。我尝试通过为您的片段提供不同的布局(在TextView内仅包含RelativeLayout)来运行您的代码,并且一切正常。

我在4.2.2手机和2.1手机中试过了。

的活动:

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class OneVsOneDuelActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.one_vs_one_view);
    }
}

片段1:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class PlayerOneDuelFragment extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.hello_world, container, false);
    }
}

片段2:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class PlayerTwoDuelFragment extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.hellow_world, container, false);
    }
}