我有一个片段。单击图像后,我开始活动。但是我一直收到这个错误
Unable to start activity ComponentInfo{com.flag.app/com.flag.app.app.activity.MarkerDetailsActivity}:
java.lang.NullPointerException: Attempt to invoke virtual method 'com.flag.app.app.model.Image com.flag.app.app.model.Marker.getImage()'
on a null object reference
这是我的活动
public class MarkerDetailsActivity extends AppCompatActivity {
public static Intent getStartIntent(Context context, Marker marker) {
Intent startIntent = new Intent(context, MarkerDetailsActivity.class);
startIntent.putExtra(EXTRA_MARKER, marker);
return startIntent;
}
ImageView mImageView;
TextView mNameTextView;
Marker mMarker;
MapView mMapView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details_marker);
mMarker = getIntent().getParcelableExtra(EXTRA_MARKER);
mImageView = findViewById(R.id.image_view);
mMapView = findViewById(R.id.map_view);
mMapView.onCreate(savedInstanceState);
bindMarker(mMarker);
}
private void bindMarker(Marker marker) {
Picasso.with(mImageView.getContext())
.load(marker.getImage().getUrl())
.into(mImageView);
mAuthorTextView.setText(marker.getAuthor());
mDescriptionTextView.setText(marker.getDescription());
mDateTextView.setText(marker.getDate());
}
}
如何解决?
答案 0 :(得分:0)
检查是否为空值。
private void bindMarker(Marker marker) {
if(marker != null){
Picasso.with(mImageView.getContext())
.load(marker.getImage().getUrl())
.into(mImageView);
mAuthorTextView.setText(marker.getAuthor());
mDescriptionTextView.setText(marker.getDescription());
mDateTextView.setText(marker.getDate());
}}
答案 1 :(得分:0)
private void bindMarker(Marker marker) {
if(marker!=null){
if(marker.getImage()!=null) {
Picasso.with(mImageView.getContext())
.load(marker.getImage().getUrl())
.into(mImageView);
}
mAuthorTextView.setText(marker.getAuthor());
mDescriptionTextView.setText(marker.getDescription());
mDateTextView.setText(marker.getDate());
}
}
使用Marker
对象是否为空之前必须检查