我希望通过动态更改数据行来创建4列。我会用LinearLayout做到这一点,但我找不到一个很好的方法来获得一组4x10的textview。这个.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>
我的LogCat:
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的TextViews集,请告诉我。
我也在使用API级别12
答案 0 :(得分: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);
}
答案 1 :(得分: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);
}