gpt4 book ai didi

android - 旋转设备后从 DialogFragment 接收回调时 fragment 未附加到 Activity

转载 作者:行者123 更新时间:2023-11-29 15:12:04 28 4
gpt4 key购买 nike

我正在创建一个应用程序,允许用户从图库上传图像或使用设备相机拍照。单击适当的按钮时,会出现一个对话框,用户可以选择“拍照”或“选择图像”。这一切都很好,问题是当我在屏幕上显示对话框时旋转设备然后选择一个选项应用程序崩溃并显示 IllegalStateException 说我的 Fragment 没有附加到 Activity。我已经包含了我用来与目标 fragment 进行通信的对话框:

创建 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();
}

该 fragment 实现了 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);
}
}
}

Activity :

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); 但这不起作用。

最佳答案

这里的问题是每次方向改变时,您都会创建一个新的 Fragment 实例。而且其中只有一个实际上附加到 Activity。试试这个

    public class TestActivity extends ActionBarActivity {

TestFragment testFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_activity);
attachFragment(savedInstanceState);
}

private void attachFragment(Bundle savedInstanceState) {
if (savedInstanceState == null) {
testFragment = TestFragment.newInstance();
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, testFragment, "testFragment").commit();
} else {
testFragment = (TestFragment) getSupportFragmentManager().findFragmentByTag("testFragment");
}
}

关于android - 旋转设备后从 DialogFragment 接收回调时 fragment 未附加到 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30174197/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com