Proguard:ArrayAdapter.createViewFromResource 中的 NullPointerException,堆栈跟踪中没有任何我的 类

Proguard: NullPointerException in ArrayAdapter.createViewFromResource without any of my classes in the stack trace

我的应用程序启动没有任何问题。但是当我登录并且下一个 activity 即将启动时,应用程序崩溃并出现以下异常:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:453)
    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:415)
    at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:204)
    at android.widget.Spinner.onMeasure(Spinner.java:602)
    at android.view.View.measure(View.java:23169)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
    at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1187)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:706)
    at android.view.View.measure(View.java:23169)
    at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1168)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:706)
    at android.view.View.measure(View.java:23169)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
    at android.view.View.measure(View.java:23169)
    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
    at android.view.View.measure(View.java:23169)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
    at android.view.View.measure(View.java:23169)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
    at android.view.View.measure(View.java:23169)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
    at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:461)
    at android.view.View.measure(View.java:23169)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
    at com.android.internal.policy.DecorView.onMeasure(DecorView.java:716)
    at android.view.View.measure(View.java:23169)
    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2718)
    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1572)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1855)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1460)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7183)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
    at android.view.Choreographer.doCallbacks(Choreographer.java:761)
    at android.view.Choreographer.doFrame(Choreographer.java:696)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

让我吃惊的是,我的类或外部库的none出现了。 如果我保留我的整个包,下一个 activity 会成功启动。但是我也不需要使用混淆。 这也很奇怪: 如果我保留我的整个包裹,它就可以正常工作。但是如果我保留两个子包(package.name.technical & package.name.functional),它就不行了。

我保留了很多在网上找到的View things。例如:

-keep class android.widget.AutoCompleteTextView {*;}
-keep class android.widget.Spinner {*;}
-keep class android.widget.ArrayAdapter {*;}
-keep public class * extends android.widget.AdapterView {*;}
-keep public class * extends android.widget.Adapter {*;}
-keep public class * extends android.widget.BaseAdapter {*;}
-keep public class * extends android.view.View {
    public <init>(android.content.Context);
    public <init>(android.content.Context, android.util.AttributeSet);
    public <init>(android.content.Context, android.util.AttributeSet, int);
    public void set*(...);
}

我还保留了应该开始的activity。

"shrinkResources" 在 build.gradle 中未启用。

有人知道为什么吗?我真的很绝望,不知道该怎么办。

有趣的问题!让我们深入研究一下。

首先,什么是 null?好吧,ArrayAdapter.createViewFromResource 中只有 one toString() call,所以它必须是那个。

那是 getItem(position) 几行后返回的项目。

getItem 很简单:它只是 returns 来自 List 对象的一个​​项目传递给构造函数。

因此,您的问题是您的 Proguard 混淆导致您将 List 中的一个 null 项目传递给您的 ArrayAdapter 之一。也许是通过 Gson 或其他基于反射的反序列化膨胀模型的问题?