0

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

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

代码:1。

  public void onDateSet(DatePicker view, int year, int month, int day) {
      Addproject changetxt = new Addproject();
      changetxt.changebuttontext(year, month, day);
      //Doing something with the date chosen
    }

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

代码 2。

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

此方法应该更改按钮上的文本。

目录日志:

12-24 22:42:25.608: E/AndroidRuntime(3478):   at     
com.kk.project.DatePickerFragment.onDateSet(DatePickerFragment.java:33)
12-24 22:42:25.608: E/AndroidRuntime(3478):     at     
android.app.DatePickerDialog.onClick(DatePickerDialog.java:111)
12-24 22:42:25.608: E/AndroidRuntime(3478):     at  
com.android.internal.app.AlertController$ButtonHandler.handleMessage
(AlertController.java:166)
12-24 22:42:25.608: E/AndroidRuntime(3478):     at  
android.os.Handler.dispatchMessage(Handler.java:99)
12-24 22:42:25.608: E/AndroidRuntime(3478):     at      
android.os.Looper.loop(Looper.java:137)
12-24 22:42:25.608: E/AndroidRuntime(3478):     at     
android.app.ActivityThread.main(ActivityThread.java:4424)
12-24 22:42:25.608: E/AndroidRuntime(3478):     at  
java.lang.reflect.Method.invokeNative(Native Method)
12-24 22:42:25.608: E/AndroidRuntime(3478):     at 
java.lang.reflect.Method.invoke(Method.java:511)
12-24 22:42:25.608: E/AndroidRuntime(3478):     at  
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-24 22:42:25.608: E/AndroidRuntime(3478):     at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-24 22:42:25.608: E/AndroidRuntime(3478):     at     
dalvik.system.NativeStart.main(Native Method)

我只贴了红色的部分

4

2 回答 2

0

The problem I think after looking at your data is, you are trying to findViewById when the activity is not in focus. As a solution you can try something like this.

public void changebuttontext (int year, int month, int day) {
   selectedDate = year + "/" + month + "/" + day;
}

here selected date is a field variable. Now in onWindowFocusChange() or onResume whichever works place the

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

part.

might want to check this for the excetion. Illegal argument exception in android dialog

于 2012-12-24T17:59:12.770 回答
0
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 是外部类程序将崩溃。

于 2012-12-24T17:42:33.240 回答