Java 不断膨胀的观点引发了许多问题。正在尝试将视图添加到布局。类卡斯特例外

Java 不断膨胀的观点引发了许多问题。正在尝试将视图添加到布局。类卡斯特例外,java,android,view,Java,Android,View,好吧,我不确定我做错了什么。在查找了很多方向之后,我想到了这个片段,但我不确定为什么它不起作用。如能提供解释和帮助,将不胜感激。包括LogCat日志 activity_camera.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width=

好吧,我不确定我做错了什么。在查找了很多方向之后,我想到了这个片段,但我不确定为什么它不起作用。如能提供解释和帮助,将不胜感激。包括LogCat日志

activity_camera.xml:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".CameraActivity" >



    <Button
        android:id="@+id/buttonMenu"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/capture"
        android:layout_alignBottom="@+id/capture"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="22dp"
        android:text="menu" />

    <Button
        android:id="@+id/capture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="24dp"
        android:onClick="onClick"
        android:text="Take pic" />


</RelativeLayout>
日志:

E/AndroidRuntime(  784): FATAL EXCEPTION: main
E/AndroidRuntime(  784): java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
E/AndroidRuntime(  784):    at android.widget.RelativeLayout$DependencyGraph.findRoots(RelativeLayout.java:1723)
E/AndroidRuntime(  784):    at android.widget.RelativeLayout$DependencyGraph.getSortedViews(RelativeLayout.java:1670)
E/AndroidRuntime(  784):    at android.widget.RelativeLayout.sortChildren(RelativeLayout.java:382)
E/AndroidRuntime(  784):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:403)
E/AndroidRuntime(  784):    at android.view.View.measure(View.java:15848)
E/AndroidRuntime(  784):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
E/AndroidRuntime(  784):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
E/AndroidRuntime(  784):    at android.view.View.measure(View.java:15848)
E/AndroidRuntime(  784):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
E/AndroidRuntime(  784):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
E/AndroidRuntime(  784):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
E/AndroidRuntime(  784):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
E/AndroidRuntime(  784):    at android.view.View.measure(View.java:15848)
E/AndroidRuntime(  784):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
E/AndroidRuntime(  784):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
E/AndroidRuntime(  784):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
E/AndroidRuntime(  784):    at android.view.View.measure(View.java:15848)
E/AndroidRuntime(  784):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
E/AndroidRuntime(  784):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
E/AndroidRuntime(  784):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
E/AndroidRuntime(  784):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
E/AndroidRuntime(  784):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
E/AndroidRuntime(  784):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
E/AndroidRuntime(  784):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
E/AndroidRuntime(  784):    at android.view.Choreographer.doFrame(Choreographer.java:532)
E/AndroidRuntime(  784):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
E/AndroidRuntime(  784):    at android.os.Handler.handleCallback(Handler.java:730)
E/AndroidRuntime(  784):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(  784):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(  784):    at android.app.ActivityThread.main(ActivityThread.java:5103)
E/AndroidRuntime(  784):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  784):    at java.lang.reflect.Method.invoke(Method.java:525)
E/AndroidRuntime(  784):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
E/AndroidRuntime(  784):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/AndroidRuntime(  784):    at dalvik.system.NativeStart.main(Native Method)

正如评论所暗示的,如果您发布了您正在使用的xml布局,那么它将非常有用。 但最有可能的解决方案似乎是xml布局中的顶层元素不是一个相对的元素;因此,类型转换(正确)失败


你不知道;似乎不需要相对布局的任何功能,所以我认为您应该转换为更通用的类(可能是FrameLayout?)。这将使您的代码更能适应布局格式文件中的更改。

正如注释所示,如果您发布了正在使用的xml布局,这将非常有用。 但最有可能的解决方案似乎是xml布局中的顶层元素不是一个相对的元素;因此,类型转换(正确)失败


你不知道;似乎不需要相对布局的任何功能,所以我认为您应该转换为更通用的类(可能是FrameLayout?)。这将使您的代码更能适应布局格式文件中的更改。

打开名为activity\u camera的xml文件,确保布局是相对的。如果没有,您应该更改xml文件中的布局,或者更改此行RelativeLayout RL=(RelativeLayout)LayoutFlater.from(this).inflate(R.layout.activity\u camera,null);RelativeLayout更改为FrameLayout。

打开名为activity\u camera的xml文件,确保布局为RelativeLayout。如果没有,您应该更改xml文件中的布局,或者更改此行RelativeLayout RL=(RelativeLayout)LayoutFlater.from(this).inflate(R.layout.activity\u camera,null);RelativeLayout更改为FrameLayout。

清理/重建项目时,不会出现任何错误。如果问题仍然存在,请删除您的R.java并重新生成项目。

清理/重建您的项目,它不会有任何错误。如果问题仍然存在,请删除您的R.java并重新生成项目。

将您的
onCreate更改为:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera);

    mPreferences = getSharedPreferences("CurrentUser", MODE_PRIVATE);

    RelativeLayout RL = (RelativeLayout) getWindow().getDecorView().findViewById(android.R.id.content);
    mTextureView = new TextureView(this);
    mTextureView.setSurfaceTextureListener(this);
    RL.addView(mTextureView);
}

您需要先调用
super.onCreate
。然后
将contentview
设置为布局(它是文件的id=名称,而不是相对布局)。然后,您可以在其中找到您的相对布局。

将您的
onCreate
代码更改为:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera);

    mPreferences = getSharedPreferences("CurrentUser", MODE_PRIVATE);

    RelativeLayout RL = (RelativeLayout) getWindow().getDecorView().findViewById(android.R.id.content);
    mTextureView = new TextureView(this);
    mTextureView.setSurfaceTextureListener(this);
    RL.addView(mTextureView);
}


您需要先调用
super.onCreate
。然后
将contentview
设置为布局(它是文件的id=名称,而不是相对布局)。然后你可以在里面找到你的相对布局。

谢谢大家!因此,我在activity_camera.xml中采用了这种方式,将RelativeLayout更改为FrameLayout。到现在为止,一直都还不错。但是谢谢@Szymon他的回答帮助了我的工作。

谢谢大家!因此,我在activity_camera.xml中采用了这种方式,将RelativeLayout更改为FrameLayout。到现在为止,一直都还不错。但是谢谢@Szymon,他的回答帮助了我的工作。

发布你的活动\u camera.xml为什么有重复的内容:
mPreferences=getSharedReferences(“CurrentUser”,MODE\u PRIVATE);mPreferences=getSharedReferences(“当前用户”,模式\私有)?哈,我甚至没有注意到,在将它从一个活动移动到另一个活动时发生了意外。这很好,它不会产生任何问题。@Brontok发布了xmlPost your activity\u camera.xml为什么有重复的内容:
mPreferences=getSharedReferences(“CurrentUser”,MODE\u PRIVATE);mPreferences=getSharedReferences(“当前用户”,模式\私有)?哈,我甚至没有注意到,当它从一个活动移动到另一个活动时发生了意外。这很好,它不会产生任何问题。@Brontok发布了xmlWhats的要求,也许我会修改你的代码并帮助你。我的TextureView为我提供了相机预览,因为我是这样定义的,所以我需要我的按钮来拍照或进入xml中定义的菜单。具体要求是什么,可能是我会修改您的代码并以某种方式帮助您。我的TextureView为我提供相机预览,因为我是这样定义的,所以我需要我的按钮在那里拍照或进入菜单,这些都是在xml中定义的。但我推断是这样的:
RelativeLayout RL=(RelativeLayout)findViewById(R.id.rel_布局)
更新了我的答案,请重试。
android.widget.FrameLayout无法强制转换为android.widget.RelativeLayout
相同的内容。这个框架在哪里?不知道!尝试这个问题的不同答案:我的代码中没有任何地方,都在android库中。但我推断是这样的:
RelativeLayout RL=(RelativeLayout)findViewById(R.id.rel_布局)
更新了我的答案,请重试。
android.widget.FrameLayout无法强制转换为android.widget.RelativeLayout
相同的内容。这个框架在哪里?不知道!尝试此问题的不同答案: