我在API 23上的自定义对话框中面临一个问题,它正在崩溃。
final Dialog dialog = new Dialog(getActivity());
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.progress_dialog);
dialog.setCancelable(false);
dialog.show();
崩溃报告:
android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@3387043 -- permission denied for this window type
at android.view.ViewRootImpl.setView(ViewRootImpl.java:598)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:310)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:86)
at android.app.Dialog.show(Dialog.java:319)
at com.example.hello.fragment.ExploreFragment.exploreBeers(ExploreFragment.java:255)
at com.example.hello.fragment.ExploreFragment$4.onEditorAction(ExploreFragment.java:203)
at android.widget.TextView.onEditorAction(TextView.java:4773)
at com.android.internal.widget.EditableInputConnection.performEditorAction(EditableInputConnection.java:139)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:304)
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:78)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:152)
at android.app.ActivityThread.main(ActivityThread.java:5497)
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)
清单许可:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
根本没有工作我有点困惑,请通过我的帖子并建议我解决一些问题。
答案 0 :(得分:0)
删除 。dialog.getWindow()的setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); 并在您的应用程序中尝试。 或者如果你想在第三方应用程序上显示它
答案 1 :(得分:0)
查看我的answer on runtime permissions,
在本回答中,您可以将代码包含在访问联系人方法中。
在任何活动中实施权限的示例。
SampleActivity.java
public class SampleActivity extends AppCompatActivity{
private final int PERMISSION_CODE = 1;
Button button;
@override
onCreate(Bundle savedInstance){
super.onCreate(savedInstance);
setContentView(R.layout.your_layout);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new OnClickListener(){
@override
public void onClick(View view){
requestPermissionAndContinue();
}
});
//remaining code to continue using the app
//your actual code should also be in this same class
}
private void requestPermissionAndContinue(){
if(ContextCompat.checkSelfPermission(this, Manifest.permission.SYSTEM_ALERT_WINDOW) != PackageManager.PERMISSION_GRANTED){
if(ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.SYSTEM_ALERT_WINDOW)){
Log.e(TAG, "permission denied, show dialog");
}else{
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SYSTEM_ALERT_WINDOW}, PERMISSION_CODE);
}
}else{
showAlert();
}
}
private void showAlert(){
//your code to show alert dialog
final Dialog dialog = new Dialog(getActivity());
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.progress_dialog);
dialog.setCancelable(false);
dialog.show();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if(grantResults.length > 0 && permissions[0]==PackageManager.PERMISSION_GRANTED){
showAlert();
}else{
//redirect to settings page or ask permission again
}
}
}