0

SetActivityMain works fine, but Show_Soundfile_List doesn't work because ListView isn't found and results in lv = null (if I'm correct).

Any idea why this is? Both have pretty much the same code & setup so I'm not sure why one is able to find it's ListView but not the other one.

Error code:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

Code:

public void SetActivityMain() {
  setContentView(R.layout.activity_main);
  ListView lv = (ListView) findViewById(R.id.lv_alarms);
  Button btn = (Button) findViewById(R.id.btn_new_alarm);

  lv.setAdapter(new MyListAdapter_alarm(MainActivity.this, R.layout.list_alarms, alarms));

  btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      NewAlarm(new Alarm());
    }
  });
}


void Show_Soundfile_List(final Alarm alarm) {
  setContentView(R.layout.activity_soundfile_list);
  ListView lv = (ListView) findViewById(R.id.lv_soundfiles);
  Button btn = (Button) findViewById(R.id.btn_list_soundfile);

  lv.setAdapter(new MyListAdapter_SoundFile(MainActivity.this, R.layout.list_soundfiles, soundfiles, alarm));

  btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      EditAlarm(alarm);
    }
  });
}
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
Tore
  • 3
  • 3
  • Where do you call `setContentView()` and how many times? When you call it for the next time the previous view hierarchy will be destroyed and GC'ed. – Raskilas Feb 08 '19 at 15:08

1 Answers1

0

I did a clean and rebuild, and it works as intended. I have never had this issue so I didn't think about it as a solution. Thanks.

Tore
  • 3
  • 3