java 错误:这个片段应该提供一个默认构造函数(一个没有参数的公共构造函数)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29762949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error: This fragment should provide a default constructor (a public constructor with no arguments)
提问by Oreo
My class extends DialogFragment
like this:
我的课程DialogFragment
是这样扩展的:
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
EditText editDate;
private Calendar dateTime = Calendar.getInstance();
private SimpleDateFormat dateFormatter = new SimpleDateFormat("dd MMMM yyyy");
public DatePickerFragment(EditText editText) {
editDate = editText;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
dateTime.set(year, monthOfYear, dayOfMonth);
editDate.setText(dateFormatter
.format(dateTime.getTime()));
}
}
And this is how i am using it in Activity:
这就是我在 Activity 中使用它的方式:
public void showDatePickerDialog(View v) {
new DatePickerFragment((EditText) v).show(getSupportFragmentManager(), "datePicker");
}
And calling like this:
并像这样调用:
<EditText
android:id="@+id/editDate"
android:onClick="showDatePickerDialog" />
But I am always getting :
但我总是得到:
Error: This fragment should provide a default constructor (a public constructor with no arguments)
回答by Deividi Cavarzan
The newer version of Android SDK forces you to get a empty, no-args constructor. It's now a good practice to do this. This allows you to save the instance state into bundle and Android will recreate your fragment calling the default constructor.
较新版本的 Android SDK 强制您获得一个空的、无参数的构造函数。现在这样做是一个很好的做法。这允许您将实例状态保存到包中,Android 将调用默认构造函数重新创建您的片段。
In this case, you have the following solutions:
在这种情况下,您有以下解决方案:
First, create the default constructor:
首先,创建默认构造函数:
public DatePickerFragment() {}
Create the instance and set the EditText via setter method:
创建实例并通过 setter 方法设置 EditText:
DatePickerFragment fragment = new DatePickerFragment();
fragment.setEditText(v); // create this setter
fragment.show();
Since EditText is parcelable, you can also set as arguments:
由于 EditText 是可分块的,您还可以设置为参数:
DatePickerFragment fragment = new DatePickerFragment();
Bundle bundle = new Bundle();
bundle.putExtra("EditText", v);
fragment.setArguments(bundle); // setArguments is a method that already exists in fragments.
fragment.show(getSupportFragmentManager(), "DatePicker");
[EDIT]As suggested, try to ignore these errors configuring build.gradle
like this:
[编辑]按照建议,尝试忽略这些错误配置,build.gradle
如下所示:
lintOptions {
abortOnError false
checkReleaseBuilds false
}
This will not stop the build of your application by using non-default constructor in fragments.
这不会通过在片段中使用非默认构造函数来停止应用程序的构建。
回答by inmyth
You need to remove this constructor
您需要删除此构造函数
public DatePickerFragment(EditText editText) {
editDate = editText;
}
You should not pass a View reference to Fragment. If you want to update an EditText in Activity or elsewhere then use listener.
您不应将 View 引用传递给 Fragment。如果您想在 Activity 或其他地方更新 EditText,请使用侦听器。
回答by Jivraj S Shekhawat
public DatePickerFragment() {}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
dateTime.set(year, monthOfYear, dayOfMonth);
// editDate.setText(dateFormatter
// .format(dateTime.getTime()));
Intent intent = new Intent();
intent.setAction("CUSTOM_INTENT_FOR_SETTING_EDITTEXT");
Bundle bundle = new Bundle();
bundle.putString("my_data", dateTime.getTime.toString());
intent.putExtras(bundle);
sendBroadcast(intent);
}
public class SubmitFragment extends Fragment {
View view;
EditText editDate;
String strDate;
Button btnSubmit;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_submit, container, false);
editDate = (EditText) view.findViewById(R.id.editDate);
btnSend = (Button) view.findViewById(R.id.buttonSend);
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
strDate = editDate.getText().toString();
Log.v("strDate..... >>>", strDate);
}
});
return view;
}
// Our handler for received Intents. This will be called whenever an Intent
// with an action named "CUSTOM_INTENT_FOR_SETTING_EDITTEXT" is broadcasted.
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Get extra data included in the Intent
String message = intent.getStringExtra("my_data");
Log.d("receiver", "message: " + message);
*****// Use this message to set Edittext here...*****
}
};
@Override
protected void onPause() {
// Unregister since the activity is about to be closed.
LocalBroadcastManager.getInstance(getactivity()).unregisterReceiver(mMessageReceiver);
super.onDestroy();
}
@Override
protected void onResume() {
// register the receiver here..
super.onResume(); LocalBroadcastManager.getInstance(getactivity()).registerReceiver(mMessageReceiver,
new IntentFilter("CUSTOM_INTENT_FOR_SETTING_EDITTEXT"));
}
}
Hope this helps..what exactly i am doing here is using localBroadcast to do the needful..another way would have been using a interface to the settext but one wrong thing about that approach is that when app is in background for so long , it will return null when oncreateview is called again.
希望这会有所帮助..我在这里所做的究竟是使用 localBroadcast 来做需要的事情..另一种方法是使用 settext 的接口,但这种方法的一个错误是当应用程序在后台运行这么长时间时,它再次调用 oncreateview 时将返回 null。
回答by Nirmal Sinh Revar
Add @SuppressLint("ValidFragment")
添加@SuppressLint("ValidFragment")
before your class,
在你上课之前,
public class DatePickerFragment extends
公共类 DatePickerFragment 扩展
I hope it helps you
我希望它能帮助你