java – 无法从扩展DialogFragment的类创建默认构造函数,并且我
我对有关默认构造函数的错误消息感到困惑.
我有2个类MainActivity和ResultDialog. MainActivity中的某些方法创建一个新对话框,并将2strings传递给ResultDialog中的自定义构造函数. ResultDialog扩展了DialogFragment.所以我定义了自己的构造函数,但是当出现错误时我刚刚创建了一个无参数的构造函数,但仍然不允许这样做. 我已经在SO上搜索了abit,但答案有点解释屏幕旋转可能会破坏并使用默认构造函数重新创建屏幕,但仍然无法回答我如何解决这个问题.
有人请帮助我,我有点困惑.我的ResultDialog类的一部分: public class ResultDialog extends DialogFragment { private String message;//to hold the message to be displayed private String title;//for alert dialog title //constructor for the dialog passing along message to be displayed in the alert dialog public ResultDialog(String message,String title){ this.message = message; this.title = title; } public ResultDialog(){ //default constructor } 解决方法
您可以使用newInstance.检查文档
http://developer.android.com/reference/android/app/Fragment.html 还检查一下 Why do I want to avoid non-default constructors in fragments? ResultDialog rd = ResultDialog.newInstance("message","title"); 然后 public static ResultDialog newInstance(String message,String title) { ResultDialog f = new ResultDialog(); Bundle args = new Bundle(); args.putString("message",message); args.putString("title",title); f.setArguments(args); return f; } 并使用getArguments()来检索值. String message = getArguments().getString("message"); String title = getArguments().getString("title"); getArguments() public final Bundle getArguments () Added in API level 11 Return the arguments supplied when the fragment was instantiated,if any. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |