无法从扩展DialogFragment的类创建默认构造函数,并且我已创建自己的自定义构造函数

Man*_*265 3 java android android-dialogfragment

我对有关默认构造函数的错误消息感到困惑.

我有2个MainActivityResultDialog.MainActivity创建新对话框并将2strings传递给自定义构造函数的一些方法ResultDialog.

ResultDialog延伸DialogFragment.所以我定义了自己的构造函数,但是当出现错误时我刚刚创建了一个无参数的构造函数,但仍然不允许这样做.

我已经在SO上搜索了abit,但答案有点解释屏幕旋转可能会破坏并使用默认构造函数重新创建屏幕,但仍然无法回答我如何解决这个问题.

错误是避免片段中的非默认构造函数:使用默认构造函数加上Fragment #setArguments(Bundle)

有人请帮助我,我有点困惑.我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
}
Run Code Online (Sandbox Code Playgroud)

Rag*_*dan 5

你可以用newInstance.检查文档

http://developer.android.com/reference/android/app/Fragment.html

还检查一下

为什么我要避免片段中的非默认构造函数?

 ResultDialog rd = ResultDialog.newInstance("message","title");
Run Code Online (Sandbox Code Playgroud)

然后

  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;
}
Run Code Online (Sandbox Code Playgroud)

并用于getArguments()检索值.

String message = getArguments().getString("message");
String title = getArguments().getString("title");
Run Code Online (Sandbox Code Playgroud)

getArguments()

public final Bundle getArguments ()

Added in API level 11
Return the arguments supplied when the fragment was instantiated, if any.
Run Code Online (Sandbox Code Playgroud)