当我将onCreateDialog方法添加到DialogFragment类时,我的应用程序崩溃了。如果我不添加该方法,则会显示包含列表的对话框。我经历了其他类似的话题,但它无法解决我的问题。拜托,我可以在这里使用一些帮助。这是我的代码。
public class SubVitalsEntryDialogFragment extends DialogFragment {
ArrayList<SubVitals> subVitalList = new ArrayList<SubVitals>();
ListView mylist;
public static SubVitalsEntryDialogFragment newInstance(ArrayList<SubVitals> sub) {
SubVitalsEntryDialogFragment f = new SubVitalsEntryDialogFragment();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putParcelableArrayList("subVitalList", sub);
f.setArguments(args);
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.subvitals_dialog_layout, null, false);
mylist = (ListView) view.findViewById(R.id.vitalEntryListView);
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
subVitalList = getArguments().getParcelableArrayList("subVitalList");
ArrayAdapter<SubVitals> adapter = new MyListAdapter(subVitalList);
mylist.setAdapter(adapter);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder alertDialogBuilder = null;
alertDialogBuilder = new AlertDialog.Builder(getActivity());
//View view = getActivity().getLayoutInflater().inflate(R.layout.subvitals_dialog_layout, null);
//alertDialogBuilder.setView(view);
alertDialogBuilder.setTitle("Enter your vital");
alertDialogBuilder.setPositiveButton("Cancel", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();
}
});
return alertDialogBuilder.create();
}
private class MyListAdapter extends ArrayAdapter<SubVitals>{
public MyListAdapter(ArrayList<SubVitals> subVitalList){
super(getActivity(), R.layout.subvitals_dialog_layout, subVitalList);
Log.d("Tag", "super okay");
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//return super.getView(position, convertView, parent);
//Making sure we've a view to work with(may have been given null
Log.d("TAG","Inside get view");
View itemView = convertView;
if (itemView == null)
itemView = LayoutInflater.from(this.getContext()).inflate(R.layout.sub_vital_template, parent, false);
String currentVital = subVitalList.get(position).getSubVitalName();
//Fill the view
TextView vitalText = (TextView) itemView.findViewById((R.id.vitalEntryListView));
vitalText.setText(currentVital);
return itemView;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return subVitalList.size();
}
}
}
logcat的:
04-08 21:48:28.032 2489-2489/com.cinch.getvitalsapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.cinch.getvitalsapp, PID: 2489
android.util.AndroidRuntimeException: requestFeature() must be called before adding content
at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:301)
at com.android.internal.app.AlertController.installContent(AlertController.java:231)
at android.app.AlertDialog.onCreate(AlertDialog.java:356)
at android.app.Dialog.dispatchOnCreate(Dialog.java:373)
at android.app.Dialog.show(Dialog.java:274)
at android.app.DialogFragment.onStart(DialogFragment.java:490)
at android.app.Fragment.performStart(Fragment.java:2077)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:922)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.app.BackStackRecord.run(BackStackRecord.java:833)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:447)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
____________ EDIT __________________
public class SubVitalsEntryDialogFragment extends DialogFragment {
ArrayList<SubVitals> subVitalList = new ArrayList<SubVitals>();
ListView mylist;
public static SubVitalsEntryDialogFragment newInstance(ArrayList<SubVitals> sub) {
SubVitalsEntryDialogFragment f = new SubVitalsEntryDialogFragment();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putParcelableArrayList("subVitalList", sub);
f.setArguments(args);
return f;
}
@Override
public void onViewCreated(final View view, Bundle savedInstanceState){
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.subvitals_dialog_layout, null, false);
mylist = (ListView) view.findViewById(R.id.vitalEntryListView);
//getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
//getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
subVitalList = getArguments().getParcelableArrayList("subVitalList");
ArrayAdapter<SubVitals> adapter = new MyListAdapter(subVitalList);
mylist.setAdapter(adapter);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder alertDialogBuilder = null;
alertDialogBuilder = new AlertDialog.Builder(getActivity());
//View view = getActivity().getLayoutInflater().inflate(R.layout.subvitals_dialog_layout, null);
//alertDialogBuilder.setView(view);
alertDialogBuilder.setTitle("Enter your vital");
alertDialogBuilder.setPositiveButton("Cancel", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();
}
});
return alertDialogBuilder.create();
}
private class MyListAdapter extends ArrayAdapter<SubVitals>{
public MyListAdapter(ArrayList<SubVitals> subVitalList){
super(getActivity(), R.layout.subvitals_dialog_layout, subVitalList);
Log.d("Tag", "super okay");
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//return super.getView(position, convertView, parent);
//Making sure we've a view to work with(may have been given null
Log.d("TAG","Inside get view");
View itemView = convertView;
if (itemView == null)
itemView = LayoutInflater.from(this.getContext()).inflate(R.layout.sub_vital_template, parent, false);
String currentVital = subVitalList.get(position).getSubVitalName();
//Fill the view
TextView vitalText = (TextView) itemView.findViewById((R.id.vitalEntryListView));
vitalText.setText(currentVital);
return itemView;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return subVitalList.size();
}
}
}
04-08 21:48:28.032 2489-2489/com.cinch.getvitalsapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.cinch.getvitalsapp, PID: 2489
android.util.AndroidRuntimeException: requestFeature() must be called before adding content
at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:301)
at com.android.internal.app.AlertController.installContent(AlertController.java:231)
at android.app.AlertDialog.onCreate(AlertDialog.java:356)
at android.app.Dialog.dispatchOnCreate(Dialog.java:373)
at android.app.Dialog.show(Dialog.java:274)
at android.app.DialogFragment.onStart(DialogFragment.java:490)
at android.app.Fragment.performStart(Fragment.java:2077)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:922)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.app.BackStackRecord.run(BackStackRecord.java:833)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:447)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
04-08 21:48:34.235 2489-2489/com.cinch.getvitalsapp I/Process﹕ Sending signal. PID: 2489 SIG: 9
04-08 21:48:34.511 2517-2532/com.cinch.getvitalsapp D/OpenGLRenderer﹕ Render dirty regions requested: true
04-08 21:48:34.523 2517-2517/com.cinch.getvitalsapp D/﹕ HostConnection::get() New Host Connection established 0xa6c56580, tid 2517
04-08 21:48:34.565 2517-2517/com.cinch.getvitalsapp D/Atlas﹕ Validating map...
04-08 21:48:34.599 2517-2529/com.cinch.getvitalsapp I/art﹕ Background sticky concurrent mark sweep GC freed 3210(243KB) AllocSpace objects, 0(0B) LOS objects, 16% free, 952KB/1135KB, paused 39.970ms total 95.451ms
04-08 21:48:34.629 2517-2532/com.cinch.getvitalsapp D/﹕ HostConnection::get() New Host Connection established 0xa6c56860, tid 2532
04-08 21:48:34.644 2517-2532/com.cinch.getvitalsapp I/OpenGLRenderer﹕ Initialized EGL, version 1.4
04-08 21:48:34.674 2517-2532/com.cinch.getvitalsapp D/OpenGLRenderer﹕ Enabling debug mode 0
04-08 21:48:34.692 2517-2532/com.cinch.getvitalsapp W/EGL_emulation﹕ eglSurfaceAttrib not implemented
04-08 21:48:34.692 2517-2532/com.cinch.getvitalsapp W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xae0ea540, error=EGL_SUCCESS
04-08 21:48:35.009 2517-2517/com.cinch.getvitalsapp W/IInputConnectionWrapper﹕ showStatusIcon on inactive InputConnection
答案 0 :(得分:1)
替换
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
与
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
并在onViewCreated()
中执行此操作,而不是onCreateView()
:
@Override
public void onViewCreated(final View view, Bundle savedInstanceState){
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
}