Android片段Universal ImageLoader - View不能为null,

时间:2015-12-15 05:09:01

标签: java android android-fragments universal-image-loader

情况是:我正在尝试从片段更新活动中的图像。

例外:java.lang.IllegalArgumentException: view must not be null

onCreateView:

 activityIcon = (ImageView) getActivity().findViewById(R.id.my_figure_view);

按钮监听器:

 imageLoader.displayImage("file://" + uri, activityIcon, ImageHelper.Personoptions);

我在Stackoverflow上发现了许多其他类似的问题。不幸的是,我没有得到明确的解决方案。

 11-17 12:58:20.710 1824-1824/com.itscn.zhbt W/System.err:     at com.nostra13.universalimageloader.core.imageaware.ViewAware.<init>(ViewAware.java:70)
11-17 12:58:20.710 1824-1824/com.itscn.zhbt W/System.err:     at com.nostra13.universalimageloader.core.imageaware.ViewAware.<init>(ViewAware.java:50)
11-17 12:58:20.710 1824-2032/com.itscn.zhbt D/ImageLoader: Start display image task [file:///storage/emulated/0/itscn/header/header.png_140x140]
11-17 12:58:20.710 1824-1824/com.itscn.zhbt W/System.err:     at com.nostra13.universalimageloader.core.imageaware.ImageViewAware.<init>(ImageViewAware.java:44)
11-17 12:58:20.710 1824-1824/com.itscn.zhbt W/System.err:     at com.nostra13.universalimageloader.core.ImageLoader.displayImage(ImageLoader.java:303)
11-17 12:58:20.710 1824-1824/com.itscn.zhbt W/System.err:     at com.itscn.zhbt.fragment.slider.PersonalInfo.loadTakeIcon(PersonalInfo.java:225)
11-17 12:58:20.710 1824-1824/com.itscn.zhbt W/System.err:     at com.itscn.zhbt.fragment.slider.PersonalInfo.onActivityResult(PersonalInfo.java:176)
11-17 12:58:20.710 1824-2032/com.itscn.zhbt D/ImageLoader: Load image from network [file:///storage/emulated/0/itscn/header/header.png_140x140]
11-17 12:58:20.710 1824-1824/com.itscn.zhbt W/System.err:     at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:156)
11-17 12:58:20.710 1824-1824/com.itscn.zhbt W/System.err:     at android.app.Activity.dispatchActivityResult(Activity.java)
11-17 12:58:20.710 1824-1824/com.itscn.zhbt W/System.err:     at android.app.ActivityThread.deliverResults(ActivityThread.java)
11-17 12:58:20.710 1824-1824/com.itscn.zhbt W/System.err:     at android.app.ActivityThread.performResumeActivity(ActivityThread.java)
11-17 12:58:20.710 1824-1824/com.itscn.zhbt W/System.err:     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java)
11-17 12:58:20.710 1824-1824/com.itscn.zhbt W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
11-17 12:58:20.710 1824-1824/com.itscn.zhbt W/System.err:     at android.app.ActivityThread.access$700(ActivityThread.java)
11-17 12:58:20.710 1824-1824/com.itscn.zhbt W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
11-17 12:58:20.715 1824-1824/com.itscn.zhbt W/System.err:     at android.os.Handler.dispatchMessage(Handler.java)
11-17 12:58:20.715 1824-1824/com.itscn.zhbt W/System.err:     at android.os.Looper.loop(Looper.java)
11-17 12:58:20.715 1824-1824/com.itscn.zhbt W/System.err:     at android.app.ActivityThread.main(ActivityThread.java)
11-17 12:58:20.715 1824-1824/com.itscn.zhbt W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
11-17 12:58:20.715 1824-1824/com.itscn.zhbt W/System.err:     at java.lang.reflect.Method.invoke(Method.java)
11-17 12:58:20.715 1824-1824/com.itscn.zhbt W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
11-17 12:58:20.715 1824-1824/com.itscn.zhbt W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
11-17 12:58:20.715 1824-1824/com.itscn.zhbt W/System.err:     at dalvik.system.NativeStart.main(Native Method)

Java代码

public class PersonalInfo extends Fragment {


    private Bitmap myBitmap;
    private byte[] mContent;

    private MainActivity mainActivity;

    private Uri imageUri;
    private File imageFile;

    public static final int TAKE_PHOTO = 1;
    public static final int CHOOSE_PHOTO = 2;
    public static final int REQUE_CODE_CROP = 3;

    private ImageView person_photo;
    private ImageView activityIcon;

    private Handler myHandler = new Handler();
    private Runnable mLoadingRunnable = new Runnable() {
        @Override
        public void run() {
            loadIcon();
        }
    };


    public PersonalInfo() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_personal__info, container, false);
        mainActivity = (MainActivity) getActivity();
        person_photo = (ImageView) rootView.findViewById(R.id.person_photo);
        activityIcon = (ImageView) getActivity().findViewById(R.id.my_figure_view);

        myHandler.post(mLoadingRunnable);

        setListener(rootView);
        return rootView;
    }

    private void setListener(View rootView) {
        ImageView backImage = (ImageView) rootView.findViewById(R.id.back);
        backImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mainActivity.backToMain();
            }
        });

        person_photo.setOnClickListener(new View.OnClickListener() {
            final CharSequence[] items = {"Alumb", "Photo"};

            @Override
            public void onClick(View v) {
                AlertDialog dlg = new AlertDialog.Builder(getContext()).setTitle("choose photo").setItems(items,
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                                if (which == 1) {
                                    Intent getImageByCamera = new Intent("android.media.action.IMAGE_CAPTURE");

                                    getImageByCamera = setIntent(getImageByCamera);

                                    startActivityForResult(getImageByCamera, TAKE_PHOTO);

                                } else {
                                    Intent getImage = new Intent(Intent.ACTION_GET_CONTENT);
                                    getImage.addCategory(Intent.CATEGORY_OPENABLE);
                                    getImage.setType("image/png");
                                    getImage.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri);

                                    getImage = setIntent(getImage);

                                    getImage.putExtra("crop", "true");
                                    getImage.putExtra("aspectX", 1);
                                    getImage.putExtra("aspectY", 1);
                                    getImage.putExtra("outputX", 600);
                                    getImage.putExtra("outputY", 600);
                                    getImage.putExtra("scale", true);
                                    getImage.putExtra("return-data", false);
                                    startActivityForResult(getImage, CHOOSE_PHOTO);
                                }

                            }

                            protected Intent setIntent(Intent intent) {
                                //folder stuff
                                File imagesFolder = new File(Environment.getExternalStorageDirectory(), "/itscn/header");

                                imagesFolder.mkdirs();

                                imageFile = new File(imagesFolder, "header.png");
                                Uri uriSavedImage = Uri.fromFile(imageFile);

                                intent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);

                                return intent;
                            }
                        }).create();
                dlg.show();
            }
        });
    }


    private void loadIcon() {

        String path = FileUtils.getHeaderPictureDirPath() + "/header.png";
        Uri uri = Uri.fromFile(new File(path));
        ImageLoader imageLoader = ImageLoader.getInstance();
        imageLoader.clearDiskCache();
        imageLoader.clearMemoryCache();

        imageLoader.displayImage(uri.toString(), person_photo, ImageHelper.Personoptions);

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);


        switch (requestCode) {
            case CHOOSE_PHOTO:
                try {
                    loadTakeIcon(FileUtils.getHeaderPictureDirPath() + "/header.png");
                } catch (Exception e) {
                    e.printStackTrace();
                }

                break;

            case TAKE_PHOTO:

                try {
                    loadTakeIcon(FileUtils.getHeaderPictureDirPath() + "/header.png");


                } catch (Exception e) {
                    e.printStackTrace();
                }

                break;
            case REQUE_CODE_CROP:
                try {
                    loadTakeIcon(FileUtils.getHeaderPictureDirPath() + "/header.png");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            default:
                break;
        }


    }



    private void loadTakeIcon(String uri) {
        ImageLoader imageLoader = ImageLoader.getInstance();
        imageLoader.clearDiskCache();
        imageLoader.clearMemoryCache();
        imageLoader.displayImage("file://" + uri, person_photo, ImageHelper.Personoptions);
        if (activityIcon != null) {
            imageLoader.displayImage("file://" + uri, activityIcon, ImageHelper.Personoptions);
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.d("PersonalInfo", "onDestroyView");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("PersonalInfo", "onDestroy");
    }
}

1 个答案:

答案 0 :(得分:0)

试试这个

 activityIcon = <your navigationview>.findViewById(R.id.my_figure_view);