I'am trying to create a Linear Layout and an editText base on a layout.xml file here is the java code :
private EditText editText(int _intID) {
EditText editText = (EditText)findViewById(R.id.player_name);
editText.setId(_intID);
editText.setHint("Element "+_intID);
editTextList.add(editText);
return editText;
}
private LinearLayout linearlayout(int _intID)
{
LinearLayout LLMain= (LinearLayout)findViewById(R.id.linear_player_base);
LLMain.setId(_intID);
LLMain.addView(editText(_intID));
LLMain.setOrientation(LinearLayout.VERTICAL);
linearlayoutList.add(LLMain);
return LLMain;
}
Here is the xml layout I want to use as a base :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginBottom="5dp"
android:layout_weight="1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:id="@+id/linear_player_base">
<EditText
android:id="@+id/player_name"
android:background="@drawable/input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:imeOptions="actionSend"
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center_horizontal"
/>
</LinearLayout>
I don't understand why when I do :
LinearLayout LLMain= (LinearLayout)findViewById(R.id.linear_player_base);
and then :
LLMain.setId(_intID);
I have this error : Attempt to invoke virtual method 'void android.widget.LinearLayout.setId(int)' on a null object reference
why LLMain is null ?
You must inflate a custom view with you layout and the find the linear layout in this view... Do it like this:
LayoutInflater inflater = getLayoutInflater();
View v = inflater.inflate(R.layout.playerLayout, null);
LinearLayout LLMain= (LinearLayout) v.findViewById(R.id.linear_player_base);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments