I am taking the first steps in developing Android. In the meantime, I'm trying to create a custom component (for testing purposes), which will later be used in Activity.
Here is the code of ClearableEditText.java:
import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; public class ClearableEditText extends LinearLayout { private EditText textField; private Button button; public ClearableEditText(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onFinishInflate() { super.onFinishInflate(); LayoutInflater.from(this.getContext()).inflate(R.layout.clearable_text, this); setupViewItems(); } private void setupViewItems() {
And here is its XML layout file:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" > <requestFocus /> </EditText> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button" /> </LinearLayout>
This is the XML layout code for the activity that will integrate the component:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".SegundaActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/ativity2_text" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" android:layout_marginTop="33dp" android:ems="10" > <requestFocus /> </EditText> <com.example.aplicacaoteste.ClearableEditText android:id="@+id/newComponent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/editText1" android:layout_centerHorizontal="true" android:layout_marginTop="33dp" android:ems="10" > </com.example.aplicacaoteste.ClearableEditText> </RelativeLayout>
When I launch the application, the component is working fine. However, as soon as I switch to the graphical layout of my activity, I get the following error message:
java.lang.NullPointerException Exception details are logged in Window > Show View > Error Log java.lang.NullPointerException at com.example.aplicacaoteste.ClearableEditText.setupViewItems(ClearableEditText.java:36) at com.example.aplicacaoteste.ClearableEditText.onFinishInflate(ClearableEditText.java:26) at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:754) at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64) at android.view.LayoutInflater.rInflate(LayoutInflater.java:718) at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:749) at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64) at android.view.LayoutInflater.rInflate(LayoutInflater.java:718) at android.view.LayoutInflater.inflate(LayoutInflater.java:489) at android.view.LayoutInflater.inflate(LayoutInflater.java:372)
I do not know what is going on here. Can anyone help me out?
source share