Android:ImageView小部件

时间:2017-07-04 07:30:56

标签: java android nullpointerexception

我的代码中发生了一些奇怪的事情,我不明白它是什么......

我有一些Imageviews,其中4个可以点击以启动CropActivity,裁剪图片,裁剪的图片将显示在点击的ImageView中。

所以,找出点击的ImageViews中的哪一个我喜欢这样:

public void onImageViewClicked(View v){
        String tag = v.getTag().toString();
        switch (tag){
            case "imgvW_1":
                this.clickedImgVw = (ImageView) findViewById(R.id.imgvW_1);
                break;
            case "imgvW_2":
                this.clickedImgVw = (ImageView) findViewById(R.id.imgvW_2);
                break;
            case "imgvW_3":
                this.clickedImgVw = (ImageView) findViewById(R.id.imgvW_3);
                break;
            case "imgvW_4":
                this.clickedImgVw = (ImageView) findViewById(R.id.imgvW_4);
                break;

        }

    }

所以现在......如果我是正确的......点击的ImageView存储在clickedImgVw .....

问题是......当我尝试在clickedImgVw中访问onActivityResult时,在裁剪之后,我得到NullPointerException

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 0 && resultCode == RESULT_OK){
            ImageCropFunction();
        }
        else if (requestCode == 2) {
            if (data != null) {
                imgUri = data.getData();
                ImageCropFunction();
            }
        }
        else if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
            if (data != null) {

                CropImage.ActivityResult result = CropImage.getActivityResult(data);

//THIS LINE HERE GIVES NULLPOINTEREXCEPTION ---------
                    String tag = clickedImgVw.getTag().toString();
//THIS LINE HERE GIVES NULLPOINTEREXCEPTION ---------

                if (resultCode == RESULT_OK) {
                    Uri resultUri = result.getUri();
                    DisplayImageOptions options = new DisplayImageOptions.Builder()
                            .displayer(new RoundedBitmapDisplayer(10))
                            .build();
                   imageLoader.displayImage(String.valueOf(resultUri),clickedImgVw, options);

                }
            }
        }
    }

这里发生了什么?,这里有什么问题?

* -------编辑--------- *

经过一些调试后,我注意到名为tag的变量在运行时改变了它的值。

我在同一个调试运行期间有一些屏幕显示...当应用程序进入名为AppCompatViewInflater.java的类时,变量tab会丢失其值

tag获取值之前:

enter image description here

tag获取值后

enter image description here

输入AppCompatViewInflater.java后:

enter image description here

1 个答案:

答案 0 :(得分:-2)

<强> Activity.class

public void onImageViewClicked(View v){
    String tag = v.getTag().toString();
    v.setTag("imgvW_1");
    switch (tag){
        case "imgvW_1":
            this.clickedImgVw = (ImageView) findViewById(R.id.imgvW_1);
            this.clickedImgVw.setTag("imgvW_1");
            break;
        case "imgvW_2":
            this.clickedImgVw = (ImageView) findViewById(R.id.imgvW_2);
            this.clickedImgVw.setTag("imgvW_2");
            break;
        case "imgvW_3":
            this.clickedImgVw = (ImageView) findViewById(R.id.imgvW_3);
            this.clickedImgVw.setTag("imgvW_3");
            break;
        case "imgvW_4":
            this.clickedImgVw = (ImageView) findViewById(R.id.imgvW_4);
            this.clickedImgVw.setTag("imgvW_4");
            break;

    }

}