RelativeLayout.LayoutParams导致Android中的应用程序崩溃

时间:2017-07-04 09:41:53

标签: java android xml android-layout layoutparams

我有一个回收站视图,我的线性布局为父级,相对布局为子级。我正在动态地将布局参数更改为这些。我的代码在线性布局方面做得非常好,但是当涉及到相对布局时,它会崩溃。

它甚至没有显示导致崩溃的行号,经过这么多次运行后,我发现导致崩溃的行。这是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)

提前多多感谢....

3 个答案:

答案 0 :(得分:2)

它清楚地说

  

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);

答案 1 :(得分:1)

java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams

您引用线性布局但使用相对布局,显示您正在使用的xml文件或检查线性布局

答案 2 :(得分:0)

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