有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java膨胀视图问题。正在尝试将视图添加到布局。类卡斯特例外

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

活动摄像机。xml:

<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:paddingBottom="@dimen/activity_vertical_margin"
    安卓:paddingLeft="@dimen/activity_horizontal_margin"
    安卓:paddingRight="@dimen/activity_horizontal_margin"
    安卓:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".CameraActivity" >



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

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


</RelativeLayout>

这是给我一个错误的代码片段

protected void onCreate(Bundle savedInstanceState) {
        mPreferences = getSharedPreferences("CurrentUser", MODE_PRIVATE); mPreferences = getSharedPreferences("CurrentUser", MODE_PRIVATE);
        super.onCreate(savedInstanceState);

        RelativeLayout RL = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.activity_camera, null);
        mTextureView = new TextureView(this);
        mTextureView.setSurfaceTextureListener(this);
        RL.addView(mTextureView);
        setContentView(RL);

日志:

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

共 (5) 个答案

  1. # 1 楼答案

    打开名为activity_camera的xml文件,确保布局是相对的。如果没有,您应该更改xml文件中的布局,或者更改此行RelativeLayout RL=(RelativeLayout)LayoutFlater。从(这个)。充气(R.layout.activity\u摄像头,空);RelativeLayout更改为FrameLayout

  2. # 2 楼答案

    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。然后setContentView到您的布局(它是您的文件的id=名称,而不是您的相对布局)。然后你可以在里面找到你的相对布局

  3. # 3 楼答案

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

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

  4. # 4 楼答案

    谢谢大家!!所以我在我的活动摄像头里这样做了。xml i从RelativeLayout更改为FrameLayout。到现在为止,一直都还不错。但是谢谢@Szymon他的回答帮助了我的工作

  5. # 5 楼答案

    清理/重建项目,它没有任何错误。如果问题仍然存在,请删除R.java并重新构建项目