Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't programatically add to existing layout #40

Open
jezer07 opened this issue Apr 2, 2015 · 3 comments
Open

Can't programatically add to existing layout #40

jezer07 opened this issue Apr 2, 2015 · 3 comments

Comments

@jezer07
Copy link

jezer07 commented Apr 2, 2015

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.

@deepakrokz
Copy link

please help me out of this problem . I really need your help

@comann
Copy link

comann commented Jun 2, 2015

I was able to work around this issue by doing the following:

  1. Create an example file float_label_edit_text.xml
<com.wrapp.floatlabelededittext.FloatLabeledEditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:float="http://schemas.android.com/apk/res-auto">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Full Name"
        android:id="@+id/editText"
        />
</com.wrapp.floatlabelededittext.FloatLabeledEditText>
  1. In your classes code
  private void addEditText(){
        View root = LayoutInflater.from(activity).inflate(R.layout.float_label_edit_text, null, false);
        final EditText editText = (EditText) root.findViewById(R.id.editText);
        mItemParent.addView(root);
}

@falcon4ever
Copy link

I was having the same problem and didn't want to use the XML layout file for my project. I looked into the code and found out why it's crashing. It's because the (first) FloatLabeledEditText constructor isn't calling setAttributes(). If this method isn't called on initialization, the value of mHintTextView is null and the app will crash.

The workaround:

Don't use this constructor:

  • public FloatLabeledEditText(Context context)

Use either of these:

  • public FloatLabeledEditText(Context context, AttributeSet attrs)
  • public FloatLabeledEditText(Context context, AttributeSet attrs, int defStyle)

Set the attributes to null if you don't have any values for it:

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(), null);
    floatLabeledEditText.addView(itemText);

    mItemParent.addView(floatLabeledEditText);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants