我正在尝试使用timepicker和datepicker创建一个表单但是仍然坚持这个错误。我只是一个初学者,所以请简单。 我创建了2个内部类DateDialogs和TimeDialogs。
这是java类。
package com.wavedevelopers.eventsplanning;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;
import java.util.Calendar;
public class BookingForm extends AppCompatActivity implements DatePickerDialog.OnDateSetListener,TimePickerDialog.OnTimeSetListener {
BackgroundWorker backgroundWorker;
Context ctx;
public BookingForm(Context ctx) {
this.ctx = ctx;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_booking_form);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
Button doneButton = (Button) findViewById(R.id.doneButton);
Button datePickerButton = (Button) findViewById(R.id.datePickerButton);
Button timePickerButton = (Button) findViewById(R.id.timePickerButton);
final EditText name = (EditText) findViewById(R.id.userNameEditText);
final EditText cnic = (EditText) findViewById(R.id.userCNICEditText);
final EditText phone = (EditText) findViewById(R.id.userPhoneEditText);
doneButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
backgroundWorker = new BackgroundWorker(ctx);
backgroundWorker.doInBackground(name.getText().toString(),cnic.getText().toString(),phone.getText().toString());
backgroundWorker.execute();
}
});
datePickerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DateDialogs pickerDialogs = new DateDialogs();
pickerDialogs.show(getSupportFragmentManager(),"date_picker");
}
});
timePickerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
TimeDialogs timeDialogs = new TimeDialogs();
timeDialogs.show(getSupportFragmentManager(),"time_picker");
}
});
}
@Override
public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
Toast.makeText(this,"Selected Date: " + monthOfYear + " / " + dayOfMonth + " / " + year,Toast.LENGTH_LONG).show();
}
@Override
public void onTimeSet(TimePicker timePicker, int hourOfDay, int minute) {
Toast.makeText(this,"Selected Time: " + hourOfDay + " : " + minute ,Toast.LENGTH_LONG).show();
}
public static class DateDialogs extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
BookingForm bookingForm = new BookingForm(getActivity());
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog;
datePickerDialog = new DatePickerDialog(getActivity(), bookingForm,year,month,day);
return datePickerDialog;
}
}
public static class TimeDialogs extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
BookingForm bookingForm = new BookingForm(getActivity());
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int min = calendar.get(Calendar.MINUTE);
TimePickerDialog timePickerDialog;
timePickerDialog = new TimePickerDialog(getActivity(),bookingForm,hour,min, DateFormat.is24HourFormat(getActivity()));
return timePickerDialog;
}
}
}
这是错误日志。
10-04 19:28:28.278 32605-32605/com.wavedevelopers.eventsplanning E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.wavedevelopers.eventsplanning, PID: 32605
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.wavedevelopers.eventsplanning/com.wavedevelopers.eventsplanning.BookingForm}: java.lang.InstantiationException: java.lang.Class<com.wavedevelopers.eventsplanning.BookingForm> has no zero argument constructor
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2337)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: java.lang.InstantiationException: java.lang.Class<com.wavedevelopers.eventsplanning.BookingForm> has no zero argument constructor
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1090)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
10-04 19:28:30.855 32605-32605/com.wavedevelopers.eventsplanning I/Process: Sending signal. PID: 32605 SIG: 9
答案 0 :(得分:1)
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.wavedevelopers.eventsplanning/com.wavedevelopers.eventsplanning.BookingForm}: java.lang.InstantiationException: java.lang.Class<com.wavedevelopers.eventsplanning.BookingForm> has no zero argument constructor
这一行告诉我程序希望你创建一个不带参数的BookingForm类的构造函数。
您可能希望添加以下内容:
// Constructor with no arguments.
public BookingForm() {
this.ctx = null;
}