1

I want to create 2d array of EdiText.
Below is the code for this.

I don't know what the problem is.
But the app stops unfortunately.

I placed this function createTL() inside onClickListener() of a button.
If I Click that button App stops unfortunately

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;
 }

My LogCat:

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}
1
  • Maybe create 2d array from View instead, as this answer showing Commented Feb 22, 2014 at 19:51

1 Answer 1

1

Fill doesn't work on 2D arrays. You need to loop and fill each row.

Sign up to request clarification or add additional context in comments.

1 Comment

for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { et[i][j] = new EditText(this); et[i][j].setText("1"); et[i][j].setWidth(50); tr.addView(et[i][j]); } } like this??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.