Skip to content

Can't programatically add to existing layout #40

@jezer07

Description

@jezer07

I'm trying to add it to an existing layout programatically

  private void addEditText(){

        EditText itemText = new EditText(getActivity());
        itemText.setHint(R.string.add_item);
        itemText.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));


        FloatLabeledEditText floatLabeledEditText = new FloatLabeledEditText(getActivity());
        floatLabeledEditText.addView(itemText);
        mItemParent.addView(floatLabeledEditText);
    }

But I got NPE when I do that:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'float android.widget.TextView.getTextSize()' on a null object reference at com.wrapp.floatlabelededittext.FloatLabeledEditText.addView(FloatLabeledEditText.java:106)

Because setAttributes() was not called in the public FloatLabeledEditText(Context context) constructor. Hence, mHintTextView was never initialized and will cause NPE when it's used in addView() method.

Please help.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions