未以前使用的模拟器上的 NullPointerException [ListView 适配器]

NullPointerException on not earlier used Emulator [ListView Adapter]

提问人:Daniëlle 提问时间:6/30/2020 最后编辑:Daniëlle 更新时间:6/30/2020 访问量:21

问:

所以我一直在尝试制作一个应用程序。它在 NEXUS 5X 模拟器上运行良好,但是当我尝试在 Pixel C(和我自己的现实生活中的手机)上运行它时,它崩溃了。

我认为这与 NEXUS 5X 模拟器已经有 3 名玩家有关。其他设备则不然。(我尝试对一些玩家进行硬编码,但崩溃了)

这是我一直遇到的崩溃:

2020-06-29 23:45:32.926 13211-13211/com.example.drinkdrankdrunk E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.drinkdrankdrunk, PID: 13211
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.drinkdrankdrunk/com.example.drinkdrankdrunk.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:2817)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
 Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
    at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:380)
    at android.widget.ListView.setAdapter(ListView.java:575)
    at com.example.drinkdrankdrunk.MainActivity.onCreate(MainActivity.java:61)
    at android.app.Activity.performCreate(Activity.java:6975)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
    at android.os.Handler.dispatchMessage(Handler.java:105) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6541) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

它在此规则上崩溃:

playersListView.setAdapter(playersAdapter);

这是我的一些代码:

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

    admin = new Admin();

    admin.addPlayer("Test");



    loadData();

    // Zorg ervoor dat het toetsenbord niet automatisch wordt geopend
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

    // Players List View
    playersListView = findViewById(R.id.playersListView);
    PlayersAdapter playersAdapter = new PlayersAdapter(this, admin.getPlayers());
    playersListView.setAdapter(playersAdapter);

    // Refresh List View
    refreshPlayersAdapter = playersAdapter;

    // Add player
    addPlayerButton = findViewById(R.id.addPlayerButton);
    addPlayerEditText = findViewById(R.id.addPlayerEditText);

    // Play Button
    playButton = findViewById(R.id.playButton);
}

admin.getPlayers() 给出了这个:

public class PlayersAdapter extends ArrayAdapter<Player> {
private LayoutInflater Inflater;
Admin admin = new Admin();


public PlayersAdapter(@NonNull Context context, @NonNull List<Player> objects) {
    super(context, R.layout.list_players, objects);

    Inflater = LayoutInflater.from(context);
}

@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
   if (convertView == null) {
       convertView = Inflater.inflate(R.layout.list_players, parent, false);
   }

    TextView playerNameTextView = convertView.findViewById(R.id.playerNameTextView);
    ImageView deleteButtonImageView = convertView.findViewById(R.id.deleteButtonImageView);

    Player player = getItem(position);

    playerNameTextView.setText(player.getNamePlayer());

    // Delete button
    deleteButtonImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            admin.getPlayers().remove(position);
            // Refresh scherm
            notifyDataSetChanged();
        }
    });

    return convertView;
}

}

我希望有人能帮助我。

X

Android ListView NullPointerException 大小 适配器

评论

0赞 EraftYps 6/30/2020
是否可以添加完整的错误堆栈跟踪和适配器类的代码?
0赞 Daniëlle 6/30/2020
添加!@EraftYps

答: 暂无答案