當設置在我的gradle這個minifyEnabled true
開始我的應用程序時,我得到一個NullPointerException異常:的ProGuard導致空指針異常
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.ae.formulaecalendar/de.ae.formulaecalendar.view.calendar.CalendarActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2444)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2504)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1367)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
at de.ae.formulaecalendar.view.calendar.CalendarActivity.onCreate(Unknown Source)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2397)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2504)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1367)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
在閱讀過的StackOverflow我發現了proguard-rules.pro文件中的一些設置:
######### KEEP ANDROID SUPPORT V7 AND DESIGN
-keep class android.support.v7.** { *; }
-keep interface android.support.v7.** { *; }
哪個並沒有真正解決這個問題(但是之前的NullPointerException)。
最後我gradle產出的重要組成部分:
buildTypes {
release {
shrinkResources true
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.config
}
}
當我確定停用minifyEnabled
寄託都工作正常。
感謝您的幫助!
這個問題有點不清楚。保持類確實解決了空指針異常?你可以發佈'CalendarActivity.onCreate()'的代碼嗎? –
請製作[SSCCE](http://sscce.org/)。 – m0skit0
@Reaz Murshed:在此NullPointerException發生之前,有一個由ProGuard引起的前一個問題,我可以使用發佈的proguard規則來解決這個問題。但是這些規則對這個NullPointerException沒有幫助,我不知道爲什麼。對不起,如果不清楚。我添加了onCreate和佈局xml。 – Alexander