单击edittext时应用程序崩溃

时间:2016-06-14 16:14:33

标签: android

当我点击时间或日期编辑文本时,时间选择器和日期选择器都没有出现,应用程序崩溃,并且在 logcat 中出现:

  

无法在父级或祖先中找到方法DatePickerDialog(View)   android的上下文:在视图类上定义的onClick属性   android.support.v7.widget.AppCompatEditText,id为'input2'

先谢谢

这是我的活动

public class MainActivity3 extends AppCompatActivity implements
    View.OnClickListener {


EditText txtDate, txtTime;
private int mYear, mMonth, mDay, mHour, mMinute;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.to_do_dialog_view);

    txtDate=(EditText)findViewById(R.id.input2);
    txtDate.setOnClickListener(this);

    txtTime=(EditText)findViewById(R.id.input3);
    txtTime.setOnClickListener(this);



}


public void onClick(View v) {

    if (v == txtDate) {

        // Get Current Date
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);


        DatePickerDialog datePickerDialog = new DatePickerDialog(this,
                new DatePickerDialog.OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker view, int year,
                                          int monthOfYear, int dayOfMonth) {

                        txtDate.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);

                    }
                }, mYear, mMonth, mDay);
        datePickerDialog.show();
    }
    if (v == txtTime) {

        // Get Current Time
        final Calendar c = Calendar.getInstance();
        mHour = c.get(Calendar.HOUR_OF_DAY);
        mMinute = c.get(Calendar.MINUTE);

        // Launch Time Picker Dialog
        TimePickerDialog timePickerDialog = new TimePickerDialog(this,
                new TimePickerDialog.OnTimeSetListener() {

                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay,
                                          int minute) {

                        txtTime.setText(hourOfDay + ":" + minute);
                    }
                }, mHour, mMinute, false);
        timePickerDialog.show();
    }
}
}

to_do_dialog_view布局

  <EditText
        android:id="@+id/input2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:singleLine="true"
        android:imeOptions="actionDone"
        android:hint="Date"
        android:inputType="date"
        android:onClick="DatePickerDialog"
        />

    <EditText
        android:id="@+id/input3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:singleLine="true"

        android:hint="Time"

        android:onClick="TimePickerDialog"
        />

</LinearLayout>
</LinearLayout>

2 个答案:

答案 0 :(得分:1)

从XML中删除10+100 = 110android:onClick="TimePickerDialog"

从Java代码中设置android:onClick="DatePickerDialog",就像您已经完成的那样。

如果您执行上述操作

,则无需

错误表明您缺少两种方法。

onClickListener

public void DatePickerDialog(View v) {}

答案 1 :(得分:1)

MainActivity3.java

public class MainActivity3 extends Activity{
EditText txtDate, txtTime;
private int mYear, mMonth, mDay, mHour, mMinute;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.to_do_dialog_view);
    txtDate=(EditText)findViewById(R.id.input2);
    txtTime=(EditText)findViewById(R.id.input3);
}


public void datePick(View v){
  // Get Current Date
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);


        DatePickerDialog datePickerDialog = new DatePickerDialog(this,
                new DatePickerDialog.OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker view, int year,
                                          int monthOfYear, int dayOfMonth) {

                        txtDate.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);

                    }
                }, mYear, mMonth, mDay);
        datePickerDialog.show();
}


public void timePick(View v){
 // Get Current Time
        final Calendar c = Calendar.getInstance();
        mHour = c.get(Calendar.HOUR_OF_DAY);
        mMinute = c.get(Calendar.MINUTE);

        // Launch Time Picker Dialog
        TimePickerDialog timePickerDialog = new TimePickerDialog(this,
                new TimePickerDialog.OnTimeSetListener() {

                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay,
                                          int minute) {

                        txtTime.setText(hourOfDay + ":" + minute);
                    }
                }, mHour, mMinute, false);
        timePickerDialog.show();
}
}

更改布局中的EditText

<EditText
        android:id="@+id/input2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:singleLine="true"
        android:imeOptions="actionDone"
        android:hint="Date"
        android:inputType="date"
        android:clickable="true"
        android:onClick="datePick" />

    <EditText
        android:id="@+id/input3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:singleLine="true"
        android:hint="Time"
        android:clickable="true"
        android:onClick="timePick" />