I try to create unity Subview for my android app. But I got a error like this
E/AndroidRuntime(1750): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.duy/com.duy.MainActivity}: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.FrameLayout
This is my Java code
package com.duy;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout.LayoutParams;
import com.unity3d.player.UnityPlayer;
public class MainActivity extends Activity {
private UnityPlayer m_UnityPlayer;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create the UnityPlayer
m_UnityPlayer = new UnityPlayer(this);
int glesMode = m_UnityPlayer.getSettings().getInt("gles_mode", 1);
boolean trueColor8888 = false;
m_UnityPlayer.init(glesMode, trueColor8888);
setContentView(R.layout.main);
// Add the Unity view
FrameLayout layout = (FrameLayout) findViewById(R.id.frameLayout2);
LayoutParams lp = new LayoutParams (LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
layout.addView(m_UnityPlayer.getView(), 0, lp);
}
}
and this is my xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frameLayout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</RelativeLayout>
And I got the following error:
06-24 09:20:41.680: E/AndroidRuntime(1913): FATAL EXCEPTION: main
06-24 09:20:41.680: E/AndroidRuntime(1913): Process: com.duy, PID: 1913
06-24 09:20:41.680: E/AndroidRuntime(1913): java.lang.Error: FATAL EXCEPTION [main]
06-24 09:20:41.680: E/AndroidRuntime(1913): Unity version : 5.1.1f1
06-24 09:20:41.680: E/AndroidRuntime(1913): Device model : unknown Android SDK built for x86
06-24 09:20:41.680: E/AndroidRuntime(1913): Device fingerprint: generic_x86/sdk_x86/generic_x86:4.4.2/KK/999428:eng/test-keys
06-24 09:20:41.680: E/AndroidRuntime(1913): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.duy/com.duy.MainActivity}: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.FrameLayout
06-24 09:20:41.680: E/AndroidRuntime(1913): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-24 09:20:41.680: E/AndroidRuntime(1913): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-24 09:20:41.680: E/AndroidRuntime(1913): at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-24 09:20:41.680: E/AndroidRuntime(1913): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-24 09:20:41.680: E/AndroidRuntime(1913): at android.os.Handler.dispatchMessage(Handler.java:102)
06-24 09:20:41.680: E/AndroidRuntime(1913): at android.os.Looper.loop(Looper.java:136)
06-24 09:20:41.680: E/AndroidRuntime(1913): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-24 09:20:41.680: E/AndroidRuntime(1913): at java.lang.reflect.Method.invokeNative(Native Method)
06-24 09:20:41.680: E/AndroidRuntime(1913): at java.lang.reflect.Method.invoke(Method.java:515)
06-24 09:20:41.680: E/AndroidRuntime(1913): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-24 09:20:41.680: E/AndroidRuntime(1913): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-24 09:20:41.680: E/AndroidRuntime(1913): at dalvik.system.NativeStart.main(Native Method)
06-24 09:20:41.680: E/AndroidRuntime(1913): Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.FrameLayout
06-24 09:20:41.680: E/AndroidRuntime(1913): at com.duy.MainActivity.onCreate(MainActivity.java:30)
06-24 09:20:41.680: E/AndroidRuntime(1913): at android.app.Activity.performCreate(Activity.java:5231)
06-24 09:20:41.680: E/AndroidRuntime(1913): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-24 09:20:41.680: E/AndroidRuntime(1913): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-24 09:20:41.680: E/AndroidRuntime(1913): ... 11 more
How can I solve this problem ?
R.id.frameLayout2
is NOT a frame layout. Look at your XML again.
That means the line: FrameLayout layout = (FrameLayout) findViewById(R.id.frameLayout2);
is wrong, so you have a ClassCastException
But if all you want is to have the unity player view, you can use just that:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create the UnityPlayer
m_UnityPlayer = new UnityPlayer(this);
int glesMode = m_UnityPlayer.getSettings().getInt("gles_mode", 1);
boolean trueColor8888 = false;
m_UnityPlayer.init(glesMode, trueColor8888);
setContentView(m_UnityPlayer.getView());
}
Basically you have tried to cast a Framelayout to a Relativelayout, in your xml you can see it is a relativelayout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frameLayout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</RelativeLayout><!--Notice it says Relativelayout-->
You can't do this:
FrameLayout layout = (FrameLayout) findViewById(R.id.frameLayout2);
Because it is actually a Relativelayout, not a Framelayout, even though you called it 'framelayout2' it is still a Relativelayout.
I recommend this one as you end up with a framelayout and that follows your tutorial more accurately. Change the Relativelayout in the XML file into a framelayout, it's very simple:
New XML File:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frameLayout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</FrameLayout><!--Notice it says FrameLayout Now.-->
This will completely solve your problem, with no extra steps.
You could also change the code in you Java to reflect that it is a relativelayout, however this doesn't follow your tutorial as much but it's good to know.
New Java:
RelativeLayout layout = (RelativeLayout) findViewById(R.id.framelayout2);
That should also work, but again that makes it a relativelayout no a framelayout so it might mess up your tutorial.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.