Matthew Pigram
Matthew Pigram

Reputation: 1430

Fragment and Activity Issues

I am trying to create a simple set of screens in an android app. The app begins with a menu screen with buttons. After a choice is made I then want to launch an activity built with 2 fragments (one for player 1 and another for player 2). However when I try and start the activity with the fragments I get an error in the android emulator.

Here is my code so far:

The main menu code

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);
}

}

The 1v1 Duel Code:

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);
}
}

The Player One fragment code:

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);
}
}

And Player Two:

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);
}
}

And layout for main menu

<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>

And the Duel Screen layout

<?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>

Also the manifest

<?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>

And last but not least here is the error log:

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

Sorry for all the code but this is really bugging me. I am new to Android programming so please help me out!

Thanks.

Upvotes: 0

Views: 844

Answers (2)

Ali
Ali

Reputation: 12674

Your Fragments are using the one_vs_one_view layout which is the same as your Activity. You cannot do this, the fragment can't keep expanding itself indefinitely.

This is your only issue. I've tried to run your code by giving your fragments a different layout (one that only contains a TextView inside a RelativeLayout) and everything works fine.

I tried it in a 4.2.2 phone and a 2.1 phone.

Activity:

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);
    }
}

Fragment 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);
    }
}

Fragment 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);
    }
}

Upvotes: 1

Nermeen
Nermeen

Reputation: 15973

In your fragment classes you should import

android.support.v4.app.Fragment

instead of

android.app.Fragment;

that's why it's telling you java.lang.ClassCastException: com.PigRam.magichelper.PlayerOneDuelFragment cannot be cast to android.support.v4.app.Fragment

And your activity should extend android.support.v4.app.FragmentActivity

Upvotes: 1

Related Questions