J
J
justxz2020-01-03 15:01:02
Java
justxz, 2020-01-03 15:01:02

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);
    }
}

I can't figure out what the problem is, please help. (In android and java just started to understand).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Botalov_Nikita, 2020-01-03
@justxz

Your list is not initialized. Before list.add("Dima"); write list = new ArrayList()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question