尝试在空对象引用上调用listview.setadapter

jv2fixgn  于 2021-07-07  发布在  Java
关注(0)|答案(0)|浏览(215)

我试图在listview上显示am integer arraylist。但由于空对象错误,我失败了。我的对象不是空的。基本上我是在试着为两队做一个记分牌。这是我的课:

public class PointsList extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.points_list);
        Bundle b;
        b = getIntent().getExtras();
        ArrayList<Integer> scoreA = new ArrayList<Integer>();
        ArrayList<Integer> scoreB = new ArrayList<Integer>();
        ListView ListA, ListB;
        scoreA = b.getIntegerArrayList("scoreA");
        scoreB = b.getIntegerArrayList("scoreB");

    ArrayAdapter arrayAdapterA = new ArrayAdapter<Integer>(
            this, R.layout.points_list, R.id.scoreAtext, scoreA );

    ArrayAdapter arrayAdapterB = new ArrayAdapter<Integer>(
            this, R.layout.points_list, R.id.scoreBtext, scoreB );

     ListA = (ListView) findViewById(R.id.scoreAList);
     ListB =  (ListView) findViewById(R.id.scoreBList);

     ListA.setAdapter(arrayAdapterA);
     ListB.setAdapter(arrayAdapterB);
    }
}

这是我的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:id="@+id/scrollView2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background"
    android:fillViewport="true">

    <ListView
        android:id="@+id/scoreBList"
        android:layout_width="189dp"
        android:layout_height="571dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.927"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.593" >

        <TextView
            android:id="@+id/scoreBtext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
    </ListView>

    <ImageView
        android:id="@+id/imageView2"
        style="@style/custom_image"
        android:layout_width="376dp"
        android:layout_height="619dp"
        android:background="@drawable/dragon"
        android:contentDescription="@string/logoDescription"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/teamNameA"
        android:layout_width="150dp"
        android:layout_height="62dp"
        android:autoSizeTextType="none"
        android:background="@drawable/red_button"
        android:gravity="center_horizontal|center_vertical"
        android:inputType="textShortMessage"
        android:maxLines="1"
        android:singleLine="true"
        android:text="@string/team_a"
        android:textColor="#000000"
        android:textColorHint="#000000"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.103"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.049" />

    <TextView
        android:id="@+id/teamNameB"
        android:layout_width="150dp"
        android:layout_height="62dp"
        android:autoSizeTextType="none"
        android:background="@drawable/red_button"
        android:gravity="center_horizontal|center_vertical"
        android:inputType="textShortMessage"
        android:maxLines="1"
        android:singleLine="true"
        android:text="@string/team_b"
        android:textColor="#000000"
        android:textColorHint="#000000"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.934"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.049" />

    <View
        android:id="@+id/divider"
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:background="?android:attr/listDivider"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ListView
        android:id="@+id/scoreAList"
        android:layout_width="189dp"
        android:layout_height="571dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.072"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.593" >

        <TextView
            android:id="@+id/scoreAtext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
    </ListView>

</androidx.constraintlayout.widget.ConstraintLayout>

我试过只输入一个带有文本视图的空白xml,正如我在其他线程上看到的那样,这正是arrayadapter所期望的。但失败了。非常感谢您的帮助!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题