Android studio 混淆打包安装后报错NullPointerException int java.util.List.size()

本文介绍了一种在Android应用开发过程中遇到的问题:使用ProGuard进行代码混淆后导致的应用崩溃。具体错误为尝试调用null对象的List.size()方法。通过调整混淆规则,排除特定model类的混淆解决了此问题。

前提:

在Android studio中,混淆,打包项目,安装真机,打开软件,报错。

不混淆,打包没有异常。错误肯定是混淆了不该混淆的。

java.lang.NullPointerException: Attempt to invoke interface method 

'int java.util.List.size()' on a null object reference

at com.*.*.adapter.HomePageAdapter.getCount(Unknown Source)

注意这个地方是List.size(),空。

解决:所有model类不要混淆。

-keep class package.*.model.**{*;}

package com.example.myapplication; import android.os.Bundle; import android.view.View; import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import com.example.myapplication.adapters.StudentAdapter; import com.example.myapplication.models.Student; import java.util.Random; // 文件顶部添加 import java.util.ArrayList; import java.util.List; import java.util.Arrays; import java.util.List; public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; private StudentAdapter adapter; private List<Student> studentList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_main); ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> { Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); return insets; // 初始化学生数据 initStudents(); recyclerView = findViewById(R.id.rv_students); recyclerView.setLayoutManager(new LinearLayoutManager(this)); // 正确做法:先创建数组再转换 Student[] stuArray = {new Student("张三"), new Student("李四")}; List<Student> correctList = Arrays.asList(stuArray); // ✅正确初始化[^1] recyclerView.setAdapter(adapter); }); } private void initStudents() { for (int i = 1; i <= 40; i++) { studentList.add(new Student( "学生" + i, "2024" + String.format("%03d", i), new Random().nextBoolean() )); } } // 在MainActivity中添加 public void onAddScoreClick(View view) { int position = recyclerView.getChildLayoutPosition((View) view.getParent()); Student student = studentList.get(position); switch(view.getId()) { case R.id.btn_add_1: student.getTotalScore(student.getTotalScore() + 1); break; case R.id.btn_add_2: student.getTotalScore(student.getTotalScore() + 2); break; } adapter.notifyItemChanged(position); } public void adjustBaseScore(int newBase) { // 遍历方式1:增强for循环 for (Student s : students) { System.out.println(s.getTotalScore()); // ✅对象.方法() } // 遍历方式2:Lambda表达式 students.forEach(s -> System.out.println(s.getTotalScore() + 1) // ✅正确调用位置 ); } } MainActivity package com.example.myapplication.models; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Student { private String name; private String studentId; private double baseScore = 50; private double additionalScore = 0; private boolean isMale; private String gender; private double totalScore; public Student(String name, String studentId, boolean isMale) { this.name = name; this.studentId = studentId; this.isMale = isMale; } // ✅必须存在且可见的访问方法 public double getTotalScore() { return this.totalScore; } public Student(String name) { this.name = name; this.totalScore = Math.random() * 100; // 模拟分数 } public String getStudentId() { return studentId; } public String getName() { return name; } public boolean isMale() { return "男".equals(this.gender); // gender字段需存在 } public Object getAdditionalScore() { return this.baseScore + this.additionalScore; // 根据实际字段调整 } public void setBaseScore(int baseScore) { this.baseScore = baseScore; // 确保baseScore字段存在 } // Getter/Setter省略 public class Main { public static void main(String[] args) { // 1. 正确初始化List Student[] stuArray = { new Student("张三"), new Student("李四") }; List<Student> fixedList = Arrays.asList(stuArray); // 2. 转换为可变List List<Student> mutableList = new ArrayList<>(fixedList); mutableList.add(new Student("王五")); // ✅允许新增 // 3. 正确调用对象方法 System.out.println("-----成绩列表-----"); mutableList.forEach(s -> System.out.printf("%s: %.1f分%n", s.getName(), s.getTotalScore() + 2 // ✅正确调用位置 ) ); } } Student 以上是我目前的代码,报错地方有很多,但是我都不知道怎么修改
最新发布
05-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值