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

问题描述 投票:0回答:3

我正在使用(下面的链接)日期选择器从用户中选择日期,选择后我想更新我的文本框(编辑文本),但它给了我“非静态方法 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
    }
}
android android-datepicker
3个回答
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()));

0
投票

在调用方法之前你还没有创建对象,所以非静态方法还不存在。


0
投票

您可以使用类型是(WebView)和(getView)

WebView mywebview = (WebView) getView().findViewById(R.id.webViewName);
© www.soinside.com 2019 - 2024. All rights reserved.