gpt4 book ai didi

android - 如何初始化 EditText 的二维数组?

转载 作者:行者123 更新时间:2023-11-30 03:04:56 24 4
gpt4 key购买 nike

我想创建 EdiText 的二维数组。
下面是这方面的代码。

我不知道是什么问题。
但不幸的是,该应用程序停止了。

我将此函数 createTL() 放在按钮的 onClickListener() 中。
如果我点击那个按钮应用程序不幸停止

public TableLayout createTL(int r)
{
int c=4;
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
myTL=new TableLayout(Ybus_Activity.this);
myTL.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
myTL.setStretchAllColumns(true);
TableRow[] tr = new TableRow[r];
EditText[][] et = new EditText[r][c];
Arrays.fill(et, new EditText(this));
Arrays.fill(tr, new TableRow(this));
for(int i=0;i<r;i++)
{
for(int j=0;j<4;j++)
{
et[i][j].setLayoutParams(params);
et[i][j].setWidth(100);
et[i][j].setImeOptions(EditorInfo.IME_ACTION_NEXT);
et[i][j].setInputType(InputType.TYPE_CLASS_NUMBER);
et[i][j].setKeyListener(DigitsKeyListener.getInstance());
et[i][j].setMaxLines(1);
tr[i].addView(et[i][j]);
myTL.addView(tr[i]);
}
}
//I already have LinearLayout named "main" under onCreate() method...
//main.addView(myTL);
return myTL;
}

我的日志:

02-23 00:40:19.200: D/AndroidRuntime(4488): Shutting down VM
02-23 00:40:19.210: W/dalvikvm(4488): threadid=1: thread exiting with uncaught exception (group=0xb2e97288)
02-23 00:40:19.210: E/AndroidRuntime(4488): FATAL EXCEPTION: main
02-23 00:40:19.210: E/AndroidRuntime(4488): java.lang.ArrayStoreException: android.widget.EditText cannot be stored in an array of type android.widget.EditText[][]
02-23 00:40:19.210: E/AndroidRuntime(4488): at java.util.Arrays.fill(Arrays.java:966)
02-23 00:40:19.210: E/AndroidRuntime(4488): at com.gkn.powersystemcalc.Ybus_Activity.createTL(Ybus_Activity.java:156)
02-23 00:40:19.210: E/AndroidRuntime(4488): at com.gkn.powersystemcalc.Ybus_Activity$2.onClick(Ybus_Activity.java:140)
02-23 00:40:19.210: E/AndroidRuntime(4488): at android.view.View.performClick(View.java:4084)
02-23 00:40:19.210: E/AndroidRuntime(4488): at android.view.View$PerformClick.run(View.java:16966)
02-23 00:40:19.210: E/AndroidRuntime(4488): at android.os.Handler.handleCallback(Handler.java:615)
02-23 00:40:19.210: E/AndroidRuntime(4488): at android.os.Handler.dispatchMessage(Handler.java:92)
02-23 00:40:19.210: E/AndroidRuntime(4488): at android.os.Looper.loop(Looper.java:137)
02-23 00:40:19.210: E/AndroidRuntime(4488): at android.app.ActivityThread.main(ActivityThread.java:4745)
02-23 00:40:19.210: E/AndroidRuntime(4488): at java.lang.reflect.Method.invokeNative(Native Method)
02-23 00:40:19.210: E/AndroidRuntime(4488): at java.lang.reflect.Method.invoke(Method.java:511)
02-23 00:40:19.210: E/AndroidRuntime(4488): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-23 00:40:19.210: E/AndroidRuntime(4488): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-23 00:40:19.210: E/AndroidRuntime(4488): at dalvik.system.NativeStart.main(Native Method)
02-23 00:40:19.250: W/ActivityManager(1012): Force finishing activity com.gkn.powersystemcalc/.Ybus_Activity
02-23 00:40:19.832: W/ActivityManager(1012): Activity pause timeout for ActivityRecord{b3d47c98 com.gkn.powersystemcalc/.Ybus_Activity}

最佳答案

填充不适用于二维数组。您需要循环并填充每一行。

关于android - 如何初始化 EditText 的二维数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21959506/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com