kongzue / DialogV3

🔥空祖家的对话框工具V3版,集成三种风格+夜间模式对话框,以及等待、提示、分享等特色对话框,支持花式自定义
1.26k stars 163 forks source link

关于dialog报错问题:Expected receiver of type android.app.AlertDialog, but got java.lang.ref.WeakReference #221

Open miskiyang opened 3 years ago

miskiyang commented 3 years ago

我的compileSdkVersion 30

MessageDialog里面的showEvent方法反射获取AlertController报错,这个对象已经被标为@UnsupportedAppUsage, 不允许反射拿了,博主更新处理下这个问题

报错信息: java.lang.IllegalArgumentException: Expected receiver of type android.app.AlertDialog, but got java.lang.ref.WeakReference W/System.err: at java.lang.reflect.Field.get(Native Method) W/System.err: at com.kongzue.dialog.v3.MessageDialog.showEvent(MessageDialog.java:630)

ppg408331701 commented 3 years ago

问个问题,是compileSdkVersion == 30 才会报错 还是,android 11 的手机都会报错?

----------------更新--------------------

奇怪,我用 compileSdkVersion = 30 minSdkVersion = 21 targetSdkVersion = 30

这个配置是去编译,然后跑在android 11的模拟器上,并没有崩溃

kongzue commented 3 years ago

想问下用的是AndroidX版本么?目前我这边的项目也在用但没遇到这个问题

ppg408331701 commented 3 years ago

想问下用的是AndroidX版本么?目前我这边的项目也在用但没遇到这个问题

我用的androidX版本但没有崩溃,等我的小米9更新android11后我再测试,但是考虑到android越来越严厉的策略,感觉确实应该减少反射的使用

kongzue commented 3 years ago

MessageDialog不存在反射


发件人: ppg408331701 notifications@github.com 发送时间: Tuesday, December 22, 2020 9:11:16 AM 收件人: kongzue/DialogV3 DialogV3@noreply.github.com 抄送: Kongzue myzcxhh@live.cn; Comment comment@noreply.github.com 主题: Re: [kongzue/DialogV3] 关于dialog报错问题:Expected receiver of type android.app.AlertDialog, but got java.lang.ref.WeakReference (#221)

想问下用的是AndroidX版本么?目前我这边的项目也在用但没遇到这个问题

我用的androidX版本但没有崩溃,等我的小米9更新android11后我再测试,但是考虑到android越来越严厉的策略,感觉确实应该减少反射的使用

― You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://apac01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fkongzue%2FDialogV3%2Fissues%2F221%23issuecomment-749281845&data=04%7C01%7C%7C10118381177a4d8c557f08d8a6167d1f%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637441962802323037%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=tgkVPTjnxfNB5oQfARvMWjOtkP9FPZb6ezt5nyNbP0U%3D&reserved=0, or unsubscribehttps://apac01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FACNFSH7JYOXUTHIK4MGRRQ3SV7W3JANCNFSM4U363Y4Q&data=04%7C01%7C%7C10118381177a4d8c557f08d8a6167d1f%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637441962802328035%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=A3cSWFA%2F7VpiEDSVnkZsbF5mFqFYXz15PWLI1fqPJlk%3D&reserved=0.

z2700589 commented 3 years ago

我也存在这个问题

miskiyang commented 3 years ago

问个问题,是compileSdkVersion == 30 才会报错 还是,android 11 的手机都会报错?

----------------更新--------------------

奇怪,我用 compileSdkVersion = 30 minSdkVersion = 21 targetSdkVersion = 30

这个配置是去编译,然后跑在android 11的模拟器上,并没有崩溃

并不是崩溃,而是引用历史缓存的dailog由于拿不到导致dialog显示不出来,你可以做个简单的测试,在你测试的demo开启messagedialog,然后点击dialog关闭后跳转到其他app,然后在onresume将该dialog带出来,有时候就会带不出来,带不出来的时候就会有这个错误提示

miskiyang commented 3 years ago

MessageDialog不存在反射 ____ 发件人: ppg408331701 notifications@github.com 发送时间: Tuesday, December 22, 2020 9:11:16 AM 收件人: kongzue/DialogV3 DialogV3@noreply.github.com 抄送: Kongzue myzcxhh@live.cn; Comment comment@noreply.github.com 主题: Re: [kongzue/DialogV3] 关于dialog报错问题:Expected receiver of type android.app.AlertDialog, but got java.lang.ref.WeakReference (#221) 想问下用的是AndroidX版本么?目前我这边的项目也在用但没遇到这个问题 我用的androidX版本但没有崩溃,等我的小米9更新android11后我再测试,但是考虑到android越来越严厉的策略,感觉确实应该减少反射的使用 ― You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://apac01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fkongzue%2FDialogV3%2Fissues%2F221%23issuecomment-749281845&data=04%7C01%7C%7C10118381177a4d8c557f08d8a6167d1f%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637441962802323037%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=tgkVPTjnxfNB5oQfARvMWjOtkP9FPZb6ezt5nyNbP0U%3D&reserved=0, or unsubscribehttps://apac01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FACNFSH7JYOXUTHIK4MGRRQ3SV7W3JANCNFSM4U363Y4Q&data=04%7C01%7C%7C10118381177a4d8c557f08d8a6167d1f%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637441962802328035%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=A3cSWFA%2F7VpiEDSVnkZsbF5mFqFYXz15PWLI1fqPJlk%3D&reserved=0.

我引用的版本不对嘛?我这边看代码是:630行,而且AlertDialog和androidx无关,这个属于android自身的 Field mAlert = AlertDialog.class.getDeclaredField("mAlert"); mAlert.setAccessible(true); Object mAlertController = mAlert.get(dialog);

kongzue commented 3 years ago

MessageDialog不存在反射 ____ 发件人: ppg408331701 notifications@github.com 发送时间: Tuesday, December 22, 2020 9:11:16 AM 收件人: kongzue/DialogV3 DialogV3@noreply.github.com 抄送: Kongzue myzcxhh@live.cn; Comment comment@noreply.github.com 主题: Re: [kongzue/DialogV3] 关于dialog报错问题:Expected receiver of type android.app.AlertDialog, but got java.lang.ref.WeakReference (#221) 想问下用的是AndroidX版本么?目前我这边的项目也在用但没遇到这个问题 我用的androidX版本但没有崩溃,等我的小米9更新android11后我再测试,但是考虑到android越来越严厉的策略,感觉确实应该减少反射的使用 ― You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://apac01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fkongzue%2FDialogV3%2Fissues%2F221%23issuecomment-749281845&data=04%7C01%7C%7C10118381177a4d8c557f08d8a6167d1f%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637441962802323037%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=tgkVPTjnxfNB5oQfARvMWjOtkP9FPZb6ezt5nyNbP0U%3D&reserved=0, or unsubscribehttps://apac01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FACNFSH7JYOXUTHIK4MGRRQ3SV7W3JANCNFSM4U363Y4Q&data=04%7C01%7C%7C10118381177a4d8c557f08d8a6167d1f%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637441962802328035%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=A3cSWFA%2F7VpiEDSVnkZsbF5mFqFYXz15PWLI1fqPJlk%3D&reserved=0.

我引用的版本不对嘛?我这边看代码是:630行,而且AlertDialog和androidx无关,这个属于android自身的 Field mAlert = AlertDialog.class.getDeclaredField("mAlert"); mAlert.setAccessible(true); Object mAlertController = mAlert.get(dialog);

刚刚查了下,这段代码是用于设置AlertDialog的按钮文字样式的,因为Google并没有公开的设置方法不得不这么做。因历史原因,DialogV3框架依然是基于DialogFragment进行的实现,现在我更推荐更新到DialogX这个库( https://github.com/kongzue/DialogX )它的Material主题Dialog是纯粹自己做的实现,不存在这些问题。

0-8-4 commented 3 years ago

你好,我在sdk30也遇到了这个问题