0

these calls in onPostResume of the abstract activity are definitely happening after setContentView(R.layout.activity_method_two); in the child classes. But I get java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RadioButton.setChecked(boolean)' on a null object reference anyway

Why can't I do this?

public class MethodTwo extends AbstractMethod {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_method_two);
    }

    @Override
    void set_default_method() {
        rb_default = (RadioButton) findViewById(R.id.radio_button_method_two);
    }

}

and

public abstract class AbstractMethod extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    RadioGroup rg_method;
    RadioButton rb_default;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        set_default_method();
    }

    @Override
    protected void onPostResume() {
        super.onPostResume();

        rb_default.setChecked(true);

        rg_method = (RadioGroup) findViewById(R.id.radio_group_method_select);
        rg_method.setOnCheckedChangeListener(this);
    }

produces an error in the stack trace:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RadioButton.setChecked(boolean)' on a null object reference

if I move the setContentView into the onCreate in the abstract class (just after super.oncreate()), it works .. but then I dont have an abstract class for multiple child classes.

roberto tomás
  • 4,435
  • 5
  • 42
  • 71
  • If you put `setContentView(R.layout.activity_method_two);` on the abstract class it happens?? – newhouse Mar 25 '16 at 23:35
  • I was just coming back, remembering I forgot to add that to the description :) – roberto tomás Mar 25 '16 at 23:36
  • 1
    [This answer](http://stackoverflow.com/a/29953146/1435985) is one possible solution. You move `setContentView` into the abstract class and have the implementing classes override a method to supply the layout id. – George Mulligan Mar 25 '16 at 23:42

1 Answers1

1

Create an abstract method like this:

abstract int getContentId();

And call it in the onCreate() from the abstract class like this

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate();
  setContentView(getContentId());
}

It should be work

newhouse
  • 946
  • 8
  • 12
  • thanks, that did it. in fact all the stuff I had migrated to onPostResume could not go back to onCreate. – roberto tomás Mar 25 '16 at 23:52
  • so I've thought about this some more and frankly, I'm still confused. why is it that the _call_ to `setContentView` has to happen in the abstract class? If I follow this advise and _move_ it there, it is not in the child class, so why does `rb_default` continue to work there? or, if it is going to work in the child classes, why did I have to move it to the abstract class at all? – roberto tomás Mar 26 '16 at 02:34