我有一个片段,我需要在其中显示自定义对话框.
请查看下面的代码.
public class MyFragment extends Fragment{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.send_layout,container,false); TextView txtView = (TextView) rootView.findViewById(R.id.tv); txtView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { openDialog(); } }); return rootView; } public void openDialog(){ AppCompatDialog dialog = new AppCompatDialog(getContext(),R.style.package_types__dialog); dialog.setContentView(R.layout.package_types_dialog); dialog.show(); } }
删除行时:
dialog.setContentView(R.layout.package_types_dialog);
FATAL EXCEPTION: main 10-20 19:42:57.489 23866-23866/in.edelworks.pickedup E/AndroidRuntime: Process: in.edelworks.pickedup,PID: 23866 10-20 19:42:57.489 23866-23866/in.edelworks.pickedup E/AndroidRuntime: android.util.AndroidRuntimeException: Window feature must be requested before adding content 10-20 19:42:57.489 23866-23866/in.edelworks.pickedup E/AndroidRuntime: at android.support.v7.app.AppCompatDelegateImplV7.throwFeatureRequestIfSubDecorInstalled(AppCompatDelegateImplV7.java:1584) 10-20 19:42:57.489 23866-23866/in.edelworks.pickedup E/AndroidRuntime: at android.support.v7.app.AppCompatDelegateImplV7.requestWindowFeature(AppCompatDelegateImplV7.java:509) 10-20 19:42:57.489 23866-23866/in.edelworks.pickedup E/AndroidRuntime: at android.support.v7.app.AppCompatDelegateImplBase.onCreate(AppCompatDelegateImplBase.java:117) 10-20 19:42:57.489 23866-23866/in.edelworks.pickedup E/AndroidRuntime: at android.support.v7.app.AppCompatDelegateImplV7.onCreate(AppCompatDelegateImplV7.java:148) 10-20 19:42:57.489 23866-23866/in.edelworks.pickedup E/AndroidRuntime: at android.support.v7.app.AppCompatDialog.onCreate(AppCompatDialog.java:60) 10-20 19:42:57.489 23866-23866/in.edelworks.pickedup E/AndroidRuntime: at android.app.Dialog.dispatchOnCreate(Dialog.java:361) 10-20 19:42:57.489 23866-23866/in.edelworks.pickedup E/AndroidRuntime: at android.app.Dialog.show(Dialog.java:262)
解决方法
我已经为你的情况实施并尝试了很多替代方案,它的工作非常好,所以我没有机会审查你的错误.但我可以建议你用AlertDialog.Builder替换AppCompatDialog,这是一个android.support.v7.app类.
替换此代码
public void openDialog(){ AppCompatDialog dialog = new AppCompatDialog(getContext(),R.style.package_types__dialog); dialog.setContentView(R.layout.package_types_dialog); dialog.show(); }
同
public void openDialog(){ AlertDialog.Builder dialog = new AlertDialog.Builder(getContext(),R.style.package_types__dialog); dialog.setView(R.layout.package_types_dialog); }
注意 :
如果您有任何处理对话事件的类,则扩展android.support.v4.app.DialogFragment的DialogFragment.并且不要忘记覆盖onCreateDialog方法.完整的课程看起来像这样
import android.os.Bundle; import android.support.v4.app.DialogFragment; /** * Created by Shreekrishna on 2/29/2016. */ public class PackageTypesDialog extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { return super.onCreateDialog(savedInstanceState); } }
这可能会解决您的问题!