Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

空指针异常 #33

Closed
duronggang opened this issue Oct 10, 2020 · 8 comments
Closed

空指针异常 #33

duronggang opened this issue Oct 10, 2020 · 8 comments
Labels
bug Something isn't working question Further information is requested

Comments

@duronggang
Copy link

首先说明框架没问题,是我用的问题,由于水平较低,不知道怎么处理是比较稳妥的,所以提个问题。Demo中 是在按钮点击时间里面去应用的框架,CustomDialogFragment 弹窗是没问题的,但是我们需求是启动activity后 去检测权限,所以我直接把代码放到onCreate 中,
@OverRide
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_java);
PermissionX.init(MainJavaActivity.this)
.permissions(Manifest.permission.WRITE_EXTERNAL_STORAGE)
.explainReasonBeforeRequest()
.onExplainRequestReason(new ExplainReasonCallbackWithBeforeParam() {
@OverRide
public void onExplainReason(ExplainScope scope, List deniedList, boolean beforeRequest) {
CustomDialogFragment dialog = new CustomDialogFragment("请允许权限选择", deniedList);
scope.showRequestReasonDialog(dialog);
}
}).onForwardToSettings(new ForwardToSettingsCallback() {
@OverRide
public void onForwardToSettings(ForwardScope scope, List deniedList) {
CustomDialogFragment dialog = new CustomDialogFragment("请允许权限选择,去设置", deniedList);
scope.showForwardToSettingsDialog(dialog);
}
}).request(new RequestCallback() {
@OverRide
public void onResult(boolean allGranted, List grantedList, List deniedList) {
if (allGranted) {
Toast.makeText(MainJavaActivity.this, "All permissions are granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainJavaActivity.this, "The following permissions are denied:" + deniedList, Toast.LENGTH_SHORT).show();
}
}
});
}
但是获取控件为空
View positiveButton = dialogFragment.getPositiveButton();
View negativeButton = dialogFragment.getNegativeButton();
请教郭神这种情况怎么处理比较好

@guolindev
Copy link
Owner

不会呀,我刚刚直接在Activity的onCreate方法里请求权限测试了一下,仍然是可以正常显示自定义对话框的,并不会空指针。

我把这个测试的demo提交了,具体可以参见这条commit 8016199

所以你可以现在把最新的源码下载下来并运行一下demo,就是直接在Activity的onCreate中请求权限的,一切正常。

至于为什么会出现空指针,我怀疑你的自定义Dialog有没有重写onCreate方法,以及有没有在onCreate方法中去调用setContentView,另外就是你的getNegativeButton和getPositiveButton中返回的控件实例是不是null,所以在这方面再调查一下看看吧。

@guolindev guolindev added the question Further information is requested label Oct 10, 2020
@guolindev guolindev pinned this issue Oct 10, 2020
@guolindev guolindev unpinned this issue Oct 10, 2020
@duronggang
Copy link
Author

郭神,你用的是CustomDialog,我用的是CustomDialogFragment

@guolindev
Copy link
Owner

这还真是一个bug,是和Fragment生命周期相关的一个问题,稍微有点头疼。

你如果着急解决,那么方案就使用和我上面一样的方案就可以了,用Dialog实现而不用DialogFragment去实现。

如果你一定要用DialogFragment,那么就再等一等,我看看能不能在下一个版本当中修复这个问题。

@guolindev guolindev added the bug Something isn't working label Oct 10, 2020
@duronggang
Copy link
Author

这么解决的,有问题吗, onWindowFocusChanged 这个方法可以作为判断activity被加载完成了吗,有版本适配问题吗

@OverRide
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(hasFocus){
if(!firstview) {//是否第一次返回true
firstview = true;
PermissionX.init(MainJavaActivity.this)
.permissions(Manifest.permission.WRITE_EXTERNAL_STORAGE)
.explainReasonBeforeRequest()
.onExplainRequestReason(new ExplainReasonCallbackWithBeforeParam() {
@OverRide
public void onExplainReason(ExplainScope scope, List deniedList, boolean beforeRequest) {
// CustomDialog customDialog = new CustomDialog(MainJavaActivity.this, "PermissionX needs following permissions to continue", deniedList);
CustomDialogFragment customDialog = new CustomDialogFragment("PermissionX needs following permissions to continue", deniedList);

                            scope.showRequestReasonDialog(customDialog);

// scope.showRequestReasonDialog(deniedList, "PermissionX needs following permissions to continue", "Allow");
}
})
.onForwardToSettings(new ForwardToSettingsCallback() {
@OverRide
public void onForwardToSettings(ForwardScope scope, List deniedList) {
scope.showForwardToSettingsDialog(deniedList, "Please allow following permissions in settings", "Allow");
}
})
.request(new RequestCallback() {
@OverRide
public void onResult(boolean allGranted, List grantedList, List deniedList) {
if (allGranted) {
Toast.makeText(MainJavaActivity.this, "All permissions are granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainJavaActivity.this, "The following permissions are denied:" + deniedList, Toast.LENGTH_SHORT).show();
}
}
});
}
}
}

@guolindev
Copy link
Owner

其实就是Activity的创建周期没走完,这个时候往Activity里去添加了一个DialogFragment,即使我是调用的showNow方法,但是DialogFragment的onCreateView并不会执行,它要等Activity的创建周期走完才能执行。

所以你用任何方法,只要能在Activity的创建周期之后再去请求,都是没有问题的。

但是我作为库的作者,还是得想办法怎么解决这个bug。

@duronggang
Copy link
Author

感谢,我先采用CustomDialog,对源码不熟悉,不知道onWindowFocusChanged 会不会有问题,期待郭神的解决方案

@guolindev
Copy link
Owner

好的,这个issue先保留着,等我有了解决方案再更新这个issue。

@guolindev
Copy link
Owner

不好意思,我对于这个问题研究了很久,试了好几种解决方案,但是始终都没能解决,Fragment在生命周期上的问题处理确实太特殊了,而DialogFragment相当于又启了一个Fragment,在和隐藏Fragment的事务冲突上我始终没能找到一个好的解决方案。

所以在这个问题上我确实有点无能为力了,或许当时提供对DialogFragment的支持就不是个正确的决定。

当然,虽然在库的层面我没找到一个好的解决办法,但是在使用层面解决的办法还是挺多的,比如你不在onCreate中去请求权限,在onResume中去请求就可以解决这个问题,或者用post延迟执行一下权限请求的时机,都是可以的。

这个问题就先关闭了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants