-
Notifications
You must be signed in to change notification settings - Fork 220
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
Comments
please help me out of this problem . I really need your help |
I was able to work around this issue by doing the following:
<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>
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);
} |
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 The workaround: Don't use this constructor:
Use either of these:
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);
} |
I'm trying to add it to an existing layout programatically
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 thepublic FloatLabeledEditText(Context context)
constructor. Hence,mHintTextView
was never initialized and will cause NPE when it's used inaddView()
method.Please help.
The text was updated successfully, but these errors were encountered: