【发布时间】:2019-03-11 23:31:22
【问题描述】:
我正在创建一个应用程序,允许用户从图库中上传图像或使用设备相机拍照。单击适当的按钮时,会出现一个对话框,用户可以选择“拍照”或“选择图像”。这一切都很好,问题是当我在屏幕上显示对话框时旋转设备然后选择一个选项应用程序崩溃,IllegalStateException 表示我的 Fragment 未附加到 Activity。我已经包含了我用来与目标片段进行通信的 Dialog:
创建 DialogFragment:
public void onClick() {
// Display a Dialog for choice between pick/ take picture
ListDialogFragment dialogFragment = ListDialogFragment.newInstance(R.string.take_photo, R.array.add_picture_options_array);
dialogFragment.setTargetFragment(this, 0);
dialogFragment.show(getFragmentManager(), "Moo");
}
DialogFragment 类:
public class ListDialogFragment extends DialogFragment {
private static final String TITLE_RESOURCE_ID = "title_resource_id";
private static final String ARRAY_RESOURCE_ID = "array_resource_id";
private DialogItemSelectedListener listener;
public interface DialogItemSelectedListener {
void onTakePhotoSelected();
void onSelectImageSelected();
}
public ListDialogFragment() {
// Default empty constructor
}
public static ListDialogFragment newInstance(@StringRes final int titleResourceId, @ArrayRes final int arrayResourceId) {
ListDialogFragment dialogFragment = new ListDialogFragment();
Bundle bundle = new Bundle();
bundle.putInt(TITLE_RESOURCE_ID, titleResourceId);
bundle.putInt(ARRAY_RESOURCE_ID, arrayResourceId);
dialogFragment.setArguments(bundle);
return dialogFragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
listener = (DialogItemSelectedListener) getTargetFragment();
} catch (ClassCastException e) {
throw new ClassCastException("Calling Fragment must implement DialogItemSelectedListener");
}
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final int title = getArguments().getInt(TITLE_RESOURCE_ID);
final int listItems = getArguments().getInt(ARRAY_RESOURCE_ID);
return new AlertDialog.Builder(getActivity()).setTitle(title).setItems(listItems, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
listener.onTakePhotoSelected();
break;
case 1:
listener.onSelectImageSelected();
break;
default:
dismiss();
}
}
}).create();
}
}
片段实现DialogItemSelectedListener,并在调用onTakePhotoSelected() 时尝试执行以下操作:
private void takePicture() {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Here, we make sure that an Activity to handle the Camera Intent actually exists
if (cameraIntent.resolveActivity(context.getApplicationContext().getPackageManager()) != null) {
// This is where the photo itself will actually go
imageFile = null;
try {
imageFile = createImageFile();
} catch (IOException ex) {
// Could not create the file
}
// At this point, we can safely assume that the file was created successfully
if (imageFile != null) {
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));
startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
活动:
public class TestActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_activity);
attachFragment();
}
private void attachFragment() {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, TestFragment.newInstance()).commit();
}
}
调用startActivityForResult();时应用崩溃
java.lang.IllegalStateException: Fragment MyTestFragment{22b2a6a0} not attached to Activity
我在这里做错了什么;这种行为是可以预期的吗?
我已经尝试过setRetainInstance(true);,但这不起作用。
【问题讨论】:
-
尝试将
getFragmentManager()更改为getChildFragmentManager,因为您是从片段内而不是从活动中打开对话框。