当前位置: 首页 > 知识库问答 >
问题:

填充listView时,数组自适应出现空指针异常

东方修谨
2023-03-14

Bank.java文件代码

package com.example.bloodbank;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;


public class Bank extends AppCompatActivity {
    private Spinner spBloodGroup,spCity;
    private String bgArray[]=new String[] {"A+","A-","B+","B-","O+","O-","AB+","AB-"};
    private DBConnection db;
    private ListView lvDonors;
    private ArrayList<String> city,name;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.acitvity_bank);

        //Initialisation spinner
        spBloodGroup=findViewById(R.id.spBloodGroup);
        spCity=findViewById(R.id.spCity);

        //Initialisation ListView
        lvDonors=findViewById(R.id.lvDonors);

        //ArrayAdapter to spinner blood group
        ArrayAdapter<String> adapter =new ArrayAdapter<>(this,R.layout.bank_spinner,bgArray);
        spBloodGroup.setAdapter(adapter);

        //Initialisation of Arraylist
        city=new ArrayList<String>();
        name=new ArrayList<>();

        //Initialisation of database onject
        db=new DBConnection(this);

        //Populating City spinner
        populateList();
        ArrayAdapter<String> adapter1=new ArrayAdapter<>(this,R.layout.spinner_city,city);
        spCity.setAdapter(adapter1);



        spBloodGroup.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                filterDonor();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        spCity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                filterDonor();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });


    }

    private void populateList() {
        SQLiteDatabase sqldb=db.getWritableDatabase();
        Cursor cursor=sqldb.rawQuery("select city from registration",null);
        while(cursor.moveToNext()) {
            city.add(cursor.getString(cursor.getColumnIndex("city")));
        }
    }

    private void filterDonor() {
        name.clear();
        SQLiteDatabase sqldb=db.getWritableDatabase();
        Cursor cursor=sqldb.rawQuery("select name from registration natural html" target="_blank">join donor where donor.bloodgroup='"+spBloodGroup.getSelectedItem().toString()+"' and registration.city='"+spCity.getSelectedItem().toString()+"'",null);
        while(cursor.moveToNext()) {
            name.add(cursor.getString(cursor.getColumnIndex("name")));
        }
        ArrayAdapter<String> adapter=new ArrayAdapter<>(this,R.layout.custom_listview,R.id.lvDonors,name);
        lvDonors.setAdapter(adapter);

    }
}

Custom_listview.xml布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="15dp">
    <TextView
        android:id="@+id/lvtvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="34sp"
        android:textColor="#000000"/>
</RelativeLayout>

activity_bank.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".Bank">

    <TextView
        android:id="@+id/tvDonarHeader"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Donate"
        android:textSize="44sp"
        android:textColor="#FF0000"
        android:layout_centerHorizontal="true"/>
    <TextView
        android:id="@+id/tvBLoodGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Blood Group"
        android:layout_below="@+id/tvDonarHeader"
        android:textSize="20sp"
        android:textColor="#000000"
        android:layout_marginRight="5dp"/>

    <Spinner
        android:id="@+id/spBloodGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/tvBLoodGroup"
        android:layout_below="@+id/tvDonarHeader"/>
    <TextView
        android:id="@+id/tvCity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="City"
        android:layout_below="@+id/tvDonarHeader"
        android:textSize="20sp"
        android:layout_toRightOf="@+id/spBloodGroup"
        android:textColor="#000000"
        android:layout_marginRight="5dp"/>

    <Spinner
        android:id="@+id/spCity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/tvCity"
        android:layout_below="@+id/tvDonarHeader"/>

    <ListView
        android:id="@+id/lvDonors"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_below="@+id/spBloodGroup"
        android:divider="@color/teal_200"
        android:dividerHeight="2dp"
        android:listSelector="@color/teal_200"/>

</RelativeLayout>

获取此错误

E/AndroidRuntime: 致命异常: main Process: com.example.bloodbank, PID: 16440 java.lang.NullPointerException: 尝试在 android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:392) at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362) at android.widget.AbsListView.obtainView(AbsListView.java:2459) at android.widget.ListView 的空对象引用上调用虚拟方法 'void android.widget.TextView.TextView:2459) at android.widget.ListView.measureHeightOfChildren(ListView.java:1281) at android.widget.ListView.onMeasure(ListView.java:1189) at android.view.View.measure(View.java:17765) at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:841) at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:507) at android.view.View.measure(View.java:17765) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5620) at android.widget.FrameLayout.onMeasure(FrameLayout.java:454) at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:145) at android.view.View.measure(View.java:17765) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5620) at androidx.appcompat.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:496) at android.view.View.measure(View.java:17765) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5620) atandroid.widget.FrameLayout.onMeasure(FrameLayout.java:454) at android.view.View.measure(View.java:17765) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5620) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1687) at android.widget.LinearLayout.measureVertical(LinearLayout.java:755) at android.widget.LinearLayout.onMeasure(LinearLayout.java:624) at android.view.View.measure(View.java:17765) atandroid.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5620) at android.widget.FrameLayout.onMeasure(FrameLayout.java:454) at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2616) at android.view.View.measure(View.java:17765) at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2349) at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1373) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1597) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1251) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6438) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:795) at android.view.Choreographer.doCallbacks(Choreographer.java:598) at android.view.Choreographer.doFrame(Choreographer.java:567) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:781) atandroid.os.Handler.handleCallback(Handler.java:810) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:189) at android.app.ActivityThread.main(ActivityThread.java:5532) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)

共有1个答案

百里京
2023-03-14

在我看来,在倒数第二行,您正在尝试调用此构造函数:

ArrayAdapter(Context上下文,int资源,int textViewResourceId,T[]对象)

注意,第三个参数应该是对TextView的引用。但第三个参数是对ListView的引用。

 类似资料:
  • 我在一个片段中制作了一个listview列表视图的适配器有一个空指针异常,但Arraylist和适配器不为空。 错误:lvElevator不能为null 错误是针对以下行的 电梯。适配器=EAdapter

  • 以下是在sun.reflect.nativeMethodAccessorImpl.Invoke0(本机方法)在sun.reflect.nativeMethodAccessorImpl.Invoke(未知源)在sun.reflect.NativeMethodAccessorImpl.Invoke(未知源)在java.lang.Reflect.Method.Invoke(未知源)在com.codena

  • > 单击 受保护得空onPrepareDialog(int id,Dialog Dialog) 受保护的对话框onCreateDialog(int id) 如果我对timepickerdialog代码做了什么错误,请告诉我。

  • 我用两个项目做了一个微调器,我想将文本颜色更改为白色,所以我做了一个xml... 我尝试这样做来设置适配器.... 但它给了我一个错误!它说在 spinner.setAdapter() 行上有一个空指针异常。我看到人们这样做的其他问题,它实际上是有效的,所以我不知道发生了什么。这是微调器的代码.... 最初的问题已修复,但现在它没有显示任何条目。

  • 问题内容: 我在尝试启动包含的活动时花了一段时间。 在适配器类的方法中,当调用a 的函数时会发生异常。 下面的代码是我的适配器类: 如您所见,我已经将字符串记录在logcat中,并且工作正常,但是下一行会出错。这是logcat的输出: 日志显示该错误发生在我的适配器类的这一行: 我非常感谢您的帮助。 问题答案: 您已经知道问题出在哪里了! 导致NPE的原因是。这意味着线 无法找到TextView。

  • 问题内容: 此代码导致空指针异常。我不知道为什么: 我已经在调试器中检查了它,所有局部变量都不为空。怎么会这样呢?BiMap来自Google Collections。 问题答案: 空指针异常是将的结果拆箱的结果。如果不包含键,则返回“ of type” 。假设分配是给引用的,Java将值拆箱到中,导致空指针异常。 您应该检查或用作局部变量类型,以免取消装箱并采取相应措施。适当的机制取决于您的上下文