Java RelativeLayout.LayoutParams导致android应用程序崩溃

Java RelativeLayout.LayoutParams导致android应用程序崩溃,java,android,xml,android-layout,layoutparams,Java,Android,Xml,Android Layout,Layoutparams,我有一个回收器视图,我有一个线性布局作为父视图,一个相对布局作为子视图。我正在将布局参数动态更改为这些参数。我的代码在线性布局上运行得很好,但当涉及到相对布局时,它就崩溃了 它甚至没有显示导致崩溃的线路号,在运行了这么多次之后,我找出了导致崩溃的线路。这是recycler视图适配器类 @Override public void onBindViewHolder(Holder holder, int position) { ChatListItem listItem

我有一个回收器视图,我有一个线性布局作为父视图,一个相对布局作为子视图。我正在将布局参数动态更改为这些参数。我的代码在线性布局上运行得很好,但当涉及到相对布局时,它就崩溃了

它甚至没有显示导致崩溃的线路号,在运行了这么多次之后,我找出了导致崩溃的线路。这是recycler视图适配器类

    @Override
    public void onBindViewHolder(Holder holder, int position) {
        ChatListItem listItem = listdata.get(position);
        LinearLayout parent=holder.parent;
        RelativeLayout chatView=holder.relLayout;
        holder.time.setText(listItem.time);
        holder.message.setText(listItem.message);
        if (!listItem.sentByCustomer) {
            int match_parent= LinearLayout.LayoutParams.MATCH_PARENT;
            int wrap_content= LinearLayout.LayoutParams.WRAP_CONTENT;
            chatView.setBackground(context.getResources().getDrawable(R.drawable.incoming_normal));

            LinearLayout.LayoutParams parentParams=new LinearLayout.LayoutParams(match_parent,wrap_content);
            parentParams.setMarginStart(5);
            parent.setLayoutParams(parentParams);

            int rel_wrap_content=RelativeLayout.LayoutParams.WRAP_CONTENT;
            RelativeLayout.LayoutParams relParams=new RelativeLayout.LayoutParams(rel_wrap_content,rel_wrap_content);
            relParams.setMarginEnd(50);




            chatView.setLayoutParams(relParams); // This line is  causing crash



        }
    }



    class Holder extends RecyclerView.ViewHolder  {
        LinearLayout parent;
        RelativeLayout relLayout;
        TextView message, time;

        public Holder(View itemView) {
            super(itemView);
            parent=(LinearLayout)itemView.findViewById(R.id.chat_view_parent);
            relLayout = (RelativeLayout) itemView.findViewById(R.id.customer_chat_view);
            message = (TextView) itemView.findViewById(R.id.customer_chat_message);
            time = (TextView) itemView.findViewById(R.id.chat_time);
        }
    }
}
一切正常布局参数属于相对布局参数,我将它们设置为相对布局,我还交叉检查了xml文件,该文件也是相对布局。错误日志仍然指出它无法将线性布局参数转换为相对布局参数。即使调试器也将其称为相对布局,但为什么我会出现错误?这是错误日志

07-04 15:09:13.609 5441-5441/com.motowala E/UncaughtException: java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams
                                                                   at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1071)
                                                                   at android.widget.LinearLayout.onMeasure(LinearLayout.java:642)
                                                                   at android.view.View.measure(View.java:19861)
                                                                   at android.support.v7.widget.RecyclerView$LayoutManager.measureChildWithMargins(RecyclerView.java:8636)
                                                                   at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1577)
                                                                   at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1511)
                                                                   at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:570)
                                                                   at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3583)
                                                                   at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3312)
                                                                   at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3844)
                                                                   at android.view.View.layout(View.java:17641)
                                                                   at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                   at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
                                                                   at android.view.View.layout(View.java:17641)
                                                                   at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                   at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                                   at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                                   at android.view.View.layout(View.java:17641)
                                                                   at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                   at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
                                                                   at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                                                                   at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                                                                   at android.view.View.layout(View.java:17641)
                                                                   at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                   at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                                   at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                                   at android.view.View.layout(View.java:17641)
                                                                   at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                   at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
                                                                   at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                                                                   at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                                                                   at android.view.View.layout(View.java:17641)
                                                                   at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                   at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                                   at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                                   at com.android.internal.policy.DecorView.onLayout(DecorView.java:728)
                                                                   at android.view.View.layout(View.java:17641)
                                                                   at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                   at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2346)
                                                                   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2068)
                                                                   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
                                                                   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6343)
                                                                   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
                                                                   at android.view.Choreographer.doCallbacks(Choreographer.java:686)
                                                                   at android.view.Choreographer.doFrame(Choreographer.java:621)
                                                                   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
                                                                   at android.os.Handler.handleCallback(Handler.java:751)
                                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                   at android.os.Looper.loop(Looper.java:154)
                                                                   at android.app.ActivityThread.main(ActivityThread.java:6176)
                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
07-04 15:09:13.613 5441-5502/com.motowala D/FA: Logging event (FE): _ae, Bundle[{_o=crash, _sc=CustomerChatActivity, _si=6120152536016377641, timestamp=1499161153609, fatal=1}]
07-04 15:09:13.906 5441-5441/com.motowala E/AndroidRuntime: FATAL EXCEPTION: main
                                                            Process: com.motowala, PID: 5441
                                                            java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams
                                                                at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1071)
                                                                at android.widget.LinearLayout.onMeasure(LinearLayout.java:642)
                                                                at android.view.View.measure(View.java:19861)
                                                                at android.support.v7.widget.RecyclerView$LayoutManager.measureChildWithMargins(RecyclerView.java:8636)
                                                                at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1577)
                                                                at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1511)
                                                                at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:570)
                                                                at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3583)
                                                                at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3312)
                                                                at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3844)
                                                                at android.view.View.layout(View.java:17641)
                                                                at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
                                                                at android.view.View.layout(View.java:17641)
                                                                at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                                at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                                at android.view.View.layout(View.java:17641)
                                                                at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
                                                                at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                                                                at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                                                                at android.view.View.layout(View.java:17641)
                                                                at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                                at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                                at android.view.View.layout(View.java:17641)
                                                                at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
                                                                at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                                                                at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                                                                at android.view.View.layout(View.java:17641)
                                                                at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                                at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                                at com.android.internal.policy.DecorView.onLayout(DecorView.java:728)
                                                                at android.view.View.layout(View.java:17641)
                                                                at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2346)
                                                                at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2068)
                                                                at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
                                                                at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6343)
                                                                at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
                                                                at android.view.Choreographer.doCallbacks(Choreographer.java:686)
                                                                at android.view.Choreographer.doFrame(Choreographer.java:621)
                                                                at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
                                                                at android.os.Handler.handleCallback(Handler.java:751)
                                                                at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                at android.os.Looper.loop(Looper.java:154)
                                                                at android.app.ActivityThread.main(ActivityThread.java:6176)
                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
提前多谢

java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams
您正在引用线性布局,但使用相对布局,显示正在使用的xml文件或检查线性布局,它清楚地表明

android.widget.RelativeLayout$LayoutParams不能强制转换为 android.widget.LinearLayout$LayoutParams

您正在尝试将LinearLayout.LayoutParams强制转换为RelativeLayout.LayoutParams。这就像试图把苹果变成橙色

更改此项:

int match_parent= LinearLayout.LayoutParams.MATCH_PARENT;
int wrap_content= LinearLayout.LayoutParams.WRAP_CONTENT;
chatView.setBackground(context.getResources().getDrawable(R.drawable.incoming_normal));

LinearLayout.LayoutParams parentParams=new LinearLayout.LayoutParams(match_parent,wrap_content);
parentParams.setMarginStart(5);
parent.setLayoutParams(parentParams);
然后试着跑开

int match_parent= RelativeLayout.LayoutParams.MATCH_PARENT;
int wrap_content= RelativeLayout.LayoutParams.WRAP_CONTENT;
chatView.setBackground(context.getResources().getDrawable(R.drawable.incoming_normal));

RelativeLayout.LayoutParams parentParams=new RelativeLayout.LayoutParams(match_parent,wrap_content);
parentParams.setMarginStart(5);
parent.setLayoutParams(parentParams);

我通过删除根线性布局并将相对布局设置为根来解决问题。但我仍然不知道发生错误的原因:(

我可以知道我的问题被否决的原因吗?relLayout=(RelativeLayout)itemView.findViewById(R.id.customer\u chat\u view);检查该id是否对应于相对布局是的,对应于相对布局。但该相对布局位于线性布局内?是否会导致这样的错误?否,我在xml中使用相对布局,该相对布局位于线性布局内。阅读问题时,您漏了一行。实际情况是该int rel_wrap_内容=RelativeLayout.LayoutParams.WRAP_内容;RelativeLayout.LayoutParams relParams=新的RelativeLayout.LayoutParams(rel_WRAP_内容,rel_WRAP_内容);relParams.setMarginEnd(50);我正在将rel_wrap_content设置为相对布局参数而不是线性布局的wrap_contentparam@bharath你应该将参数作为你的父布局。如果你的父布局为RelativeLayout,你应该使用RelativeLayout参数。谢谢你,也许这就是应用崩溃的原因。我通过删除根布局并创建作为根的相对布局