我试图从片段内部调用日期时间片段。我将EditText作为参数传递给DatePickerFragment,这样一旦设置了日期,它就可以在UI中更新它。但是,我的应用程序在开始之前就会中止
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's support team for more information.
为什么这段代码不起作用?
答案 0 :(得分:0)
问题显然是由于RAM的大小,而不是因为DatePicker Fragment
。您需要将模拟器RAM的大小减小到1GB(1024MB)并尝试再次运行。