【问题标题】:I'm trying to create Dynamic EditText But getting Error我正在尝试创建动态 EditText 但出现错误
【发布时间】:2019-10-30 10:04:15
【问题描述】:

有什么办法可以完成这件事。我正在从另一个活动获取输入以创建动态 editText 以获取输入。

public class GetNames extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.names);
    LinearLayout ll = findViewById(R.id.lin);

    Bundle bundle = getIntent().getExtras();
    String entered_no = bundle.getString("no_of_persons");

    for (int i = 0; i < (Integer.valueOf(entered_no)); i++) {
        EditText editText = new EditText(GetNames.this);
        editText.setId(editText.generateViewId());
        ll.addView(editText,0);
        editText.setHint(i);
    }
}

}

有没有办法解决这个问题。

她是我的 XML 文件

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="wrap_content"
    android:id="@+id/lin"
    android:layout_height="wrap_content"
    android:orientation="vertical"></LinearLayout>

这里是错误信息

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.abhishakkrmalviya.perfecthalf/com.abhishakkrmalviya.perfecthalf.GetNames}: android.content.res.Resources$NotFoundException: String resource ID #0x0

【问题讨论】:

  • 我的例子对你有帮助吗?

标签: java android xml


【解决方案1】:

你的问题是你没有设置布局参数,看看这个例子。

ConstraintLayout layout = (ConstraintLayout)findViewById(R.id.mainConstraint);
ConstraintSet set = new ConstraintSet();

EditText editText = new EditText(GetNames.this);
editText.setId(View.generateViewId());
editText.setHeight(50);
editText.setWidth(50);

layout.addView(view,0);
set.clone(layout);
set.connect(view.getId(), ConstraintSet.TOP, layout.getId(), ConstraintSet.TOP, 60);
set.applyTo(layout);

您可以参考this了解更多信息

【讨论】:

    猜你喜欢
    • 2021-04-30
    • 1970-01-01
    • 2014-07-26
    • 1970-01-01
    • 2022-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多