を使用したとき、私はフラグメントを使用する場合、私が完全なコードを貼り付け、それはここで日付ピッカーは私がフラグメント
Error:(63, 61) error: incompatible types: MainActivity cannot be converted to Context
のようなエラーを発生させる活動に取り組んでいるDatePicker
を設定する機能していません。
MainActivity.Java
package com.example.sachin.datepicker;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.icu.text.SimpleDateFormat;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.EditText;
import java.util.Calendar;
import java.util.Locale;
public class MainActivity extends Fragment implements View.OnClickListener {
private EditText fromDateEtxt;
private EditText toDateEtxt;
private EditText toDateEtxt1;
private DatePickerDialog fromDatePickerDialog;
private DatePickerDialog toDatePickerDialog;
private DatePickerDialog toDatePickerDialog1;
private SimpleDateFormat dateFormatter;
View view;
@TargetApi(Build.VERSION_CODES.N)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_add_wod, container, false);
dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);
findViewsById();
setDateTimeField();
return view;
}
private void findViewsById() {
fromDateEtxt = (EditText) view.findViewById(R.id.openingdate);
fromDateEtxt.setInputType(InputType.TYPE_NULL);
fromDateEtxt.requestFocus();
toDateEtxt = (EditText) view.findViewById(R.id.Birthdate);
toDateEtxt.setInputType(InputType.TYPE_NULL);
toDateEtxt.requestFocus();
toDateEtxt1 = (EditText) view.findViewById(R.id.Anniversary);
toDateEtxt1.setInputType(InputType.TYPE_NULL);
}
private void setDateTimeField() {
fromDateEtxt.setOnClickListener(this);
toDateEtxt.setOnClickListener(this);
toDateEtxt1.setOnClickListener(this);
Calendar newCalendar = Calendar.getInstance();
fromDatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
fromDateEtxt.setText(dateFormatter.format(newDate.getTime()));
}
},newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
toDatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@TargetApi(Build.VERSION_CODES.N)
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
toDateEtxt.setText(dateFormatter.format(newDate.getTime()));
}
},newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
toDatePickerDialog1 = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@TargetApi(Build.VERSION_CODES.N)
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
toDateEtxt1.setText(dateFormatter.format(newDate.getTime()));
}
},newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
}
@Override
public void onClick(View view) {
if(view == fromDateEtxt) {
fromDatePickerDialog.show();
} else if(view == toDateEtxt) {
toDatePickerDialog.show();
} else if(view == toDateEtxt1) {
toDatePickerDialog1.show();
}
}
}
このコードは動作しません: -
fromDatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
fromDateEtxt.setText(dateFormatter.format(newDate.getTime()));
}
},newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
私はそれを試してみましたが、それは何度も何度も同じエラーが発生している。..