Hallo,
ich versuche gerade ein eigenes DialogFragment zu schreiben. Es funktionert auch soweit, aber ich würde gerne den ressource int für das Layout sowie den Titel per Bundle übergeben, aber ich scheitere an einer NullPointerException:
Mein DialogFragment:
Der Aufruf:
Ganz offensichtlich komme ich über das Bundle savedInstanceState nicht an das per dialog.setArguments(args); übergebene Bundle heran. Wieso?
ich versuche gerade ein eigenes DialogFragment zu schreiben. Es funktionert auch soweit, aber ich würde gerne den ressource int für das Layout sowie den Titel per Bundle übergeben, aber ich scheitere an einer NullPointerException:
Code:
java.lang.NullPointerException at SingleCoiceListDialogFragment.onCreateDialog(SingleCoiceListDialogFragment.java:71)
Mein DialogFragment:
Java:
public class SingleCoiceListDialogFragment extends DialogFragment {
SimpleCursorAdapter mAdapter;
/* The activity that creates an instance of this dialog fragment must
* implement this interface in order to receive event callbacks.
* Each method passes the DialogFragment in case the host needs to query it. */
public interface SingleChoiceListener {
public void onItemSelected(DialogFragment dialog, int which);
}
// Use this instance of the interface to deliver action events
SingleChoiceListener mSingleChoiceListener;
// Override the Fragment.onAttach() method to instantiate the listener
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// Verify that the host activity implements the callback interface
try {
// Instantiate the listener so we can send events to the host
mSingleChoiceListener = (SingleChoiceListener) activity;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(activity.toString()
+ " must implement NoticeDialogListener");
}
}
/**
*
* @param context
* @return
*/
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater layoutInflater = getActivity().getLayoutInflater();
DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
mSingleChoiceListener.onItemSelected(SingleCoiceListDialogFragment.this, which);
}
};
builder.setTitle("Titel");
builder.setTitle(savedInstanceState.getString("title")); // [Zeile 71]
builder.setView(layoutInflater.inflate(R.layout.layout, null));
builder.setAdapter(mAdapter, onClickListener);
return builder.create();
}
public void setAdapter(SimpleCursorAdapter adapter) {
mAdapter = adapter;
}
}
Java:
SingleCoiceListDialogFragment dialog = new SingleCoiceListDialogFragment();
Bundle args = new Bundle();
args.putString("title", "Zutat auswählen");
args.putInt("layout", R.layout.layout);
dialog.setArguments(args);
//..
dialog.show(getFragmentManager(), "NoticeDialogFragment");