-1

whenever i try to edit the content of a list view or and text view through the java code my app just crashes and stops working

when i comment the line of code which edit the content the just works fine

in the following example when comment the line

lv1.setAdapter(AA1);

the app works

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;   

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
ListView lv1;
ArrayAdapter<String> AA1;
ArrayList<String> names;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        lv1= findViewById(R.id.lv1);
        names = new ArrayList<String>();
        names.add("Jake");
        names.add("Amy");
        names.add("Diaz");
    names.add("Boyl");
    AA1 = new ArrayAdapter<String >(this,android.R.layout.simple_list_item_1,names);
    lv1.setAdapter(AA1);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }
}
Muhamed El-Banna
  • 593
  • 7
  • 21

1 Answers1

1

you have to call those first

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

so your code will be

 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
    lv1= findViewById(R.id.lv1);
    names = new ArrayList<String>();
    names.add("Jake");
    names.add("Amy");
    names.add("Diaz");
names.add("Boyl");
AA1 = new ArrayAdapter<String >(this,android.R.layout.simple_list_item_1,names);
lv1.setAdapter(AA1);
}
Muhamed El-Banna
  • 593
  • 7
  • 21