记录一个自定义dialog时遇见的空指针异常
android.view.InflateException: Binary XML file line #21: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:768)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:867)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
at android.view.LayoutInflater.inflate(LayoutInflater.java:519)
at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:418)
at android.app.Dialog.setContentView(Dialog.java:528)
at com.ccl.keyassistant.view.CustomDialog.<init>(CustomDialog.java:50)
at com.ccl.keyassistant.view.CustomDialog.<init>(CustomDialog.java:35)
at com.ccl.keyassistant.view.CustomDialog.<init>(CustomDialog.java:41)
at com.ccl.keyassistant.view.CustomDialog.<init>(CustomDialog.java:31)
at com.ccl.keyassistant.activity.GuideActivity.onClick(GuideActivity.java:140)
at android.view.View.performClick(View.java:6266)
at android.view.View$PerformClick.run(View.java:24730)
at android.os.Handler.handleCallback(Handler.java:793)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:176)
自定义dialog代码
public CustomDialog(Context context,int layout,int themeResId,int height,int width,int gravity,int anim){
super(context,themeResId);
//设置显示布局
//此处报空指针异常
setContentView(layout);}
//设置宽高
Window window = getWindow();
WindowManager.LayoutParams attributes = window.getAttributes();
attributes.width = width;
attributes.height = height;
attributes.gravity = gravity;
window.setAttributes(attributes);
window.setWindowAnimations(anim);
}
//此处报空指针异常
setContentView(layout)