Thursday, May 9, 2013

[android help] 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.app.Activity;
import android.os.Bundle;

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.annotation.TargetApi;
import android.app.Fragment;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


@TargetApi(Build.VERSION_CODES.HONEYCOMB)
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.annotation.TargetApi;
import android.app.Fragment;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


@TargetApi(Build.VERSION_CODES.HONEYCOMB)
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



xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >

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

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

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

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






And the Duel Screen layout




android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:baselineAligned="false">

android:id="@+id/player_one_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"/>

android:id="@+id/player_two_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />



Also the manifest




package="com.PigRam.magichelper"
android:versionCode="1"
android:versionName="1.0" >

android:minSdkVersion="8"
android:targetSdkVersion="17" />

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

android:label="@string/app_name" >







android:label="@string/app_name">







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.



.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...