2014-02-22 76 views
0

我不知道什麼是在下面code.But我知道問題是哪裏的問題arises.It是從方法createTL() ..它停止unfortunately.Help我解決這個疑難問題。要創建n行4專欄EditText查看。 編輯:現在我初始化都等和tr..but錯誤仍然arises..Tis是更新的代碼創建表格佈局時遇到困難?

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,"0.0"); 
    Arrays.fill(tr,et); 
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; 

}

我logcat的:(更新)

02-22 23:15:01.315: D/AndroidRuntime(6445): Shutting down VM 
02-22 23:15:01.315: W/dalvikvm(6445): threadid=1: thread exiting with uncaught exception (group=0xb2e26288) 
02-22 23:15:01.345: E/AndroidRuntime(6445): FATAL EXCEPTION: main 
02-22 23:15:01.345: E/AndroidRuntime(6445): java.lang.ArrayStoreException: java.lang.String cannot be stored in an array of type android.widget.EditText[][] 
02-22 23:15:01.345: E/AndroidRuntime(6445):  at java.util.Arrays.fill(Arrays.java:966) 
02-22 23:15:01.345: E/AndroidRuntime(6445):  at com.gkn.powersystemcalc.Ybus_Activity.createTL(Ybus_Activity.java:156) 
02-22 23:15:01.345: E/AndroidRuntime(6445):  at com.gkn.powersystemcalc.Ybus_Activity$2.onClick(Ybus_Activity.java:140) 
02-22 23:15:01.345: E/AndroidRuntime(6445):  at android.view.View.performClick(View.java:4084) 
02-22 23:15:01.345: E/AndroidRuntime(6445):  at android.view.View$PerformClick.run(View.java:16966) 
02-22 23:15:01.345: E/AndroidRuntime(6445):  at android.os.Handler.handleCallback(Handler.java:615) 
02-22 23:15:01.345: E/AndroidRuntime(6445):  at android.os.Handler.dispatchMessage(Handler.java:92) 
02-22 23:15:01.345: E/AndroidRuntime(6445):  at android.os.Looper.loop(Looper.java:137) 
02-22 23:15:01.345: E/AndroidRuntime(6445):  at android.app.ActivityThread.main(ActivityThread.java:4745) 
02-22 23:15:01.345: E/AndroidRuntime(6445):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-22 23:15:01.345: E/AndroidRuntime(6445):  at java.lang.reflect.Method.invoke(Method.java:511) 
02-22 23:15:01.345: E/AndroidRuntime(6445):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
02-22 23:15:01.345: E/AndroidRuntime(6445):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
02-22 23:15:01.345: E/AndroidRuntime(6445):  at dalvik.system.NativeStart.main(Native Method) 
02-22 23:15:01.385: W/ActivityManager(1026): Force finishing activity com.gkn.powersystemcalc/.Ybus_Activity 
02-22 23:15:01.545: D/dalvikvm(1026): GC_FOR_ALLOC freed 899K, 11% free 15374K/17159K, paused 84ms, total 86ms 
02-22 23:15:02.074: W/ActivityManager(1026): Activity pause timeout for ActivityRecord{b3ac8e38 com.gkn.powersystemcalc/.Ybus_Activity} 
+0

安置自己的堆棧跟蹤上帝的sacke! – bluewhile

+0

什麼是堆棧跟蹤,我在哪裏可以找到? – gkn06

+0

發佈你的logcat。可用於DDMS –

回答

0

你是發起的EditText陣列在EditText[][] et = new EditText[r][c];但該陣列填充有null對象。然後你調用那些null對象的方法,這就是爲什麼你得到NullPointerException

你需要呼籲他們任何方法之前,以填補et與初始化EditText對象。

這同樣適用於在TableRow[] tr = new TableRow[r];

編輯:作爲一個說明,你可能希望你的返回類型更改爲TableLayoutmyTL對象器添加到顯示屏幕上的視圖。

+0

可以輕鬆填寫​​的數組,你很可能使用'Arrays.fill()' – Jakar

+0

如何做到這一點的兄弟 – gkn06

+0

http://developer.android.com/reference/java /util/Arrays.html – Jakar