android.widget.ListView.setAdapter (android.widget.ListAdapter) 'для пустой ссылки на объект - PullRequest
0 голосов
/ 10 октября 2019

ошибка в строке setAdapter. Не могу найти где ошибка. Это не про нулевое исключение. что-то не в порядке

java.lang.RuntimeException: невозможно запустить действие ComponentInfo {com.example.myapplication / com.example.myapplication.Namelist}: java.lang.NullPointerException: попытка выполнитьвызвать виртуальный метод 'void android.widget.ListView.setAdapter (android.widget.ListAdapter)' для ссылки на пустой объект в android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2913) в android.app.ActivityThread.handleLaunchActivity (ActivityThread).java: 3048) at android.app.servertransaction.LaunchActivityItem.execute (LaunchActivityItem.java:78) в

Основной класс:

public class Namelist extends AppCompatActivity {
String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry",
        "WebOS","Ubuntu","Windows7","Max OS X"};
ListView l1;
ArrayAdapter<String> arrayAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    l1 = (ListView) findViewById(R.id.list);

    arrayAdapter = new ArrayAdapter<String>(Namelist.this,R.layout.activity_namelist,mobileArray);

   l1.setAdapter(arrayAdapter);

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

}

}

активность xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Namelist">

<ListView
    android:id="@+id/list"
    android:layout_width="409dp"
    android:layout_height="729dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

заранее спасибо

1 Ответ

2 голосов
/ 10 октября 2019

Вы не можете ссылаться на l1 до setContentView(R.layout.activity_namelist)

Правильный код здесь:

public class Namelist extends AppCompatActivity {

    String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry","WebOS","Ubuntu","Windows7","Max OS X"};
    ListView l1;
    ArrayAdapter<String> arrayAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

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

        l1 = findViewById(R.id.list);

        arrayAdapter = new ArrayAdapter<String>(Namelist.this,android.R.layout.simple_list_item_1,mobileArray);
       l1.setAdapter(arrayAdapter);
    }
}

И вы также передали неправильный layout R.layout.activity_namelist во второй параметр ArrayAdapter. Вы не объявили имя макета activity_namelist. Таким образом, вы можете использовать встроенный макет по умолчанию android.R.layout.simple_list_item_1, спросите код выше.

...