通用Dialog避免IllegalArgumentException(上)

简介: NewDialog.java避免IllegalArgumentException

NewDialog.java


避免IllegalArgumentException


关于错误分析在下面

package com.youpinwallet.ypw.view;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.WindowManager;
import com.youpinwallet.ypw.R;
/*
 * 白色背景,垂直居中
 * @author Administrator
 *
 */
public class NewDialog extends Dialog {
    Activity mParentActivity;
    public NewDialog(Context context, int theme) {
        super(context, theme);
        mParentActivity= (Activity) context;
    }
    public NewDialog(Context context) {
        super(context);
    }
    /**
     * 弹出自定义ProgressDialog
     *
     * @param context        上下文
     * @param cancelable     是否按返回键取消
     * @param cancelListener 按下返回键监听
     * @return
     */
    public static NewDialog create(Context context, boolean cancelable, OnCancelListener cancelListener) {
        NewDialog dialog = new NewDialog(context, R.style.Custom_Progress);
        dialog.setContentView(R.layout.progress_layout);
        // 按返回键是否取消
        dialog.setCancelable(cancelable);
        // 监听返回键处理
        dialog.setOnCancelListener(cancelListener);
        // 设置居中
        dialog.getWindow().getAttributes().gravity = Gravity.CENTER;
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        // 设置背景层透明度
        lp.dimAmount = 0.2f;
        dialog.getWindow().setAttributes(lp);
//    dialog.show();
        return dialog;
    }
    @Override
    public void dismiss()
    {
        // 避免依附界面销毁跑出的异常java.lang.IllegalArgumentException: View=com.android.internal.policy.PhoneWindow$DecorView
        if (mParentActivity != null && !mParentActivity.isFinishing())
        {
            super.dismiss();    //调用超类对应方法
        }
    }
}


NewDialogUtils.java


package com.qunarjie.qnj.utils;
import android.content.Context;
import com.qunarjie.qnj.view.NewDialog;
//请稍等通用
public class NewDialogUtils {
    private static NewDialog dialog = null;
    public static void setNewDialog(Context context) {
        if (context != null) {
            if (dialog != null) {
                dialog.dismiss();
                dialog = null;
            }
            if (dialog == null) {
                dialog = NewDialog.create(context, true, null);
            }
            dialog.show();
        }
    }
    public static void dismissNewDialog() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
            dialog = null;
        }
    }
}


R.style.Custom_Progress


styles.xml

<!-- 自定义Progress -->
<style name="Custom_Progress" parent="@android:style/Theme.Dialog">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowCloseOnTouchOutside">false</item>
    <item name="android:windowNoTitle">true</item>
</style>


progress_layout.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@drawable/shape_dialog_white"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:paddingBottom="20dp"
    android:paddingLeft="30dp"
    android:paddingRight="30dp"
    android:paddingTop="20dp">
    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:indeterminateDrawable="@drawable/progressbar" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/mdp_15"
        android:text="请稍等..."
        android:textColor="@color/color_text_dark"
        android:textSize="@dimen/msp_14" />
</LinearLayout>


shape_dialog_white.xml


<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="5dp" />
    <solid android:color="@color/white" />
</shape>


progressbar.xml


<?xml version="1.0" encoding="utf-8"?>
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toDegrees="3600">
    <shape
        android:innerRadiusRatio="3"
        android:shape="ring"
        android:thicknessRatio="8"
        android:useLevel="false">
        <gradient
            android:centerY="0.50"
            android:endColor="#12b7f5"
            android:startColor="#038fef"
            android:type="sweep"
            android:useLevel="false" />
    </shape>
</animated-rotate>



相关文章
|
4月前
Flutter-解决Try catch出现异常:type ‘_TypeError‘ is not a subtype of type ‘Exception‘ in type cast
Flutter-解决Try catch出现异常:type ‘_TypeError‘ is not a subtype of type ‘Exception‘ in type cast
91 1
|
Java 编译器 程序员
Checked Exception 和 Unchecked Exception 有什么区别?
Checked Exception 和 Unchecked Exception 有什么区别?
|
7月前
|
存储 安全 API
C++ 17 新特性 C++ String View:了解C++ 17 std::string_view的使用场景
C++ 17 新特性 C++ String View:了解C++ 17 std::string_view的使用场景
311 2
|
4月前
java.lang.IllegalStateException: Could not find method onClickcrea(View) in a parent or ancestor Con
java.lang.IllegalStateException: Could not find method onClickcrea(View) in a parent or ancestor Con
59 1
|
数据安全/隐私保护
input中常用的type属性与使用场景
input中常用的type属性与使用场景
67 0
NullPointerException:method 'android.content.BroadcastReceiver.onReceive' on a null object reference
NullPointerException:method 'android.content.BroadcastReceiver.onReceive' on a null object reference
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionVie
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionVie
|
JavaScript
element ui 自定义主题失败(primordials is not defined)
最近在使用Element ui 自定义主题时,遇到了问题,就是一直提示primordials is not defined。简言之,就是node版本的问题。所以需要降低node版本。
|
安全 Java 开发工具
通用Dialog避免IllegalArgumentException(下)
今天遇到一个很奇特的问题,当用户设置了PIN码,在锁屏界面正常解锁PIN码后,进入Launcher时显示com.Android.phone 已停止运行。一开始猜想会不会是解锁PIN码的时候处理导致了Phone进程报错,通过log分析找到了问题的大概原因:
276 0
|
Kotlin
【错误记录】布局组件加载错误 ( Attempt to invoke virtual method ‘xxx$Callback android.view.Window.getCallback()‘ )
【错误记录】布局组件加载错误 ( Attempt to invoke virtual method ‘xxx$Callback android.view.Window.getCallback()‘ )
318 0