2014-07-25 81 views
0
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可以解決問題。但它仍然給我同樣的錯誤。任何人都可以幫忙謝謝從NonStatic Java獲取數據到靜態

+0

'populateSetDate'是靜態的。你不需要這是靜態的 – Raghunandan

+0

@Raghunandan我可以知道如何 –

+0

你看@ http://stackoverflow.com/questions/18211684/how-to-transfer-the-formatted-date-string-from-my -datepickerfragment – Raghunandan

回答

2

首先讓你的方法populateSetDate和變量非靜態。

而在片段:

public void onDateSet(DatePicker view, int year, int month, int day) 
    { 
     ((RetrieveActivity)getActivity()).populateSetDate(year, month+1,day); 
    } 

更好的解決方案是創建一個interface

interface Callbacack{ 
    public void populateSetDate(int year,int month,int day); 
} 

讓你活動實現:

public class RetrieveActivity extends Activity implements Callback 

內部片段:

public static class DatePickerFragment extends DialogFragment 
    implements DatePickerDialog.OnDateSetListener 
    { 
    Callback callback; 
    public void setCallback(Callback callback){ 
     this.callback = callback; 
    } 

而且onDateSet

public void onDateSet(DatePicker view, int year, int month, int day) 
     { 
      callback.populateSetDate(year, month+1,day); 
     }