android中的2维数组(EditText视图的“n”行和4列?)

时间:2014-02-22 14:25:42

标签: android

我想创建一个tablelayout,它显示“n”行数和EditText视图的“4”列,用户在其中输入输入值.Below是我将放在setOnClickListener()下面的函数..我有一些错误在这些和我不知道错误。我是一个创建Android应用程序的初学者。如果这个问题听起来很傻,我真的为此感到抱歉..

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(TL);
  }

编辑:添加了LogCat 我的logcat

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

0 个答案:

没有答案