DatePickerFragment внутри фрагмента не работает

Я пытаюсь вызвать фрагмент времени даты изнутри фрагмента. Я передаю EditText в качестве аргумента DatePickerFragment, чтобы после установки даты он мог обновить его в пользовательском интерфейсе. Однако мое приложение прерывается, прежде чем оно начнется

DateTimeFragment:

package com.example.makemyday;

import java.util.Calendar;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;

public class DateTimeFragment extends Fragment{

 private EditText editStartDate;
 private EditText editEndDate;
 @Override 
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
 { 
 View v = inflater.inflate(R.layout.fragment_datetime, container, false);
 final Calendar calendar = Calendar.getInstance();

 int yy = calendar.get(Calendar.YEAR);
 int mm = calendar.get(Calendar.MONTH)+1;
 int dd = calendar.get(Calendar.DAY_OF_MONTH);

 ImageButton startDate=(ImageButton)v.findViewById(R.id.start_date_button);
 editStartDate = (EditText)v.findViewById(R.id.startDate);
 editStartDate.setText(Integer.toString(dd)+ "/" + Integer.toString(mm) +"/"+ Integer.toString(yy));

 ImageButton endDate=(ImageButton)v.findViewById(R.id.end_date_button);
 editEndDate = (EditText)v.findViewById(R.id.endDate);
 editEndDate.setText(Integer.toString(dd)+ "/" + Integer.toString(mm) +"/"+ Integer.toString(yy));


 startDate.setOnClickListener(new View.OnClickListener() {

 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 try
 {
 DialogFragment newFragment= (DialogFragment) new DatePickerFragment(editStartDate);
 newFragment.show(getActivity().getSupportFragmentManager(), "datePicker");
 }

 catch(Exception e)
 {

 return ;
 }
 }
 });

 endDate.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
 try
 {
 DialogFragment newFragment= (DialogFragment) new DatePickerFragment(editEndDate);
 newFragment.show(getActivity().getSupportFragmentManager(), "datePicker");

 }
 catch(Exception e)
 {
 return ;
 }
 }
 });

 return v;
 }


}

DatePickerFragment

package com.example.makemyday;
import java.util.Calendar;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;

@SuppressLint("ValidFragment")
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

 /*public static interface OnCompleteListener {
 public abstract void onComplete(String date, int id);
 }

 private OnCompleteListener mListener;
 */
 private EditText edit_text;

 public DatePickerFragment(EditText edit_text) {
 this.edit_text=edit_text;
 //buttonID=id;
 }
 /*// make sure the Activity implemented it
 public void onAttach(Activity activity) {
 super.onAttach(activity);
 try {
 this.mListener = (OnCompleteListener)activity;
 }
 catch (final ClassCastException e) {
 throw new ClassCastException(activity.toString() + " must implement OnCompleteListener");
 }
 }*/

 @Override
 public Dialog onCreateDialog(Bundle savedInstanceState) {
 final Calendar calendar = Calendar.getInstance();
 int yy = calendar.get(Calendar.YEAR);
 int mm = calendar.get(Calendar.MONTH)+1;
 int dd = calendar.get(Calendar.DAY_OF_MONTH);
 return new DatePickerDialog(getActivity(), this, yy, mm, dd);
 }

 public void onDateSet(DatePicker view, int yy, int mm, int dd) {
 String date =Integer.toString(dd) + "/" + Integer.toString(mm) + "/" + Integer.toString(yy);
 edit_text.setText(date);
 //this.mListener.onComplete(date,buttonID);

 }
}

Я получаю следующую ошибку:

[2013-12-26 18:19:15 - MakeMyDay] ------------------------------
[2013-12-26 18:19:15 - MakeMyDay] Android Launch!
[2013-12-26 18:19:15 - MakeMyDay] adb is running normally.
[2013-12-26 18:19:15 - MakeMyDay] Performing com.example.makemyday.MainActivity activity launch
[2013-12-26 18:19:16 - MakeMyDay] Automatic Target Mode: launching new emulator with compatible AVD 'HappifyV2'
[2013-12-26 18:19:16 - MakeMyDay] Launching a new emulator with Virtual Device 'HappifyV2'
[2013-12-26 18:19:21 - Emulator] emulator: WARNING: Requested RAM size of 1907MB is too large for your environment, and is reduced to 1072MB.
[2013-12-26 18:19:21 - Emulator] Failed to allocate memory: 8
[2013-12-26 18:19:21 - Emulator] 
[2013-12-26 18:19:21 - Emulator] This application has requested the Runtime to terminate it in an unusual way.
[2013-12-26 18:19:21 - Emulator] Please contact the application support team for more information.

Почему этот код не работает?

1 ответ

Проблема явно из-за размера ОЗУ, а не из-за DatePicker Fragment. Вам необходимо уменьшить размер оперативной памяти эмулятора до 1 ГБ (1024 МБ) и попробовать запустить его снова.

licensed under cc by-sa 3.0 with attribution.