【问题标题】:Non static method findviewbyid int cannot be referenced from a static context不能从静态上下文引用非静态方法 findviewbyid int
【发布时间】:2018-04-19 10:35:57
【问题描述】:

我正在使用(下面的链接)日期选择器从用户那里选择日期,选择后我想更新我的 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
    }
}

【问题讨论】:

  • 你的意思是编译错误。

标签: android android-datepicker


【解决方案1】:

你不能在onDateSet()方法里面做findViewById

你需要在onCreate()方法中做findViewById

示例

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()));

【讨论】:

  • 我可以把它放在onCreate()中但是我想在用户选择日期后更新它
  • 它已经是全球性的,并且 edtDate 已经是那样了......我想做的是在用户选择特定日期后更新这个编辑文本
  • @riasat 您无需再次调用 findViewById,只需使用该全局变量即可。
  • @riasat 分享您的整个活动代码 qith 问题以及您的选择器的代码
  • 这与findViewById() 调用具有相同的问题。您无法从静态嵌套类访问 MainActivity 的任何实例成员(变量或方法)。
【解决方案2】:

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

【讨论】:

  • 我们在说哪个对象?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-07
  • 2020-11-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多