记录开发中遇到的 bug,不再让自己重复地被同样的 bug 折磨。
时间:2019年3月1日16:41:59
错误日志如下:
2019-03-01 21:21:35.573 17900-17900/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.bat.clean, PID: 17900
java.lang.RuntimeException: Unable to resume activity {com.bat.clean/com.bat.clean.battery.BatterySaverActivity}: java.lang.RuntimeException: Cannot create an instance of class com.bat.clean.battery.scanning.BatterySaverScanningViewModel
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4278)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4319)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2065)
at android.os.Handler.dispatchMessage(Handler.java:109)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7555)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:469)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:963)
Caused by: java.lang.RuntimeException: Cannot create an instance of class com.bat.clean.battery.scanning.BatterySaverScanningViewModel
at android.arch.lifecycle.r$a.a(Unknown Source:80)
at android.arch.lifecycle.r.a(Unknown Source:35)
at com.bat.clean.battery.scanning.a.c(Unknown Source:15)
at android.support.v4.app.Fragment.i(Unknown Source:13)
at android.support.v4.app.i.a(Unknown Source:600)
at android.support.v4.app.i.b(Unknown Source:38)
at android.support.v4.app.i.a(Unknown Source:46)
at android.support.v4.app.c.e(Unknown Source:160)
at android.support.v4.app.i.b(Unknown Source:38)
at android.support.v4.app.i.a(Unknown Source:112)
at android.support.v4.app.i.a(Unknown Source:88)
at android.support.v4.app.i.i(Unknown Source:21)
at android.support.v4.app.f.c(Unknown Source:4)
at android.support.v4.app.FragmentActivity.onStart(Unknown Source:29)
at android.support.v7.app.AppCompatActivity.onStart(Unknown Source:0)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1340)
at android.app.Activity.performStart(Activity.java:7364)
at android.app.Activity.performRestart(Activity.java:7439)
at android.app.Activity.performResume(Activity.java:7444)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4246)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4319)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2065)
at android.os.Handler.dispatchMessage(Handler.java:109)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7555)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:469)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:963)
Caused by: java.lang.IllegalAccessException: java.lang.Class is not accessible from java.lang.Class
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
at android.arch.lifecycle.r$a.a(Unknown Source:26)
at android.arch.lifecycle.r.a(Unknown Source:35)
at com.bat.clean.battery.scanning.a.c(Unknown Source:15)
at android.support.v4.app.Fragment.i(Unknown Source:13)
at android.support.v4.app.i.a(Unknown Source:600)
at android.support.v4.app.i.b(Unknown Source:38)
at android.support.v4.app.i.a(Unknown Source:46)
at android.support.v4.app.c.e(Unknown Source:160)
at android.support.v4.app.i.b(Unknown Source:38)
at android.support.v4.app.i.a(Unknown Source:112)
at android.support.v4.app.i.a(Unknown Source:88)
at android.support.v4.app.i.i(Unknown Source:21)
at android.support.v4.app.f.c(Unknown Source:4)
at android.support.v4.app.FragmentActivity.onStart(Unknown Source:29)
at android.support.v7.app.AppCompatActivity.onStart(Unknown Source:0)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1340)
at android.app.Activity.performStart(Activity.java:7364)
at android.app.Activity.performRestart(Activity.java:7439)
at android.app.Activity.performResume(Activity.java:7444)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4246)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4319)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2065)
at android.os.Handler.dispatchMessage(Handler.java:109)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7555)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:469)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:963)
原因分析:
从上面的日志可以看出,最终指向的是 IllegalAccessException
这个异常,查询一下 java 文档:
当应用程序试图反射性地创建一个实例(而不是数组)、设置或获取一个字段,或者调用一个方法,但当前正在执行的方法无法访问指定类、字段、方法或构造方法的定义时,抛出 IllegalAccessException。
在项目中,在多个地方用到了 ViewModel
,但是别的地方都是正常的。对比一下差异,发现出问题的 BatterySaverScanningViewModel
这个类没有加上 public
的权限修饰符。加上 public
权限修饰符,再次运行,没有报错了。
另外,构造方法上不加 public,也会导致这一问题的出现。最开始的考虑是不暴露更多的类出去,才使用了默认的权限修饰符。没想到,弄巧成拙。不过,自己还不明白。知道的同学,麻烦在留言区讨论啊。
时间:2019年3月2日16:16:22
问题描述:使用 javac 编译 .java 类时,出现这个错误。我的文件中是含有中文字符:
public class Main {
public static void main(String[] args) {
ChineseProguard.start();
new SpecificCharacterProguard("proguard-o0O.txt", '0', 'o', 'O', '8', '〇').start();
new SpecificCharacterProguard("proguard-1il.txt", '1', 'i', 'l', 'L', 'I', '丨').start();
new SpecificCharacterProguard("proguard-socialism.txt", "富强民主文明和谐自由平等公正法治爱国敬业诚信友善".toCharArray()).start();
}
}
原因分析:由于JDK是国际版的,我们在用javac编译时,编译程序首先会获得我们操作系统默认采用的编码格式(GBK),然后JDK就把Java源文件从GBK编码格式转换为Java内部默认的Unicode格式放入内存中,然后javac把转换后的Unicode格式的文件编译成class类文件,此时,class文件是Unicode编码的,它暂存在内存中,紧接着,JDK将此以Unicode格式编码的class文件保存到操作系统中形成我们见到的class文件。当我们不加设置就编译时,相当于使用了参数:javac -encoding GBK Test.java,就会出现不兼容的情况。
解决办法:使用-encoding参数指明编码方式:javac -encoding UTF-8 Test.java,就可以了。
参考 https://www.cnblogs.com/lucky-zhangcd/p/8409810.html
Process: com.example.mynetworkmonitor, PID: 14177
java.lang.SecurityException: NetworkStats: Neither user 10663 nor current process has android.permission.READ_NETWORK_USAGE_HISTORY.
at android.os.Parcel.readException(Parcel.java:1602)
at android.os.Parcel.readException(Parcel.java:1555)
at android.net.INetworkStatsSession$Stub$Proxy.getSummaryForAllUid(INetworkStatsSession.java:355)
at android.app.usage.NetworkStats.startSummaryEnumeration(NetworkStats.java:330)
at android.app.usage.NetworkStatsManager.querySummary(NetworkStatsManager.java:166)
at com.example.mynetworkmonitor.MainActivity$1.onClick(MainActivity.java:43)
at android.view.View.performClick(View.java:5264)
at android.view.View$PerformClick.run(View.java:21297)
at android.os.Handler.handleCallback(Handler.java:743)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5546)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
时间:2019年3月6日12:48:51
问题描述:使用 javac 命令编译一个位于 access 文件夹中的 Dinner.java 类,这个文件的 package 信息是 package access;
报出的错误。
javac access.Dinner.java
解决办法:改用 javac access/Dinner.java
javac access/Dinner.java
不再出现上面的报错了。
错误日志如下:
java.lang.RuntimeException: cannot find implementation for com.bat.clean.db.AppDatabase. AppDatabase_Impl does not exist
at android.arch.persistence.room.Room.getGeneratedImplementation(Room.java:93)
at android.arch.persistence.room.RoomDatabase$Builder.build(RoomDatabase.java:630)
at com.bat.clean.db.AppDatabase.getInstance(AppDatabase.java:22)
at com.bat.clean.db.LocalGeneralResultInfoDataSource.getInstance(LocalGeneralResultInfoDataSource.java:25)
at com.bat.clean.service.InitService.lambda$initGeneralResultInfos$102(InitService.java:80)
at com.bat.clean.service.-$$Lambda$InitService$5yqqvD0AWlnqVQDqaypUvR-00DA.run(lambda)
at com.library.common.threadhelper.ThreadPool$TaskWithPriority.run(ThreadPool.java:165)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:833)
时间:2019年3月13日10:42:44
原因分析:Room 的依赖是写在工具模块里,如下:
dependencies {
api "android.arch.persistence.room:runtime:$rootProject.room_version"
annotationProcessor "android.arch.persistence.room:compiler:$rootProject.room_version"
}
而主模块依赖于工具模块,在主模块里就没有写 Room 的依赖
解决办法:
在主模块里添加 Room 的依赖:
dependencies {
annotationProcessor "android.arch.persistence.room:compiler:$rootProject.room_version"
}
解决了此异常。
错误日志如下:
java.lang.IllegalStateException: Room cannot verify the data integrity. Looks like you've changed schema but forgot to update the version number. You can simply fix this by increasing the version number.
at android.arch.persistence.room.RoomOpenHelper.checkIdentity(RoomOpenHelper.java:135)
at android.arch.persistence.room.RoomOpenHelper.onOpen(RoomOpenHelper.java:115)
at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.onOpen(FrameworkSQLiteOpenHelper.java:151)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:282)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:175)
at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase(FrameworkSQLiteOpenHelper.java:96)
at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:54)
at android.arch.persistence.room.RoomDatabase.beginTransaction(RoomDatabase.java:252)
at com.bat.clean.db.GeneralResultInfoDao_Impl.insert(GeneralResultInfoDao_Impl.java:74)
at com.bat.clean.db.LocalGeneralResultInfoDataSource.insert(LocalGeneralResultInfoDataSource.java:40)
at com.bat.clean.service.InitService.lambda$initGeneralResultInfos$104(InitService.java:80)
at com.bat.clean.service.-$$Lambda$InitService$9wgKLi2EU-iYRy8rP8A3GM9XeI8.run(lambda)
at com.library.common.threadhelper.ThreadPool$TaskWithPriority.run(ThreadPool.java:165)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:833)
时间:2019年3月13日11:43:01
原因分析:自己在已经编译好的 GeneralResultInfo 类中添加一个构造方法,由于已经编译好,并且生成了 schema 文件。
解决办法:删除生成的 schema json 文件,重新运行,解决这个问题。
Caused by: android.view.InflateException: Binary XML file line #102: Error inflating class ImageView
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at android.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:303)
at android.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:284)
at com.bat.clean.notificationcleaner.NotificationCleanerIntroActivity.onCreate(NotificationCleanerIntroActivity.java:48)
at android.app.Activity.performCreate(Activity.java:5264)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f080102
at android.content.res.Resources.getValue(Resources.java:1133)
at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:331)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:198)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:191)
at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:102)
at android.support.v7.widget.AppCompatImageHelper.loadFromAttributes(AppCompatImageHelper.java:59)
at android.support.v7.widget.AppCompatImageView.(AppCompatImageView.java:78)
at android.support.v7.widget.AppCompatImageView.(AppCompatImageView.java:68)
at android.support.v7.app.AppCompatViewInflater.createImageView(AppCompatViewInflater.java:182)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
at android.support.v7.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1266)
at android.support.v7.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1316)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:684)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at android.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:303)
at android.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:284)
at com.bat.clean.notificationcleaner.NotificationCleanerIntroActivity.onCreate(NotificationCleanerIntroActivity.java:48)
at android.app.Activity.performCreate(Activity.java:5264)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
at dalvik.system.NativeStart.main(Native Method)
时间:2019年3月14日14:55:25
原因分析:在 api 23 的手机上运行是 ok 的,在 api 19 的手机上运行出错的。怀疑是图片太大导致的,其实并不是。查看日志的信息,发现 Caused by: android.content.res.Resources$NotFoundException 的信息,即资源没有找到。查看资源的位置,却在 drawable-v21 的文件夹下面。难怪找不到。
解决办法:将图片资源放在对应的 drawable-xhdpi 文件夹下。
时间:2019年3月15日17:51:02
问题描述:需要做如下的 UI 显示:
想到了用 在 strings.xml 中使用 html 标签来实现的办法:
<string name="main_content_ram_percent"><Font color=#65EAFF>%1$s</font>/<Font color=#9965EAFF>%2$s<font>string>
第二个颜色是带有透明度的颜色,但是运行后却发现是如下的效果:
设置的透明度居然没有效果。
查询一下 html font 标签的语法:
标签直接使用color颜色参数设置文字颜色,值可以直接加RGB颜色值(#FFFFFF 、#000000 、#F00)
解决办法:使用 SpannableString 来设置,代码如下:
String used = "1.67GB";
String total = "/2.94GB";
String text = used + total;
SpannableString spannableString = new SpannableString(text);
spannableString.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.main_content_boost_text_color)),
0, used.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.main_content_boost_text_light_color)),
used.length(), text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
mBinding.mainAppBar.mainContent.tvmemory.setText(spannableString);
color 资源如下:
<color name="main_content_boost_text_color">#FF65EAFFcolor>
<color name="main_content_boost_text_light_color">#9965EAFFcolor>
上面的 ForegroundColorSpan 设置,相当于 setTextColor。
时间:2019年3月21日13:01:55
问题描述:
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
C:\Users\wangzhichao>adb shell top|grep com.bat.clean
'grep' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
解决办法:
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
C:\Users\wangzhichao>adb shell "top|grep com.bat.clean"
15278 0 5% S 83 1245244K 183232K fg u0_a50 com.bat.clean
15278 0 1% S 83 1245244K 183232K fg u0_a50 com.bat.clean
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
C:\Users\wangzhichao>adb shell
root@aries:/ # top|grep com.bat.clean
15278 0 6% S 83 1276164K 199192K fg u0_a50 com.bat.clean
15278 2 1% S 83 1276164K 199192K fg u0_a50 com.bat.clean
15278 2 3% S 83 1276164K 199192K fg u0_a50 com.bat.clean
15278 0 4% S 83 1276164K 199192K fg u0_a50 com.bat.clean
15278 1 14% S 83 1276164K 199192K fg u0_a50 com.bat.clean
15278 0 2% S 83 1276164K 199192K fg u0_a50 com.bat.clean
15278 0 14% S 83 1276164K 199192K fg u0_a50 com.bat.clean
15278 0 18% S 83 1276164K 199192K fg u0_a50 com.bat.clean
有些 bug 还没有完全解决,但也记载下来。要是有一天解决了呢。相信积累的力量,总有一天会滴水石穿。