包私有自定义视图构造函数仅在发布版本上导致崩溃

时间:2018-10-08 08:31:14

标签: android

我用以下代码创建了一个自定义视图:

public class CustomTextView extends AppCompatTextView {
    CustomTextView(final Context context, @Nullable final AttributeSet attrs) {
        super(context, attrs);
    }
}

它包含在活动的布局文件中:

<?xml version="1.0" encoding="utf-8"?>
<net.chmielowski.shrinktest.CustomTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Hello World!" />

当我在调试模式下运行该应用程序时,该视图显示没有任何问题,但是在发布模式下构建该视图后,它崩溃了,但出现以下异常:

java.lang.RuntimeException: Unable to start activity ComponentInfo{net.chmielowski.shrinktest/net.chmielowski.shrinktest.MainActivity}: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class net.chmielowski.shrinktest.RedTextView
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
    at android.app.ActivityThread.-wrap14(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6682)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
 Caused by: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class net.chmielowski.shrinktest.RedTextView
 Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class net.chmielowski.shrinktest.RedTextView
 Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
    at java.lang.Class.getConstructor0(Class.java:2204)
    at java.lang.Class.getConstructor(Class.java:1683)
    at android.view.LayoutInflater.createView(LayoutInflater.java:625)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:794)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:496)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:378)
    at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
    at net.chmielowski.shrinktest.MainActivity.onCreate(MainActivity.java:11)
    at android.app.Activity.performCreate(Activity.java:6942)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2880)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
    at android.app.ActivityThread.-wrap14(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6682)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

CustomTextView的构造函数的可见性更改为public之后,问题消失并且视图正确显示。

gradle.build文件中,我没有ProGuard特定设置

buildTypes {
    release {
        signingConfig signingConfigs.config
    }
}

我的问题是:为什么会发生此异常?为什么在调试模式包中,在布局膨胀期间会接受私有构造函数,而在发布模式下却不能接受?

0 个答案:

没有答案