I'm new to ADT, so, I'm facing some issues trying extend 'android.widget.Button'. In fact, I want to access an android.widget.EditText from the button, but every time I do that I come across an NullPointerException:
public class ButtonAdd extends Button implements OnClickListener{
public ButtonAdd(Context context) {
super(context);
this.onCreate();
}
public ButtonAdd(Context context, AttributeSet attrs) {
super(context, attrs);
this.onCreate();
}
public ButtonAdd(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.onCreate();
}
@Override
public void onClick(View v) {
EditText item = (EditText) this.findViewById(R.id.editText1);
Toast.makeText(this.getContext(), "Text: " + item.getText().toString(), Toast.LENGTH_LONG).show();
}
private void onCreate() {
this.setOnClickListener(this);
}
}
This raises the following exception:
FATAL EXCEPTION: main
java.lang.NullPointerException
at outlook.ButtonAdd.onClick(ButtonAdd.java:35)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)