Java 通过按钮激活的日期选择器选择日期后,更改按钮上的文本

Java 通过按钮激活的日期选择器选择日期后,更改按钮上的文本,java,android,android-layout,android-fragments,android-button,Java,Android,Android Layout,Android Fragments,Android Button,我希望日期选择器选择的日期显示在调用日期选择器的按钮上(通过片段类)。我试图通过在类中创建一个方法来实现这一点,该方法定义了单击按钮后要执行的操作。该方法通过setText()更改文本。fragment类通过onDateSet()按钮调用此方法 现在,当我通过日期选择器设置日期时,应用程序崩溃了 代码:1。 这里调用另一个类中的方法,该方法将更改按钮的文本,并传递日、月、年数据 代码2。 此方法用于更改按钮上的文本 CatLog: 我只发布了红色部分 public void changebutt

我希望日期选择器选择的日期显示在调用日期选择器的按钮上(通过片段类)。我试图通过在类中创建一个方法来实现这一点,该方法定义了单击按钮后要执行的操作。该方法通过setText()更改文本。fragment类通过onDateSet()按钮调用此方法

现在,当我通过日期选择器设置日期时,应用程序崩溃了

代码:1。

这里调用另一个类中的方法,该方法将更改按钮的文本,并传递日、月、年数据

代码2。

此方法用于更改按钮上的文本

CatLog:

我只发布了红色部分

public void changebuttontext (int year, int month, int day) {
    Button date_button = (Button)findViewById(R.id.datebutton);     
    date_button.setText(year + "/" + month + "/" + day);
}

应该从Activity类调用此代码。如果AddProject是一个外部类程序,它将崩溃。

在查看数据后,我认为问题是,当活动不在焦点时,您正在尝试查找dviewbyd。 作为一种解决方案,您可以尝试类似的方法

public void changebuttontext (int year, int month, int day) {
   selectedDate = year + "/" + month + "/" + day;
}
此处所选日期是一个字段变量。现在,在onWindowFocusChange()或onResume中,无论哪一个工作都可以放置

 Button date_button = (Button)findViewById(R.id.datebutton);     
    date_button.setText(selectedDate);
部分

可能要检查此项是否超出范围。

显示了什么错误消息?@Hardik4560嗯,应用程序只是崩溃了。你想让我发布日志吗?是的……想查看异常以调试原因。@Hardik4560发布!请看,我不能!onDataSet是片段类的一部分<代码>公共类DatePickerFragment扩展DialogFragment实现DatePickerDialog.OnDateSetListener,此类类无法识别findViewById。无法工作。同样的问题仍然存在。我添加了
Button date\u Button=(Button)findviewbyd(R.id.datebutton);日期按钮。设置文本(选择日期)
到同一个类,在具有
changebuttonext()
onWindowFocusChange()下。如果我使用onResume,当我进入包含该按钮的活动时,应用程序就会崩溃。我再次发布了问题,但方法不同。我知道答案了。是链接。不过,感谢您抽出时间:)得到了答案……这不是一个简单的解决方案吗……)
public void changebuttontext (int year, int month, int day) {
    Button date_button = (Button)findViewById(R.id.datebutton);     
    date_button.setText(year + "/" + month + "/" + day);
}
public void changebuttontext (int year, int month, int day) {
   selectedDate = year + "/" + month + "/" + day;
}
 Button date_button = (Button)findViewById(R.id.datebutton);     
    date_button.setText(selectedDate);