当我输入值时,我动态创建了edittext,我无法显示值。如果我想添加我在编辑文本中输入的值,我怎么可能需要给出for循环。
这是代码
public class NewActivity extends Activity {
LinearLayout ll;
EditText lEditText;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ll=new LinearLayout(this);
ll=(LinearLayout)findViewById(R.id.my);
Button lButton = (Button)findViewById(R.id.mybtnid);
lButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
lEditText = new EditText(getApplicationContext());
lEditText.setHint("Text Here");
lEditText.setInputType(InputType.TYPE_CLASS_NUMBER);
ll.addView(lEditText);
}
});
Button button1=(Button)findViewById(R.id.btn2);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
TextView tv = new TextView(getApplication());
int x=Integer.parseInt(lEditText.getText().toString());
tv.setText(x);
ll.addView(tv);
}
});
我在 logcat 中收到错误:
AndroidRuntime(509): android.content.res.Resources$NotFoundException: String resource ID #0x1
E/AndroidRuntime(509): at android.content.res.Resources.getText(Resources.java:201)
E/AndroidRuntime(509): at android.widget.TextView.setText(TextView.java:2817)
E/AndroidRuntime(509): at com.app.NewActivity$2.onClick(NewActivity.java:44)
E/AndroidRuntime(509): at android.view.View.performClick(View.java:2408)
E/AndroidRuntime(509): at android.view.View$PerformClick.run(View.java:8816)
E/AndroidRuntime(509): at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime(509): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(509): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(509): at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime(509): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(509): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(509): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(509): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime(509): at dalvik.system.NativeStart.main(Native Method)