非静态方法findviewbyid int不能从静态上下文中引用

时间:2018-04-19 10:35:58

标签: android android-datepicker

我正在使用(下面的链接)日期选择器从用户中选择日期并在选择后我想更新我的TextBox(编辑文本)但是它给了我"非静态方法findviewbyid int不能从静态背景"错误 任何机构都可以建议我采取其他方式做同样的事情或这种方法中的任何错误吗?

https://developer.android.com/guide/topics/ui/controls/pickers.html

public void onDateSet(DatePicker view, int year, int month, int day) {
        // Do something with the date chosen by the user
        String myFormat = "yyyy-MM-dd";
        SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
        ((EditText) findViewById(R.id.ev_Date)).setText(sdf.format(usercalendar.getTime()));
    }

这就是我的整个代码

public class CreateReport extends AppCompatActivity {
public EditText EventDate;
private static Calendar usercalendar;
private String Lat, Long;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_create_report);
//        final ActionBar ab = getSupportActionBar();
//        ab.setTitle("Create Reoprt");
    Bundle bundle = getIntent().getExtras();

    EventDate = (EditText) findViewById(R.id.ev_Date);

    if (bundle != null) {
        Lat = bundle.getString("lat");
        Toast.makeText(getContext(), "Latitude" + Lat, Toast.LENGTH_LONG).show();
        Long = bundle.getString("Long");
    }
}
public void showDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getSupportFragmentManager(), "datePicker");
}
public static class DatePickerFragment extends DialogFragment
        implements DatePickerDialog.OnDateSetListener {

    @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);
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        // Do something with the date chosen by the user
        String myFormat = "yyyy-MM-dd";
        SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
//            ((EditText) findViewById(R.id.ev_Date)).setText(sdf.format(usercalendar.getTime()));
//            EventDate.setText(sdf.format(usercalendar.getTime())); 
//Unaccesable
    }
}

2 个答案:

答案 0 :(得分:3)

您无法在findViewById方法

中执行 onDateSet()

您需要在findViewById)方法中执行 onCreate(

示例示例

public class MainActivity extends AppCompatActivity {

    // decalre your EditText Global
    EditText edtDate;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // do findViewById inside oncraeate
        edtDate = findViewById(R.id.ev_Date);

    }

}

现在在onDateSet()内使用

    // when ever u want to set text in EditText edtDate just use like this
    edtDate.setText(sdf.format(usercalendar.getTime()));

答案 1 :(得分:0)

在调用方法之前,您尚未创建对象,因此非静态方法尚不存在。