簡體   English   中英

嘗試在空對象引用 Android 適配器上調用虛擬方法

[英]Attempt to invoke virtual method on a null object reference Android Adapter

我想在我的 Android 應用程序中包含一個 SearchView,但是當 setAdapter 將適配器綁定到視圖時我遇到了一個問題:

來自 MainActivity 的 OnCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    binding = ActivityMainBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());




    // Locate the ListView in listview_main.xml
    list = findViewById(R.id.listview);

    for (int i = 0; i < animalNameList.length; i++) {
        AnimalNames animalNames = new AnimalNames(animalNameList[i]);
        // Binds all strings into an array
        arraylist.add(animalNames);
    }

    // Pass results to ListViewAdapter Class
    adapter = new ListViewAdapter(this, arraylist);

    // Binds the Adapter to the ListView
    list.setAdapter(adapter);

    // Locate the EditText in listview_main.xml
    editsearch = findViewById(R.id.search);
    editsearch.setOnQueryTextListener(this);



    setSupportActionBar(binding.appBarMain.toolbar);

    DrawerLayout drawer = binding.drawerLayout;
    NavigationView navigationView = binding.navView;
    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top level destinations.
    mAppBarConfiguration = new AppBarConfiguration.Builder(R.id.nav_main,R.id.nav_add_alumno,
            R.id.nav_alumnado, R.id.nav_ejercicios, R.id.nav_insignias, R.id.nav_settings)
            .setOpenableLayout(drawer)
            .build();
    NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment_content_main);
    NavController navController = navHostFragment.getNavController();
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);
}

有錯誤的 Logcat:

2022-06-13 19:13:12.720 9197-9197/com.example.llm E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.llm, PID: 9197
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.llm/com.example.llm.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
    at com.example.llm.MainActivity.onCreate(MainActivity.java:63)
    at android.app.Activity.performCreate(Activity.java:7994)
    at android.app.Activity.performCreate(Activity.java:7978)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:223) 
    at android.app.ActivityThread.main(ActivityThread.java:7656) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 

我做了一些研究,但每個頁面都執行相同的過程:從 .xml 獲取視圖,將數據獲取到 ArrayList 或一些類似對象,創建適配器並執行 .setAdapter。

我真的不知道這是否足夠的信息,如果不是,我可以添加所需的一切。

活動主.xml:

    <?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        android:id="@+id/app_bar_main"
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />
    </androidx.drawerlayout.widget.DrawerLayout>

順便說一下,使用 SearchView 的視圖如下:

    <?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">


    <ImageView
        android:id="@+id/imLogo"
        android:layout_width="250dp"
        android:layout_height="100dp"
        android:layout_marginTop="36dp"
        android:src="@drawable/logo"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/txtNombre"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Grupo: "
        android:textSize="16sp"
        app:layout_constraintBottom_toBottomOf="@+id/spinner"
        app:layout_constraintEnd_toStartOf="@+id/spinner"
        app:layout_constraintHorizontal_bias="0.917"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/spinner"
        app:layout_constraintVertical_bias="0.5" />

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="210dp"
        android:layout_height="48dp"
        android:contentDescription="@string/spinnerRef"
        app:layout_constraintBottom_toTopOf="@+id/txtDescription"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/txtNombre"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imLogo" />

    <TextView
        android:id="@+id/txtDescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:text="Búsqueda de alumnos: "
        android:textSize="16sp"
        app:layout_constraintBottom_toTopOf="@+id/relativeLayout3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.172"
        app:layout_constraintStart_toStartOf="parent" />

    <RelativeLayout
        android:id="@+id/relativeLayout3"
        android:layout_width="613dp"
        android:layout_height="521dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imLogo"
        app:layout_constraintVertical_bias="0.38">

        <SearchView
            android:id="@+id/search"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:iconifiedByDefault="false">

            <requestFocus />
        </SearchView>

        <ListView
            android:id="@+id/listview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_below="@+id/search" />

    </RelativeLayout>

    <Button
        android:id="@+id/btnCrearCurso"
        android:layout_width="350dp"
        android:layout_height="60dp"
        android:backgroundTint="@color/blueFlag"
        android:text="Añadir Estudiantes"
        app:cornerRadius="20dp"
        app:iconPadding="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/relativeLayout3" />

</androidx.constraintlayout.widget.ConstraintLayout>

這在 android 中是不允許的。 您只能找到可以在 UI 中看到的視圖。 嘗試使用 SearchView 的綁定作為您的內容。 例子:

binding = SearchViewBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM