簡體   English   中英

Java Android - 自定義適配器getCount方法nullPointer錯誤

[英]Java Android - Custom Adapter getCount Method nullPointer Error

我為 listview 編寫了一個自定義適配器,它從基本適配器擴展而來。 基本上它會列出一些名字,姓氏等。

getSize 方法 在適配器 class 中,出現 nullpointException。 我無法弄清楚發生了什么。

2021-11-27 13:55:36.561 27997-27997/com.example.listviewlesson E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.listviewlesson, PID: 27997 java.lang.RuntimeException: Unable to start activity ComponentInfo{ com.example.listviewlesson/com.example.listviewlesson.MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference at android.app.ActivityThread. performLaunchActivity(ActivityThread.java:2913) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78 ) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1808) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java. lang.reflect.Method.invoke(Native Method) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.Z93F725A07423FE1C889F 448B33D21F46Z:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference at com.example.listviewlesson.UserListAdapter.getCount( UserListAdapter.java:27 ) at android.widget.ListView.setAdapter(ListView.java:575) at com.example.listviewlesson.MainActivity.fillList( MainActivity.java :26 ) 在 com.example.listviewlesson.MainActivity.onCreate( MainActivity.java:21 ) 在 android.app.Activ ity.performCreate(Activity.java:7136)...

這是我的適配器 class

package com.example.listviewlesson;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class UserListAdapter extends BaseAdapter {

Context context;
List<UserModel> uList;

public UserListAdapter(List<UserModel> list, Context context) {
    this.uList = list;
    this.context = context;

}

@Override
public int getCount() {

    return uList.size();
}

@Override
public Object getItem(int position) {
    return uList.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View layout = LayoutInflater.from(context).inflate(R.layout.layout, parent, false);

    TextView name = (TextView) layout.findViewById(R.id.namexml);
    TextView surname = (TextView) layout.findViewById(R.id.surnamexml);
    TextView age = (TextView) layout.findViewById(R.id.agexml);
    TextView team = (TextView) layout.findViewById(R.id.teamxml);

    name.setText(uList.get(position).getName());
    surname.setText(uList.get(position).getSurname());
    age.setText(uList.get(position).getAge());
    team.setText(uList.get(position).getTeam());

    return layout;
}
}

這是主要活動

package com.example.listviewlesson;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
List<UserModel> userList;
UserListAdapter adp;
ListView lview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Tanimla();
    fillList();

}
public void fillList() {
    userList = new ArrayList<UserModel>(userList);
    UserModel model0 = new UserModel("USER1", "SRNAME", "29", "BJK");
    UserModel model1 = new UserModel("USER2", "SRNAME", "29", "BJK");
    UserModel model2 = new UserModel("USER3", "SRNAME", "1", "BJK");

    userList.add(model0);
    userList.add(model1);
    userList.add(model2);


    adp = new UserListAdapter(userList, this);
    lview.setAdapter(adp);
}
void Tanimla() {
    lview = (ListView) findViewById(R.id.listview0);
}


}

這是主要布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity">


<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/listview0"
    />

一切都好,只需在 fillList() 方法中更改這行代碼即可。

 public void fillList() {
    userList = new ArrayList<>();
    UserModel model0 = new UserModel("USER1", "SRNAME", "29", "BJK");
    UserModel model1 = new UserModel("USER2", "SRNAME", "29", "BJK");
    UserModel model2 = new UserModel("USER3", "SRNAME", "1", "BJK");

    userList.add(model0);
    userList.add(model1);
    userList.add(model2);


    adp = new UserListAdapter(userList, this);
    lview.setAdapter(adp);
}

暫無
暫無

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

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