Answer the question
In order to leave comments, you need to log in
I can't figure out why the application with ArrayAdapter is crashing?
I want to display a list of names. The application is going, but on the emulator it immediately crashes.
package com.example.arraylist;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<String> list;
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list.add("Dima");
list.add("Anton");
list.add("Stas");
list.add("Pasha");
adapter = new ArrayAdapter<>(this, R.layout.array_adapter, list);
ListView lv = (ListView)findViewById(R.id.listView);
lv.setAdapter(adapter);
}
}
Answer the question
In order to leave comments, you need to log in
Your list is not initialized. Before list.add("Dima"); write list = new ArrayList()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question