【发布时间】:2014-07-25 05:41:26
【问题描述】:
public class RetrieveActivity extends Activity {
static String dateData;
static int choice ;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_retrieve);
}
public void showDatePickerDialogFrom(View v)
{
choice=1;
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
}
public void showDatePickerDialogTo(View v)
{
choice=2;
String dateSelected;
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
}
public static void populateSetDate(int year,int month,int day)
{
dateData = ""+day+"/"+month+"/"+year;
if(choice==1)
{
EditText dateFrom=(EditText)findViewById(R.id.dateTo); <------ error over here on the static to non static stuff.
dateFrom.setText(dateData);
}
else
{
EditText dateTo=(EditText)findViewById(R.id.dateFrom1);<--- here also the same.
dateTo.setText(dateData);
}
}
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)
{
populateSetDate(year, month+1,day);
}
我试图将变量更改为静态,这样它就不会给我错误。然后我在网上搜索了如何解决这个问题。有些人确实告诉我添加 getActivity 将解决问题。但它仍然给我同样的错误。任何人都可以提供帮助。谢谢
【问题讨论】:
-
populateSetDate是静态的。你不需要它是静态的 -
@Raghunandan 我可以知道如何
-
为什么 populateSetDate 还是静态的?声明你的 EditText 全局并将你的 findviewbyid 放在 onCreate 方法中
-
@Raghunandan 非常感谢~我不知道一开始就不是静态的。