0

我想做 4 列动态变化的数据行。我会用 LinearLayout 来做到这一点,但我找不到获得一组 4x10 文本视图的好方法。此 .java 引发了一个致命错误,该错误列在底部:

public class DebtList extends Activity {

Integer trigger = 5;
Double totalDebt = 0.00;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.debtlist);

    //Bundle extras = getIntent().getExtras();

    String[] debtNames = new String[10];
    Double[] debtAmount = new Double[10];
    Double[] debtRate = new Double[10];
    Double[] debtTerm = new Double[10];

    Integer stopper = trigger+1;

    TableLayout tl=(TableLayout)findViewById(R.id.debtlist);
    TableRow tr1 = new TableRow(this);
    TextView textview = new TextView(this);

    for(int i=0; i<stopper; i++)
    {

        tr1.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        tr1.setId(i);

        textview.setText("" + i);
        tr1.addView(textview);
        tl.addView(tr1, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        tr1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT));
        tr1.setId(i);

    }

    trigger++;
}
}

我有一个(基本上)空白的 .XML 可以使用:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/debtlist"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

</TableLayout>

我的日志猫:

10-13 04:12:07.412: E/AndroidRuntime(799): FATAL EXCEPTION: main
10-13 04:12:07.412: E/AndroidRuntime(799): java.lang.ClassCastException: android.app.ActionBar$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams
10-13 04:12:07.412: E/AndroidRuntime(799):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:587)
10-13 04:12:07.412: E/AndroidRuntime(799):  at android.widget.TableLayout.measureVertical(TableLayout.java:468)
10-13 04:12:07.412: E/AndroidRuntime(799):  at android.widget.TableLayout.onMeasure(TableLayout.java:431)
10-13 04:12:07.412: E/AndroidRuntime(799):  at android.view.View.measure(View.java:10828)
10-13 04:12:07.412: E/AndroidRuntime(799):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
10-13 04:12:07.412: E/AndroidRuntime(799):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:267)
10-13 04:12:07.412: E/AndroidRuntime(799):  at android.view.View.measure(View.java:10828)
10-13 04:12:07.412: E/AndroidRuntime(799):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:764)
10-13 04:12:07.412: E/AndroidRuntime(799):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:519)
10-13 04:12:07.412: E/AndroidRuntime(799):  at android.view.View.measure(View.java:10828)
10-13 04:12:07.412: E/AndroidRuntime(799):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
10-13 04:12:07.412: E/AndroidRuntime(799):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:267)
10-13 04:12:07.412: E/AndroidRuntime(799):  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:1890)
10-13 04:12:07.412: E/AndroidRuntime(799):  at android.view.View.measure(View.java:10828)
10-13 04:12:07.412: E/AndroidRuntime(799):  at android.view.ViewRoot.performTraversals(ViewRoot.java:909)
10-13 04:12:07.412: E/AndroidRuntime(799):  at android.view.ViewRoot.handleMessage(ViewRoot.java:2003)
10-13 04:12:07.412: E/AndroidRuntime(799):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-13 04:12:07.412: E/AndroidRuntime(799):  at android.os.Looper.loop(Looper.java:132)
10-13 04:12:07.412: E/AndroidRuntime(799):  at android.app.ActivityThread.main(ActivityThread.java:4025)
10-13 04:12:07.412: E/AndroidRuntime(799):  at java.lang.reflect.Method.invokeNative(Native Method)
10-13 04:12:07.412: E/AndroidRuntime(799):  at java.lang.reflect.Method.invoke(Method.java:491)
10-13 04:12:07.412: E/AndroidRuntime(799):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
10-13 04:12:07.412: E/AndroidRuntime(799):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
10-13 04:12:07.412: E/AndroidRuntime(799):  at dalvik.system.NativeStart.main(Native Method)

如果有人有任何更有效的方法来根据用户输入的数据量显示一组 4X1-10 的 TextView,请告诉我。

我也在使用 API 级别 12

4

2 回答 2

1

如日志:

ClassCastException: android.app.ActionBar$LayoutParams 不能转换为 android.widget.LinearLayout$LayoutParams

表示您正在导入ActionBar.LayoutParams以将 LayoutParams 设置为 TableLayout 和 Buttons,因此请使用 LinearLayout.LayoutParams 作为:

for(int i=0; i<stopper; i++)
    {

     tr1.setLayoutParams(new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT,
                                                     LayoutParams.WRAP_CONTENT));
        //.....
     tr1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                                                    LayoutParams.WRAP_CONTENT)));
     tr1.setId(i);

    }
于 2013-10-13T04:19:57.007 回答
0

您正在获得类转换异常,因为您正在使用表格布局并膨胀 ActionBar 线性布局的上下文。

java.lang.ClassCastException: android.app.ActionBar$LayoutParams 不能转换为 android.widget.LinearLayout$LayoutParams

对于 x 行数,每行有 4 列吗?

为此使用自定义布局并使用它。

 for(int i=0; i<stopper; i++)
    {

       // Inflate your row "template" and fill out the fields.
    TableRow row = (TableRow)LayoutInflater.from(context).inflate(R.layout.attrib_row,  null);
    ((TextView)row.findViewById(R.id.attrib_name)).setText(b.NAME);
    ((TextView)row.findViewById(R.id.attrib_value)).setText(b.VALUE);
    table.addView(row);

    }
于 2013-10-13T04:32:37.537 回答