1

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)
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
Rafael
  • 21
  • 4
  • 1
    to which layout does the editext belong to. – Raghunandan Mar 18 '14 at 16:22
  • This might help: http://stackoverflow.com/questions/9228777/findviewbyid-not-working-in-a-not-mainactivity-class – AndyG Mar 18 '14 at 16:38
  • The EditText belongs to the MainActivity. I thought I could link the layout EditText to any instantiated EditText, even if it is not located in the MainActivity. – Rafael Mar 19 '14 at 10:27

3 Answers3

1

View.findViewById checks only that the id of this view is equal to the id provided, and return this if the ids matche, because, of course a View can not have children. That's why

 EditText item = (EditText) this.findViewById(R.id.editText1); 

item is null. And it makes perfect sense. How can a Button have an EditText? Probably your EditText belongs to the layout you set in your Activity

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
1

That's it, you have to find the view on the MainActivity and then use it in your ButtonAdd. Here's an example of how you can achieve this:

public ButtonAdd(Context context, AttributeSet attrs, int defStyle) { 
 super(context, attrs, defStyle); 
 this.onCreate();
 setOnClickListener(new OnClickListener() {
 @Override
   public void onClick(View v) {
     String text =MainActivity.item.getText().toString();
     Toast.makeText(this.getContext(), "Text: " + text, Toast.LENGTH_LONG).show();

   }
 });
}

Hope it helps!

amalulla
  • 481
  • 2
  • 7
1

I finally FOUND A SOLUTION in a way I can continue developing the way I wanted before. I just had to get access to the parent view, as you can see on the onCreate() method:

public class ButtonAdd extends Button implements OnClickListener{
    private View parentView;

    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.parentView.findViewById(R.id.editText1);
        try {
            Toast.makeText(this.getContext(), "Text: " + item.getText().toString(), Toast.LENGTH_LONG).show();
        } catch (NullPointerException e) {
            System.err.println("Did not work");
        }
    }

    private void onCreate() {
        this.parentView = (View) this.getParent();
        this.setOnClickListener(this);
    }
}
Rafael
  • 21
  • 4