Android custom component works fine at runtime, but doesn't work at design time

RhondaC

New Member
I am doing my first steps in Android development. In the meantime I am trying to create a custom component (for testing purposes) that will later be used on an Activity.Here's the code for ClearableEditText.java:\[code\]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() { // TODO Auto-generated constructor stub this.textField = (EditText) findViewById(R.id.editText1); this.button = (Button) findViewById(R.id.button1); this.textField.setText("Hello World 123"); } }\[/code\]And here's its layout XML file:\[code\]<?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>\[/code\]This is the XML layout code for the activity that will integrate the component:\[code\]<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>\[/code\]When I run the app, the component works fine. Nonetheless, as soon as I switch to the Graphical Layout of my Activity, I get the following error message:\[code\]java.lang.NullPointerExceptionException details are logged in Window > Show View > Error Logjava.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)\[/code\]I have no clue about what's going on in here. Can anyone help me out?
 
Back
Top