Search code examples
androidandroid-activitynullpointerexceptionsuperclass

Creating Object from other class


I have a settings screen where you can choose between, add and remove configurations for the app.

When adding a configuration, I create a new Instance of a inputBox Class (extending the settings activity class - where I stored the procedure for the standard android text input box) to query the name for the new configuration.

In the Onclick of this inputbox a procedure from the superClass (the settings-activity) is called to create a new configuration object. This Procedure queries some things from the activity (e.g. selected spinner element) including the progress of a seekBar.

This is where I get a NPE:

java.lang.NullPointerException: Attempt to invoke virtual method'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference

The same object creation procedure is also called on initialization of the app and works just fine.

I understand from the Error that the issue is that when calling the procedure from a child class the reference of the variables to the corrseponding elements of the screen is not set anymore - and therefore cannot be queried.

So the question:
How can I query values of activity elements, when the procedure is called from another class?

I know that the topic is quite broad, but I can't figure it out for a couple of days now
Thanks for your help in advance.

Here is a scheme of the problem:

public class Settings extends AppCompatActivity{
Context settingsContext = this;
private Spinner someSpinner;
//other elements
@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);
    someSpinner = (Spinner) findViewById(R.id.someView);
    //other elements
    addNewConfig.setOnClickListener((v) --> {
        inputBox inputBox = new inputBox("OK", "Cancel", settingsContext, "sourcePath",1,1);
        newConfigName = inputBox.show();
    });

public sSetting makeNewConfig(String name, String sourcePath, int dataFrom, int dataTo){
    sSetting newConfig;
    newConfig = new sSetting("NAME", someSpinner.getProgress()>0, ...);
    return newConfig;
}
}

And the inputBox:

 public final class inputBox extends Settings {
 //someVars
 inputBox(String buttonOk, String buttonCancel, Context setContext, String sourcePath, int dataFrom, int dataTo){
     //variable setters
 }
 private String show() {
      //show msgbox
      //onclick ok
      super.makeNewConfig(....);
 }

Solution

  • For solving the problem I restructured my Project a little:
    I removed the inputBox-Part, which, after some research considered for a too complicated solution for what I needed anyway.
    However: I now added a editText to my Settings View. Although I had to change my Settings view for this, it now looks better and it was ridiculously easy to edit the configuration name for the user.

    I think in most cases that will do the trick. Adding Popup-Boxes just needs more error handling and makes the design more complicated

    I hope this helps ;) If you need the code for it it is available here: GitHub - AIM