2016-01-02 18 views
1

我想在點擊圖像按鈕時彈出日期選擇器對話框,然後用戶選擇日期將顯示在編輯文本字段中。我遵循Android開發人員的指導方針,通過DialogFragment使用,併成功彈出日期選擇器對話框。用戶可以選擇對話框中的日期,但當用戶按下設置按鈕時會崩潰。Edittext不可能從日期選擇器得到用戶解決的日期

事實上,我懷疑這是onDateSet中LayoutInflater中getContext()的問題,因爲有一句話說「調用所需的API級別23(當前API:12)」,但我無法想象它瞭解如何解決它。

這是Android開發人員推薦調用getContext()獲取活動上下文,並不確定問題發生的原因。

我試圖使用@TargetApi,但它表示這隻用於該方法。

有人能讓我知道我的代碼中的問題在哪裏嗎?

的邏輯說: java.lang.NoSuchMethodError:com.test.testapplication.DatePickerFragment.getContext

DatePickerFragment

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { 


final Calendar calendar = Calendar.getInstance(); 
int YEAR; 
int MONTH; 
int DAY; 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 

    YEAR = calendar.get(Calendar.YEAR); 
    MONTH = calendar.get(Calendar.MONTH); 
    DAY = calendar.get(Calendar.DAY_OF_MONTH); 

    return new DatePickerDialog(getActivity(), this, YEAR, MONTH, DAY); 
} 

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

    YEAR = year; 
    MONTH = monthOfYear; 
    DAY = dayOfMonth; 

    String dateFormat = "MM/dd/yy"; 
    SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, Locale.CHINESE); 

    View fragment = LayoutInflater.from(getContext()).inflate(R.layout.activity_datepickerdialogfragment, null); 
    EditText editText = (EditText) fragment.findViewById(R.id.edittext); 
    editText.setText(sdf.format(calendar.getTime())); 
} 
} 

MainActivity

public class DPDFActivity extends FragmentActivity { 

EditText editText; 
ImageButton btn; 
DialogFragment datePickerFragment; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_datepickerdialogfragment); 

    editText = (EditText) findViewById(R.id.edittext); 
    btn = (ImageButton) findViewById(R.id.btn); 

    btn.setOnClickListener(new showDateDialog()); 
} 

public class showDateDialog implements View.OnClickListener { 
    @Override 
    public void onClick(View v) { 
     datePickerFragment = new DatePickerFragment(); 
     datePickerFragment.show(getFragmentManager(), "DatePicker"); 
    } 
} 
} 

回答

1

創建日期選擇器裏面你班活動靜態爲例如我做了這樣的事情在我的片段。

public static class DatePickerFragment extends DialogFragment 
     implements DatePickerDialog.OnDateSetListener { 
    int currentYear; 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     // Use the current date as the default date in the picker 
     final Calendar c = Calendar.getInstance(); 
     currentYear = 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, currentYear, month, day); 
    } 

    public void onDateSet(DatePicker view, int year, int month, int day) { 
     if(currentYear-year >=15) { 
      CharSequence strDate = null; 
      Calendar chosenDate = Calendar.getInstance(); 
      chosenDate.setTimeInMillis(0); 
      chosenDate.set(year, month, day); 
      Date date = chosenDate.getTime(); 
      SimpleDateFormat dateFormatter = new SimpleDateFormat(
        "yyyy-MM-dd"); 
      strDate = dateFormatter.format(date); 

      btnDOB.setText(strDate); 
     } 
     else 
     { 
      Toast.makeText(view.getContext(),"Selected Year should be mininmum 15 year ago from current year. Please try again.",Toast.LENGTH_LONG).show(); 
     } 
    } 
} 

在您的活動定義的EditText任何methon(職業等級)之外

private static EditText meditText; 

使用findById()在活動方法的OnCreate()只能在稍後更新。

+0

您的建議工作,使其更容易理解,並已執行到我的程序。我只關心我們是否可以按照Android開發者的建議來做到這一點。 – Troy

0

而不是嘗試調用getContext()。你應該可以在這裏使用getActivity()。

View fragment = LayoutInflater.from(getActivity()).inflate(R.layout.activity_datepickerdialogfragment, null); 
+0

getActivity()可以在沒有崩潰的情況下使用,但是日期設置後日期不會顯示在編輯文本上。你知道哪裏出了問題嗎?順便說一下,你知道爲什麼android開發人員建議使用getContext(),如果它不起作用? – Troy

+0

here,e​​ditText.setText(sdf.format(calendar.getTime())); calendar.getTime()返回一個Date對象而不是一個字符串。嘗試使用editText.setText(sdf.format(calendar.getTime()。getTime())),我知道它的醜陋。 –

+0

也不知道爲什麼android開發人員建議使用getContext()。 –

相關問題