我的代码中发生了一些奇怪的事情,我不明白它是什么......
我有一些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
获取值之前:
tag
获取值后
输入AppCompatViewInflater.java
后:
答案 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;
}
}