Android .LinearLayout $ LayoutParams无法转换为.FrameLayout $ LayoutParams

时间:2018-04-10 13:16:33

标签: android classcastexception layoutparams

每当我尝试以编程方式创建布局时,都会出现以下错误:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.appdev.vaidas.RMA, PID: 7024
              java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams
                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:186)
                  at android.widget.ScrollView.onMeasure(ScrollView.java:340)
                  at android.view.View.measure(View.java:19857)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
                  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                  at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1117)
                  at android.widget.LinearLayout.onMeasure(LinearLayout.java:642)
                  at android.view.View.measure(View.java:19857)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
                  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                  at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
                  at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
                  at android.view.View.measure(View.java:19857)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
                  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                  at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1117)
                  at android.widget.LinearLayout.onMeasure(LinearLayout.java:642)
                  at android.view.View.measure(View.java:19857)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
                  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                  at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
                  at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
                  at android.view.View.measure(View.java:19857)
                  at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1293)
                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                  at android.widget.ScrollView.onMeasure(ScrollView.java:340)
                  at android.view.View.measure(View.java:19857)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
                  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                  at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
                  at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
                  at android.view.View.measure(View.java:19857)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
                  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                  at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1117)
                  at android.widget.LinearLayout.onMeasure(LinearLayout.java:642)
                  at android.view.View.measure(View.java:19857)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                  at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
                  at android.view.View.measure(View.java:19857)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
                  at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:400)
                  at android.view.View.measure(View.java:19857)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                  at android.view.View.measure(View.java:19857)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
                  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                  at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
                  at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
                  at android.view.View.measure(View.java:19857)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                  at com.android.internal.policy.DecorView.onMeasure(DecorView.java:689)
                  at android.view.View.measure(View.java:19857)
                  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2275)
E/AndroidRuntime:     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1366)
                      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1619)
                      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
                      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6337)
                      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:6119)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

我不在任何地方使用FrameLayout,但我想这与将ScrollView添加到布局有关。我尝试取出ScrollView并且没有出现错误。

代码:

private void createLayout(WaiterApp app, Order order) {
    LinearLayout layout = this;
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, 585, 0.5f);
    params.setMargins(10, 0, 10, 10);
    layout.setLayoutParams(params);
    layout.setOrientation(VERTICAL);

    TextView title = new TextView(app.getApplicationContext());
    params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 125);
    title.setBackgroundColor(getResources().getColor(R.color.colorButton, null));
    title.setLayoutParams(params);
    title.setGravity(Gravity.CENTER);
    title.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_START);
    title.setTextColor(getResources().getColor(R.color.colorTextSecondary, null));
    // Format a text
    String text;
    User servedBy = app.getDB().getUser(order.getWaiterID());
    text = "<p> <b>" + getResources().getString(R.string.table_no) + "</b> " + order.getTable().toString() + "<br />"
            + "<b>" + getResources().getString(R.string.waiter_name) + "</b> " + servedBy.getFirstName() + " " + servedBy.getLastName() + "<br />"
            + "<b>" + getResources().getString(R.string.status) + "</b> " + order.getStatus() + "</p>";
    // Assign the text
    title.setText(Html.fromHtml(text, Html.FROM_HTML_MODE_LEGACY));
    layout.addView(title);

    // Body
    LinearLayout body = new LinearLayout(app.getApplicationContext());
    params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 460);
    body.setLayoutParams(params);
    body.setBackgroundColor(getResources().getColor(R.color.colorButtonSelected, null));

    ScrollView scrollView = new ScrollView(app.getApplicationContext());
    params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    scrollView.setLayoutParams(params);
    scrollView.setFillViewport(true);
    scrollView.setPadding(0, 0, 0, 20);

    TextView body_text = new TextView(app.getApplicationContext());
    ScrollView.LayoutParams body_params = new ScrollView.LayoutParams(ScrollView.LayoutParams.MATCH_PARENT, ScrollView.LayoutParams.WRAP_CONTENT);
    body_params.setMargins(10, 0, 10, 10);
    body_text.setLayoutParams(body_params);
    body_text.setTextColor(getResources().getColor(R.color.colorTextSecondary, null));

    // Format text based on items that belong to the order
    StringBuilder sb = new StringBuilder();
    ArrayList<OrderItem> items = app.getDB().getOrderItems(order.getID());
    for (OrderItem i : items) {
        Item item = app.getDB().getItem(i.getItemID());
        // First line
        sb.append(i.getQuantity());
        sb.append("x ");
        sb.append(item.getName());
        // Second line
        if (i.getComment().length() != 0) {
            sb.append("\n");
            sb.append("(");
            sb.append(i.getComment());
            sb.append(")");
        }
        sb.append("\n\n");
    }
    body_text.setText(sb);
    scrollView.addView(body_text);
    body.addView(scrollView);
    layout.addView(body);

    // Remove order button (only shows if order was placed by the same user)
    if (order.getWaiterID() == app.getUser().getID()) {
        // Make body_text smaller
        params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 400);
        params.setMargins(10, 135, 10, 0);
        body_text.setLayoutParams(params);

        TextView remove_text = new TextView(app.getApplicationContext());
        params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 80);
        params.setMargins(0, 505, 0, 0);
        remove_text.setLayoutParams(params);
        remove_text.setBackgroundColor(getResources().getColor(R.color.colorRed, null));
        remove_text.setTextSize(15);
        remove_text.setTextColor(getResources().getColor(R.color.colorWhite, null));
        remove_text.setGravity(Gravity.CENTER);
        remove_text.setText(getResources().getString(R.string.order_remove));
        layout.addView(remove_text);
    }
}

该函数创建的布局作为子项添加到另一个LinearLayout。 如果有人能解释我做错了什么,我会非常感激。目前我无法看到我出错的地方。

1 个答案:

答案 0 :(得分:1)

注意: - ScrollView父级是FrameLayout。所以,LayoutParams使用Framelayout

ScrollView scrollView = new ScrollView(this);
FrameLayout.LayoutParams layoutParams= new 
FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, 
FrameLayout.LayoutParams.WRAP_CONTENT);
scrollView.setLayoutParams(params);