[英]Java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.FrameLayout
I try to create unity Subview for my android app. 我尝试为我的Android应用程序创建统一的Subview。 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 这是我的Java代码
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: 这是我的xml文件:
<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. R.id.frameLayout2
不是框架布局。 Look at your XML again. 再次查看您的XML。
That means the line: FrameLayout layout = (FrameLayout) findViewById(R.id.frameLayout2);
这意味着一行: FrameLayout layout = (FrameLayout) findViewById(R.id.frameLayout2);
is wrong, so you have a ClassCastException
是错误的,所以您有一个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());
}
The Problem: 问题:
Basically you have tried to cast a Framelayout to a Relativelayout, in your xml you can see it is a relativelayout: 基本上,您尝试将Framelayout强制转换为Relativelayout,在xml中,您可以看到它是相对布局:
<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. 因为它实际上是一个Relativelayout,而不是一个Framelayout,即使您将其称为“ framelayout2”,它仍然是一个Relativelayout。
Solution 1: 解决方案1:
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: 将XML文件中的Relativelayout更改为framelayout,这很简单:
New XML File: 新的XML文件:
<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. 这将完全解决您的问题,而无需执行其他步骤。
Solution 2: 解决方案2:
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. 您也可以在Java中更改代码以反映它是相对布局,但是这样做并没有遵循您的教程那么多,但很高兴知道。
New Java: 新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. 这也应该起作用,但是再次使它成为无框架布局的相对布局,这样可能会弄乱您的教程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.