【问题标题】:Updating TextView from DatePicker从 DatePicker 更新 TextView
【发布时间】:2016-03-14 19:32:35
【问题描述】:

我正在尝试从 Datepicker 中更新 TextView,但是我收到了错误

“不能从静态上下文引用非静态方法(updateDateTextView(long))”

这是我的 DatePickerFragment.java

public 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) {
        Calendar calendar = new GregorianCalendar(year, month, day);
        long timestamp = TimeUnit.MILLISECONDS.toSeconds(calendar.getTimeInMillis());
        AddItem.updateDateTextView(timestamp);
    }
}

这是我在 AddItem.java 中的函数,它是一个 Activity。

      public  class AddItem extends AppCompatActivity {

    Context ctx = this;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_item);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

    }
    public void showDatePickerDialog(View v) {
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getFragmentManager(), "datePicker");
    }

    public void updateDateTextView(long timestamp){
        TextView textViewToChange = (TextView) findViewById(R.id.openDate);
        Calendar cal = Calendar.getInstance(Locale.ENGLISH);
        cal.setTimeInMillis(timestamp);
        String date = DateFormat.format("dd-MM-yyyy", cal).toString();
        textViewToChange.setText(date);
    }

}

【问题讨论】:

  • 您正在从静态方法调用方法 updateDateTextView...请发布 MCVE....
  • 我在原帖中添加了 MCVE

标签: java android function static datepicker


【解决方案1】:

正如他们所说,您不能像静态方法那样调用非静态方法。试试这个:

在您的 Activity 中实现 DatePickerDialog.OnDateSetListener 和逻辑:

public class AddItem extends AppCompatActivity implements DatePickerDialog.OnDateSetListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    showDatePickerDialog();
}

public void showDatePickerDialog() {
    DatePickerFragment newFragment = new DatePickerFragment();
    newFragment.init(this, this);
    newFragment.show(getSupportFragmentManager(), "date_picker");
}

public void updateDateTextView(long timestamp){
    TextView textViewToChange = (TextView) findViewById(R.id.openDate);
    Calendar cal = Calendar.getInstance(Locale.ENGLISH);
    cal.setTimeInMillis(timestamp);
    String date = DateFormat.format("dd-MM-yyyy", cal).toString();
    textViewToChange.setText(date);
}

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

    Calendar calendar = new GregorianCalendar(year, monthOfYear, dayOfMonth);
        long timestamp = TimeUnit.MILLISECONDS.toSeconds(calendar.getTimeInMillis());

        updateDateTextView(timestamp);
}

并用监听器初始化 DatePicker,类应该是这样的:

public static class DatePickerFragment extends DialogFragment {

    DatePickerDialog.OnDateSetListener listener;
    Context context;

    public void init(Context context, DatePickerDialog.OnDateSetListener listener) {
        this.listener = listener;
        this.context = context;
    }

    @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(context, listener, year, month, day);
    }

}

我试过了,效果很好。

【讨论】:

    【解决方案2】:

    非静态方法不能与静态类一起使用 尝试改变

    public void updateDateTextView(long timestamp){ ..... }

    public static void updateDateTextView(long timestamp){
    ...}
    

    【讨论】:

    • 这样做会导致带有 findViewByID 的 updateDateTextView() 函数内部出错,错误是一样的 - “无法从静态上下文引用非静态方法 (findViewById(int))”跨度>
    • 您是否尝试访问日期选择器标题?
    • 我正在尝试使用 DatePicker 的值更新 AddItem 活动中的 TextView
    【解决方案3】:

    你正在这样做:

    public void onDateSet(DatePicker view, int year, int month, int day) {
            Calendar calendar = new GregorianCalendar(year, month, day);
            long timestamp = TimeUnit.MILLISECONDS.toSeconds(calendar.getTimeInMillis());
            AddItem.updateDateTextView(timestamp); ///Here is the error
        }
    

    错误是 AddITem.updateDateTextView 错误。 updateDateTextView 方法不是静态的。

    您需要使用 Intent 启动该 Activity,并且在生命周期中的某处调用方法 updateDateTextView

    修改方法并使其成为静态(但这不是开发人员的好主意)

    public static void onDateSet(DatePicker view, int year, int month, int day)
    

    【讨论】:

    • 我怎么不能从静态方法中调用它?
    【解决方案4】:

    也可以在 DatePickerFragment 类中定义一个interface,如下面的代码所示。

    public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
    
    DateChooserListenerHandler dateChooserListenerHandler;
    @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);
    }
    @Override
    public void onDateSet(DatePicker datePicker, int year, int month, int day) {
        Calendar calendar = new GregorianCalendar(year, month, day);
        long timestamp = TimeUnit.MILLISECONDS.toSeconds(calendar.getTimeInMillis());
        //Note this is where the updateTextView method in the interface is invoked from
        ((DateChooserListenerHandler) getActivity()).updateTextView(timestamp);
    }
    
    //Define an interface with a method that takes long, it will be overridden by classes that implement the interface
    public interface DateChooserListenerHandler{
    
        public void updateTextView(long s);
    }
    

    }

    然后 AddItem 活动类可以实现如下代码中的接口。

    public class AddItem extends AppCompatActivity implements DatePickerFragment.DateChooserListenerHandler {
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
    }
    
    public void showDatePickerDialog(View v) {
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getSupportFragmentManager(), "timePicker");
    }
    
    //The overridden method from the interface defined in DatePickerFragment class
    @Override
    public void updateTextView(long s) {
        updateDateTextView(s);
    }
    
    public void updateDateTextView(long timestamp){
        TextView textViewToChange = (TextView) findViewById(R.id.textView);
        Calendar cal = Calendar.getInstance(Locale.ENGLISH);
        cal.setTimeInMillis(timestamp);
        String date = DateFormat.format("dd-MM-yyyy", cal).toString();
        textViewToChange.setText(date);
    }
    

    }

    这对我来说很好,但是我没有检查它对应用程序的性能影响,但我发现它更容易。 如果您只想直接从 DatePickerFragment 更新 TextView,那么您可以像下面这样进行。

     @Override
    public void onDateSet(DatePicker datePicker, int year, int month, int day) {
        Calendar calendar = new GregorianCalendar(year, month, day);
        long timestamp = TimeUnit.MILLISECONDS.toSeconds(calendar.getTimeInMillis());
        TextView textView = (TextView) getActivity().findViewById(R.id.textView);
        textView.setText(timestamp + "");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多