简体   繁体   中英

I'm trying to create Dynamic EditText But getting Error

Is there any way to get this thing done. I'm getting input from another activity to create dynamic editText to get input.

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);
    }
}

}

Is there any way to resolve this.

Her is my XML file

<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>

Here are error messages

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

Your problem is that you are not setting layout parameters, That have a look at this example.

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);

You can refer this for more info

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM