- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在创建一个应用程序,允许用户从图库上传图像或使用设备相机拍照。单击适当的按钮时,会出现一个对话框,用户可以选择“拍照”或“选择图像”。这一切都很好,问题是当我在屏幕上显示对话框时旋转设备然后选择一个选项应用程序崩溃并显示 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/
我有一个 AppCompatActivity,它在某个时候显示一个 DialogFragment。在此对话框中,有些项目在删除之前需要确认。通过另一个 Yes/No DialogFragment 询问
我们添加一个通用的/普通的 fragment ,以编程的方式做一些事情,比如: fragmentTransaction.add(containerViewId, fragmentToAdd, frag
我有一个 DialogFragment 向用户显示选项列表,其中一个选项是“删除”选项,当用户按下删除选项时我想显示另一个 DialogFragment 作为确认,不幸的是,确认对话框没有显示。 这是
假设我在一个名为 my_dialog_fragment.xml 的 xml 布局文件中指定了 DialogFragment 的布局,并指定了 layout_width 和 >layout_height
当我设置对话框 fragment 的样式时,android 允许截取对话框 fragment 的屏幕截图,但在删除样式后它会起作用。 下面是对话框 fragment 的onCreate代码 @Over
我的应用程序中有一个带有日期和时间字段的 DialogFragment1(由 MyFragmentActivity 调用)。当用户单击它们时,会出现 DatePickerFragment 和 Time
显示 ProgressDialog 时应用程序崩溃。我正在使用滑出式键盘在手机上进行测试。显示对话框时,我滑动键盘,应用崩溃。 public static class ProgressDialogFr
正如问题中提到的.. 我有一个 fragment ,我从中创建并显示一个 DialogFragment,并将 targetFragment 设置为此当前 fragment 。 DialogFragme
如何在此 DialogFragment 中检索 editText1?它存在于 vraag_banen.xml 中,但 getView() 为空。 该对话框显示正常,也没有编译错误,但是我无法弄清楚如何
我应该如何创造这个设计的优越部分? 我正在使用 DialogFragment 在弹出窗口中执行此操作,但无法实现顶部透明的效果。 按钮设计但 background_circle: 使用
最后,我发现问题是什么...... 如果你有这样的功能: public void test() { DialogFragment dlg = new LoginDialog(); dl
我有一个带有监听器的 DialogFragment,用于单击按钮以调用 fragment 中的函数。 我收到 lateinit property listener has not been initi
我在 Activity 上有一个 DialogFragment。此 DialogFragment 是一个由 Web 服务调用填充的 ListView 。单击此 ListView 中的一行后,对话框应消
我正在尝试遵循本教程:http://developer.android.com/guide/topics/ui/controls/pickers.html#DatePicker 这是他们提供的代码,所
有什么方法可以更改 DialogFragment 中正面、负面和中性按钮的显示顺序,以便我可以先放置负面按钮? 我不想改变按钮的“性质”,而是保持它们为“积极”、“消极”和“中性”。 最佳答案 尝试这
这里可能重复,抱歉... 我不知道如何将自定义样式添加到我的 DialogFragment。此刻我正在上课 public final class SelectFragment extends Dial
我花了几个小时来让这个简单的用例正常工作:当我按下音量硬件按钮时,我想打开一个 DialogFragment。该对话框包含一个 SeekBar,应根据按下的按钮进行设置。 我这里有几个问题。 首先,我
DialogFramgment 有问题(在多个 Android 4.2/4.4 设备上支持 v4 库)。 我有两个 DialogFragment:EditAccountDialogFragment 和
当我将有效代码从 fragment 复制到dialogfragment时,它突然不起作用。 在常规 fragment 上:将位图保存在 sd 卡上。 on dialogfragment: 不将位图保存
我花了一整天的时间试图弥补这一点,但我不能.. 这就是问题所在:我想要一个是/否 AlertDialog方向改变时不会消失,所以我决定使用 DialogFragment . 所以我准备了代码并第一次使
我是一名优秀的程序员,十分优秀!