我的ImageView总是试图重复使用相同的位图,而每当我点击(onTap())时,它应该去寻找不使用相同图像的正确图像。我怎么能阻止它呢? 此外,每当我回收它时,当我再次点击时,我的活动就会崩溃......
请帮忙!
我的logcat给了我:
03-14 14:40:15.519 D/setting image>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>(29389): grrrrr java.lang.NullPointerException
这是我的片段:
protected boolean onTap(int i) {
/* Toast.makeText(getApplicationContext(),
locations.get(i).getSnippet() + " " + locations.get(i).getTitle(),
Toast.LENGTH_SHORT).show();*/
// mapView = (MapView) findViewById(R.id.mapView);
OverlayItem item=getItem(i);
GeoPoint geo=item.getPoint();
Bitmap bitmap = null;
Point pro= mapView.getProjection().toPixels(geo, null);
if (pro!=null)
{ View view=panel.getView();
/* }
catch (Exception e){Log.d("merde>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "grrrrr " +e);}*/
String capteur= getType (locations.get(i).getSnippet());
((TextView)view.findViewById(R.id.poptext))
.setText(String.valueOf("image captured by : " + capteur + "\n" + "Latitude = " + locations.get(i).getPoint().getLatitudeE6() +" " + "Longitude = "+ locations.get(i).getPoint().getLongitudeE6() +" " +
"\n" + "Image can be found at the following directory :" + locations.get(i).getTitle()
));
if (locations.get(i).getTitle()!=null){
try {
bitmap = BitmapFactory.decodeFile(locations.get(i).getTitle());} catch (Exception e){Log.d("getting Title>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "grrrrr " +e.getMessage());}
if (bitmap!=null){
try{
mContext=getApplicationContext ();
((ImageView) findViewById(R.id.ImageV)).setImageBitmap(bitmap);
} catch (Exception e){Log.d("setting image>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "grrrrr " +e);}}
else {}
}else {}
panel.show(true
);}
return(true);
}
my popup panel
class PopupPanel {
Context mContext= getApplicationContext();
View popup;
boolean isVisible=false;
PopupPanel(int layout) {
ViewGroup parent=(ViewGroup)mapView.getParent();
popup=getLayoutInflater().inflate(layout, parent, false);
popup.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
hide();
}
});
}
View getView() {
return(popup);
}
void show(boolean alignTop) {
RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
if (alignTop) {
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp.setMargins(20, 20, 20, 20);
}
else {
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lp.setMargins(20, 20, 20, 60);
}
hide();
((ViewGroup)mapView.getParent()).addView(popup, lp);
isVisible=true;
}
void hide() {
if (isVisible) {
isVisible=false;
((ViewGroup)popup.getParent()).removeView(popup);
}
}
答案 0 :(得分:1)
我可以马上告诉你的一件事是,e.getMessage()并没有真正起作用。它将返回null并且您将获得空指针异常。
<强>更新强>
好吧,我也可以告诉你,
((ImageView) findViewById(R.id.ImageV)).setImageBitmap(bitmap);
当你打电话并说出
时findViewById(R.id.ImageV)
视图R.id.ImageV在其他地方膨胀,你在这里得到的只是一个原始参考。对那个观点。您需要引用最初夸大该视图的实际视图,然后通过该视图调用
myOldView.findViewById(R.id.ImageV)
这肯定会解决你的问题。
答案 1 :(得分:0)
只有两件事可以为null
ImageView
- &gt;如果是这种情况,请确保您使用的是定义了IamgeV的布局 *。 Bitmap
,但事实并非如此,因为您已经验证过if条件
因此请确保使用正确的布局文件或传递正确的ImageView ID