我有getChildAt(index)
函数获取null的问题。有些视图添加了程序化,但有些确实有效,但有些确实没有......我通过监视器检查层次结构并确保索引正确,所以我无法理解问题所在。
这是getChildAt(index)
可以让孩子完美的部分工作。
Cursor aCursor = db.rawQuery(sql, new String[]{danceId, (i+1)+""});
int index = 0;
if (aCursor.getCount()>0){
while(aCursor.moveToNext()){
EditText ansText = new EditText(activity);
ansText.setText(aCursor.getString(0));
ansText.setTag(R.id.da_id,aCursor.getString(1));
ansText.setTag(R.id.edit_flag, false);
ansText.addTextChangedListener(new editFlagListener(ansText));
ansView.addView(ansText, index);
index++;
}
} else {
EditText ansText = new EditText(activity);
ansText.setTag(R.id.edit_flag, false);
ansText.addTextChangedListener(new editFlagListener(ansText));
ansView.addView(ansText, index);
}
这是问题部分。我通过点击按钮添加子视图。
public void onClick(View v) {
// TODO Auto-generated method stub
LinearLayout ans = null;
switch(v.getId()){
case R.id.q1_add:
ans = a1;
break;
case R.id.q2_add:
ans = a2;
break;
case R.id.q3_add:
ans = a3;
break;
case R.id.q4_add:
ans = a4;
break;
case R.id.q5_add:
ans = a5;
break;
case R.id.q6_add:
ans = a6;
break;
case R.id.q7_add:
ans = a7;
break;
case R.id.q8_add:
ans = a8;
break;
case R.id.q9_add:
ans = a9;
break;
case R.id.q10_add:
ans = a10;
break;
}
if (ans != null){
EditText ansText = new EditText(activity);
ans.addView(ansText, ans.getChildCount());
}
}
最后是我得到孩子的部分(通过按钮添加的子句返回null),最后一个句子是if子句:
for(int j=0; j<ansView.getChildCount(); j++){
Log.i(TAG, "text no.:"+j);
EditText ansText = (EditText)ansView.getChildAt(j);
if ( (boolean)ansText.getTag(R.id.edit_flag) == true){
...
}
}
我得到的错误(在这个例子中我有5个孩子,按钮添加了第5个孩子):
08-15 10:48:30.021: I/OneToOneNote(3888): dq id:1
08-15 10:48:30.021: I/OneToOneNote(3888): text no.:0
08-15 10:48:30.021: I/OneToOneNote(3888): text no.:1
08-15 10:48:30.021: I/OneToOneNote(3888): text no.:2
08-15 10:48:30.021: I/OneToOneNote(3888): text no.:3
08-15 10:48:30.021: I/OneToOneNote(3888): text no.:4
08-15 10:48:30.021: D/AndroidRuntime(3888): Shutting down VM
08-15 10:48:30.021: W/dalvikvm(3888): threadid=1: thread exiting with uncaught exception (group=0x4154eba8)
08-15 10:48:30.021: E/AndroidRuntime(3888): FATAL EXCEPTION: main
08-15 10:48:30.021: E/AndroidRuntime(3888): Process: com.garland.digital.hk.BNI.Supreme, PID: 3888
08-15 10:48:30.021: E/AndroidRuntime(3888): java.lang.NullPointerException
08-15 10:48:30.021: E/AndroidRuntime(3888): at com.garland.digital.hk.BNI.Supreme.OneToOneNote.onOptionsItemSelected(OneToOneNote.java:142)
08-15 10:48:30.021: E/AndroidRuntime(3888): at android.support.v4.app.Fragment.performOptionsItemSelected(Fragment.java:1608)
08-15 10:48:30.021: E/AndroidRuntime(3888): at android.support.v4.app.FragmentManagerImpl.dispatchOptionsItemSelected(FragmentManager.java:2012)
08-15 10:48:30.021: E/AndroidRuntime(3888): at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:378)
08-15 10:48:30.021: E/AndroidRuntime(3888): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1012)
08-15 10:48:30.021: E/AndroidRuntime(3888): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:741)
08-15 10:48:30.021: E/AndroidRuntime(3888): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
08-15 10:48:30.021: E/AndroidRuntime(3888): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:884)
08-15 10:48:30.021: E/AndroidRuntime(3888): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
08-15 10:48:30.021: E/AndroidRuntime(3888): at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:546)
08-15 10:48:30.021: E/AndroidRuntime(3888): at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:115)
08-15 10:48:30.021: E/AndroidRuntime(3888): at android.view.View.performClick(View.java:4438)
08-15 10:48:30.021: E/AndroidRuntime(3888): at android.view.View$PerformClick.run(View.java:18422)
08-15 10:48:30.021: E/AndroidRuntime(3888): at android.os.Handler.handleCallback(Handler.java:733)
08-15 10:48:30.021: E/AndroidRuntime(3888): at android.os.Handler.dispatchMessage(Handler.java:95)
08-15 10:48:30.021: E/AndroidRuntime(3888): at android.os.Looper.loop(Looper.java:136)
08-15 10:48:30.021: E/AndroidRuntime(3888): at android.app.ActivityThread.main(ActivityThread.java:5001)
08-15 10:48:30.021: E/AndroidRuntime(3888): at java.lang.reflect.Method.invokeNative(Native Method)
08-15 10:48:30.021: E/AndroidRuntime(3888): at java.lang.reflect.Method.invoke(Method.java:515)
08-15 10:48:30.021: E/AndroidRuntime(3888): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
08-15 10:48:30.021: E/AndroidRuntime(3888): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
08-15 10:48:30.021: E/AndroidRuntime(3888): at dalvik.system.NativeStart.main(Native Method)