0

I am trying to make a button in activity2(settings page), when the button is clicked, then the title of activity1(MainActivity) will change to what I set to. I have been trying to use interface to carry out the function but it gives me a null pointer exception error.

MainActivity

@Override
    public void changeActionBarTitle(String editText){
        actionTitle = editText;
        setTitle(actionTitle);
    };

Interface

public interface ActionBarInterface {
    void changeActionBarTitle(String editText);
}

Setting page (activity 2)

public class SettingsPage extends AppCompatActivity {
    ActionBarInterface actionBarInterface;
    Button editCompanyNameButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings_page);
        setTitle("Settings");

        editCompanyNameButton = findViewById(R.id.editCompanyNameButton);
        editCompanyNameButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                actionBarInterface.changeActionBarTitle("test");
            }
        });
    }
}

Thanks.

Henry
  • 25
  • 6

2 Answers2

0

It gives a null pointer exception error because actionBarInterface was not initialised.

Check out topics on 'Getting a Result from an Activity', making use of classes and functions such as Intent, startActivityForResult, onActivityResult.

Android: how to make an activity return results to the activity which calls it?

lurker0152
  • 118
  • 1
  • 5
  • I tried this line "actionBarInterface = (ActionBarInterface) getActivity;" but it wasn't an option. I also tried to make a static method so I thought I can called it from another class but it say something like non static something i can't remember it was quite long. Or is there a easy way to do this? – Henry Mar 11 '20 at 23:59
  • Even if you succeed getting a reference to the `MainActivity`, how can the `setTitle` be triggered when the present activity is now `SettingsPage`? Use `Intent`, `.putExtra()` classes and functions to pass information between activities. Alternative would be to save to shared preferences and retrieve it later. – lurker0152 Mar 12 '20 at 02:04
0

You can try this code without using the interface

MainActivity:

    @Override
protected void onResume() {
    setTitle(Main2Activity.title);
    super.onResume();
}

activity2:

public class SettingsPage extends AppCompatActivity {
Button editCompanyNameButton;
static String title;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    setTitle("Settings");

    editCompanyNameButton = findViewById(R.id.editCompanyNameButton);
    editCompanyNameButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            title = "test";
        }
    });
}

}

tomysek
  • 159
  • 1
  • 10
  • 1
    This works like a charm. It didn't even occur to me that I could try to get String from activity2. Thanks. – Henry Mar 12 '20 at 06:10