1
在onCreate()方法,我創建一個標籤的文本編輯程序,象這樣:獲取視圖標籤創建程序
EditText et = new EditText(getApplicationContext());
et.setTag(field.getFieldId());
et.setBackgroundResource(R.drawable.textbox);
et.setTextColor(getResources().getColor(R.color.noir));
parentLayout.addView(et);
在afterTextChanged()方法,我希望檢索的EditText標籤來獲取用戶輸入的文字。
我試過這個代碼,但我得到的NullPointerException:
EditText et = (EditText) parentLayout.findViewWithTag("4249");
String strValue = et.getText().toString();
你能幫助我請!
謝謝。
顯示'logcat'和行'NPE'被拋出 – hrskrs 2015-03-25 12:22:11
其實我找到了parentLayout不是EditText的真正父母,它也是一個linearLayout,它也是以編程方式創建的,所以我編輯了我的代碼,如下所示:LinearLayout layout =(LinearLayout)parentll.findViewWithTag(field.getFieldId()); \t \t \t \t EditText et =(EditText)layout.findViewWithTag(field.getFieldId()); \t \t \t \t String strValue = et.getText()。toString();我在這裏得到NullPointer異常:EditText et =(EditText)layout.findViewWithTag(field.getFieldId()); – 2015-03-25 12:29:48