我一直在尋找這個千職位,但我沒有找到如何解決我的問題。onActivityResult()沒有得到在DialogFragment執行
我有一個ImageView。當用戶點擊這個ImageView時,會顯示一個DialogFragment,用戶可以選擇用相機拍攝新照片,或從畫廊中選擇一張照片。直到這裏一切正常。
的問題是,由用戶選擇的圖像時,應替換當前在ImageView的,但該位,是不工作的一個,因爲執行該代碼的onActivityResult()函數是不是被執行,因此ImageView中的圖像始終保持不變。我會很感激任何幫助,因爲我沒有看到或理解,爲什麼這個代碼沒有被執行。
我的用戶之後獲得的logcat的一個警告,選擇圖像:
05-07 12:17:11.542: I/ActivityManager(59): Displayed activity com.android.gallery/com.android.camera.ImageGallery: 935 ms (total 935 ms)
05-07 12:17:12.812: W/FragmentActivity(3614): Activity result no fragment exists for index: 0x10001
05-07 12:17:12.862: W/InputManagerService(59): Starting input on non-focused client [email protected] (uid=10016 pid=317)
Activity.java:
private ImageView imageLoader = null;
imageLoader = (ImageView) findViewById(R.id.f_imageLoader);
imageLoader.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
ImageLoaderDialog imageLoaderDialog = new ImageLoaderDialog(imageLoader);
imageLoaderDialog.show(getSupportFragmentManager(), "imageLoaderDialog");
}
Activity.xml:
<ImageView
android:id="@+id/f_imageLoader"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="0.20"
android:contentDescription="@string/imgDesc"
android:src="@drawable/my_image" />
ImageLoaderDialog .java:
//Dialog for choosing between new camera image or gallery image.
public class ImageLoaderDialog extends android.support.v4.app.DialogFragment {
private ImageView targetImageView = null;
final int TAKE_PICTURE = 0;
final int PICK_PHOTO = 1;
public ImageLoaderDialog (View view) {
targetImageView = (ImageView) view;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Selecciona");
final String[] imageSources = getResources().getStringArray(R.array.imageSources);
builder.setItems(imageSources, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
switch(item) {
case TAKE_PICTURE:
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePicture, TAKE_PICTURE);
break;
case PICK_PHOTO:
Intent pickPhoto = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto, PICK_PHOTO);
break;
}
}
});
return builder.create();
}
//Set image to user's selected image.
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == android.app.Activity.RESULT_OK) {
Uri selectedImage = intent.getData();
Log.i("IMAGEN", ""+selectedImage);
targetImageView.setImageURI(selectedImage);
}
}
}
任何幫助將不勝感激。
爲FragmentActivity不可見。 – Rowan 2015-02-20 02:23:19
Android 4.4.2顯然不再是這樣。如果片段調用自己的'startActivityForResult(...)',則不調用活動的'onActivityResult(...)'。如果片段調用'getActivity()。startActivityForResult(...)',則是。 – 2015-08-14 21:54:34