在android studio中构建APK时出错
在我的项目中有在android studio中构建APK时出错,android,android-studio,android-gradle-plugin,Android,Android Studio,Android Gradle Plugin,在我的项目中有DialogFragment public class MyDatePicer extends DialogFragment implements DatePickerDialog.OnDateSetListener { private TextView tv; public MyDatePicer(TextView tv) { this.tv = tv; } @NonNull @Override public D
DialogFragment
public class MyDatePicer extends DialogFragment implements DatePickerDialog.OnDateSetListener {
private TextView tv;
public MyDatePicer(TextView tv) {
this.tv = tv;
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
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);
Dialog picker = new DatePickerDialog(getActivity(), this, year, month, day);
picker.setTitle("Выбереите дату");
return picker;
}
@Override
public void onStart() {
super.onStart();
Button nButton = ((AlertDialog) getDialog())
.getButton(DialogInterface.BUTTON_POSITIVE);
nButton.setText("Готово");
}
@Override
public void onDateSet(android.widget.DatePicker datePicker, int year,int month, int day) {
tv.setText(day + "-" + (month + 1) + "-" + year);
}
}
在我的文章中,我称之为:
dateBegin = (TextView) v.findViewById(R.id.dateBegin);
dateEnd = (TextView) v.findViewById(R.id.dateEnd);
....
case R.id.dateBegin:
dateDialog = new MyDatePicer(dateBegin);
dateDialog.show(getActivity().getSupportFragmentManager(), "datePicker");
break;
case R.id.dateEnd:
dateDialog = new MyDatePicer(dateEnd);
dateDialog.show(getActivity().getSupportFragmentManager(), "datePicker");
break;
一切都按我的需要进行。我在模拟器和手机上运行,它可以正常工作。但当我尝试构建android APK studio时,会产生一个错误:
Error:Error: This fragment should provide a default constructor (a public constructor with no arguments) (com.managment.pavel.managmentgradle.fragments.MyDatePicer) [ValidFragment]
我不知道如何在另一节课上传达观点(文本查看我的日期)。我不知道如何建立APK,所以我有。告诉我该修复什么?您的错误消息表明它需要一个空构造函数。试试这个:
public class MyDatePicer extends DialogFragment implements DatePickerDialog.OnDateSetListener {
private TextView tv;
public MyDatePicer(){
// empty to satisfy the compiler.
}
public MyDatePicer(TextView tv) {
this.tv = tv;
}
// truncated...
}
希望这能让你继续下去:)你已经有了一个解决问题的答案,我只想对这个方法补充几句警告 现在,添加一个空构造函数应该可以解决您的问题。但是,这受制于
onDataSet()
中的NullPointerException
在tv.settx(…)
-如果在某个时间点操作系统将尝试使用默认构造函数重新创建片段,则textview可能为null
要解决此问题,可以在设置文本之前检查null,如:
if(tv!=null) {
tv.setText(...)
}
但是,更好的方法不是将TextView作为构造函数参数传递,而是在对话框中设置一个侦听器,将结果(日期格式)传递给调用活动。
大概是这样的:
@Override
public void onDateSet(android.widget.DatePicker datePicker, int year,int month, int day) {
listener.onDateSelected(day + "-" + (month + 1) + "-" + year);
}
下面是一个使用此方法的示例:
这也可能是有用的
我自己也遇到了这个问题。虽然最好根据Android Studio给出的建议重新设计应用程序,但要绕过此错误,只需在调试模式而不是发布模式下构建应用程序。那会解决你的问题
您应该在输入密钥库密码后立即生成APK时获得选项屏幕,以确定生成的类型。错误消息意味着您应该在片段中添加默认构造函数,您是否尝试添加
public MyDatePicer(){}
?现在另一个错误:错误:在片段中避免非默认构造函数:使用默认构造函数加上片段#setArguments(Bundle),而不是[ValidFragment],我无法修复。错误:错误:在片段中避免非默认构造函数:使用默认构造函数加上片段#setArguments(Bundle),而不是[ValidFragment]删除接受TextView
参数的构造函数,因此您将只剩下默认构造函数。然后使用接口回调将结果发送到调用活动,然后在活动中将文本设置为TextView
我无法将示例用于代码。我不明白如何确定在哪个TextView中插入结果?我可以在我的代码上树立一个榜样,所以我花了你更多的时间?