1
我:空指針異常旋轉裝置(SherlockDialogFragment)
public class LocationSelectionFragment extends SherlockDialogFragment implements
LocationImageChangeListener, DeleteActionListener {
// some code...
// ...
public void onLocationImageRemove(int position) {
Log.message("Enter");
Log.variable("position", String.valueOf(position));
mPosition = position;
ConfirmDeleteFragment deleteFragment = ConfirmDeleteFragment.NewInstance(mAdapter.getItem(position).locationName, this);
deleteFragment.show(getFragmentManager(), Constants.DELETE_DIALOG);
}
private void deleteRecord() {
LocationRecord record = mAdapter.getItem(mPosition);
mDbAdapter.deleteLocation(record._id);
mAdapter.remove(record);
mAdapter.notifyDataSetChanged();
}
@Override
public void onDeleteLocation() {
Log.message("Enter");
dismiss();
//deleteRecord();
}
我ConfirmDeleteFragment類:
爲空指針異常/**
* Implements delete confirmation dialog
*/
public class ConfirmDeleteFragment extends SherlockDialogFragment {
/**
* Interface for delete action
*/
public interface DeleteActionListener{
/**
* Listener for handling delete location
*/
public void onDeleteLocation();
}
private static DeleteActionListener mCallback;
private String mLocationName;
static public ConfirmDeleteFragment NewInstance(String locationName, DeleteActionListener callback) {
ConfirmDeleteFragment deleteDialog = new ConfirmDeleteFragment();
deleteDialog.mLocationName = locationName;
ConfirmDeleteFragment.mCallback = callback;
return deleteDialog;
}
public ConfirmDeleteFragment() {
super();
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Log.message("Enter");
if (savedInstanceState != null) {
mLocationName = savedInstanceState.getString(Constants.DELETE_LOCATION_NAME);
}
String message = String.format(Locale.US, getString(R.string.sunset_deleteConfirm_message), mLocationName);
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
// Setting Dialog Title
alertDialog.setTitle(R.string.sunset_deleteConfirm_title);
// Setting Dialog Message
alertDialog.setMessage(message);
// On pressing Delete button
alertDialog.setPositiveButton(R.string.sunset_deleteConfirm_positive,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
mCallback.onDeleteLocation();
}
});
// on pressing cancel button
alertDialog.setNegativeButton(R.string.sunset_deleteConfirm_negative,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
return alertDialog.create();
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putString(Constants.DELETE_LOCATION_NAME, mLocationName);
}
}
步驟:
- 用戶打開的對話框LocationSelectionFragment。
- 用戶單擊刪除圖像並打開ConfirmDeleteFragment。
用戶旋轉設備並確認操作。結果 - 空指針異常:
10-26 10:42:48.747: E/AndroidRuntime(3657): java.lang.NullPointerException 10-26 10:42:48.747: E/AndroidRuntime(3657): at android.support.v4.app.DialogFragment.dismissInternal(DialogFragment.java:184) 10-26 10:42:48.747: E/AndroidRuntime(3657): at android.support.v4.app.DialogFragment.dismiss(DialogFragment.java:155) 10-26 10:42:48.747: E/AndroidRuntime(3657): at ru.neverdark.phototools.fragments.LocationSelectionFragment.onDeleteLocation(LocationSelectionFragment.java:231) 10-26 10:42:48.747: E/AndroidRuntime(3657): at ru.neverdark.phototools.fragments.ConfirmDeleteFragment$1.onClick(ConfirmDeleteFragment.java:83) 10-26 10:42:48.747: E/AndroidRuntime(3657): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166) 10-26 10:42:48.747: E/AndroidRuntime(3657): at android.os.Handler.dispatchMessage(Handler.java:99) 10-26 10:42:48.747: E/AndroidRuntime(3657): at android.os.Looper.loop(Looper.java:137) 10-26 10:42:48.747: E/AndroidRuntime(3657): at android.app.ActivityThread.main(ActivityThread.java:5103) 10-26 10:42:48.747: E/AndroidRuntime(3657): at java.lang.reflect.Method.invokeNative(Native Method) 10-26 10:42:48.747: E/AndroidRuntime(3657): at java.lang.reflect.Method.invoke(Method.java:525) 10-26 10:42:48.747: E/AndroidRuntime(3657): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 10-26 10:42:48.747: E/AndroidRuntime(3657): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 10-26 10:42:48.747: E/AndroidRuntime(3657): at dalvik.system.NativeStart.main(Native Method)
如果沒有旋轉裝置 - 做工精細。 在日誌中我可以看到什麼時候onResume方法在兩種情況下都調用了LocationSelectionFragment(帶有旋轉和無旋轉),但是在旋轉的情況下 - 當我調用Dismiss爲LocationSelectionFragment時使用。爲什麼?
不錯。請不要忘記將您的答案標記爲已接受(等待時間過後)。謝謝! – davidcesarino