我正在尝试创建BaseDialog并在整个应用程序中重新使用它。但是我在这方面面临一个问题。我的BaseDialog类是
public class BaseDialog extends Dialog {
private Context mContext;
public BaseDialog(@NonNull Context context) {
super(context);
mContext = context;
}
public BaseDialog(@NonNull Context context, int themeResId) {
super(context, themeResId);
mContext = context;
}
protected BaseDialog(@NonNull Context context, boolean cancelable, @Nullable DialogInterface.OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
mContext = context;
}
@Override
public void show() {
/**
* My Common code here for all alert
*/
super.show();
}
}
我正在使用此类,如下所示。
public class ConfirmCancelDialog extends BaseDialog{
public ConfirmCancelDialog(Context context, boolean cancelable,
OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
}
public ConfirmCancelDialog(Context context, int theme) {
super(context, theme);
}
public ConfirmCancelDialog(Context context) {
super(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.confirm_cancel_dialog);
//My remaining code
}
}
但是,如果我尝试使用以下代码,则会出错。
private void showDialog(){
BaseDialog passwordDialog = new BaseDialog(context);
passwordDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
passwordDialog.setContentView(R.layout.password);
//Rest of my code
}
我在setContentView();
中遇到错误
但是如果我使用Dialog passwordDialog = new Dialog(context);
,我的代码就可以正常工作。我想在整个应用程序中使用BaseDialog。有人可以帮我吗?
我遇到以下错误:
at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.view.LayoutInflater.createView(LayoutInflater.java:613)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:812)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:752)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:883)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:846)
at android.view.LayoutInflater.inflate(LayoutInflater.java:522)
at android.view.LayoutInflater.inflate(LayoutInflater.java:430)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:412)
at android.app.Dialog.setContentView(Dialog.java:519)
at sonosite.com.dialogwithsound.BaseDialog.<init>(BaseDialog.java:50)
at sonosite.com.dialogwithsound.MainActivity.showPasswordPromptDialog(MainActivity.java:219)
at sonosite.com.dialogwithsound.MainActivity$2.onClick(MainActivity.java:61)
at android.view.View.performClick(View.java:5647)
at android.view.View$PerformClick.run(View.java:22465)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6238)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
Suppressed: java.io.IOException: No original dex files found for dex location /data/app/sonosite.com.dialogwithsound-1/split_lib_slice_7_apk.apk
at dalvik.system.DexFile.openDexFileNative(Native Method)
at dalvik.system.DexFile.openDexFile(DexFile.java:367)
at dalvik.system.DexFile.<init>(DexFile.java:112)
at dalvik.system.DexFile.<init>(DexFile.java:77)
at dalvik.system.DexPathList.loadDexFile(DexPathList.java:359)
at dalvik.system.DexPathList.makeElements(DexPathList.java:323)
at dalvik.system.DexPathList.makeDexElements(DexPathList.java:263)
at dalvik.system.DexPathList.<init>(DexPathList.java:126)
at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:48)
at dalvik.system.PathClassLoader.<init>(PathClassLoader.java:64)
2019-02-14 16:14:11.795 30451-30451/sonosite.com.dialogwithsound E/AndroidRuntime: at com.android.internal.os.PathClassLoaderFactory.createClassLoader(PathClassLoaderFactory.java:43)
at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:58)
at android.app.LoadedApk.createOrUpdateClassLoaderLocked(LoadedApk.java:522)
at android.app.LoadedApk.getClassLoader(LoadedApk.java:555)
at android.app.ActivityThread.getTopLevelResources(ActivityThread.java:1924)
at android.app.LoadedApk.getResources(LoadedApk.java:768)
at android.app.ContextImpl.<init>(ContextImpl.java:2077)
at android.app.ContextImpl.createAppContext(ContextImpl.java:2022)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5394)
at android.app.ActivityThread.-wrap2(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:102)