我有一个名为locateImage的方法来检索图像资源。为什么我得到NullPointerException?
public class MediaPreviewActivity extends AppCompatActivity {
private List<Note> notes;
private SubsamplingScaleImageView imageView;
private TextView note;
private int position = 0;
private View btnNext, btnPrev;
@SuppressLint("SetTextI18n")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*note = (TextView) findViewById(R.id.note);
btnNext = findViewById(R.id.next);
btnPrev = findViewById(R.id.previous);*/
locateImageView();
btnPrev.setOnClickListener(onClickListener(0));
btnNext.setOnClickListener(onClickListener(1));
notes = Arrays.asList(
new Note("Pinch to zoom", "Use two finger pinch to zoom in and out. The zoom is centred on the pinch gesture, and you can pan at the same time."),
new Note("Quick scale", "Double tap and swipe up or down to zoom in or out. The zoom is centred where you tapped."),
new Note("Drag", "Use one finger to drag the image around."),
new Note("Fling", "If you drag quickly and let go, fling momentum keeps the image moving."),
new Note("Double tap", "Double tap the image to zoom in to that spot. Double tap again to zoom out.")
);
note.setText(notes.get(position).subtitle + ": " + notes.get(0).text);
}
/*@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.rotate) {
//rotating image
imageView.setOrientation((imageView.getOrientation() + 90) % 360);
}
return super.onOptionsItemSelected(item);
}*/
@SuppressLint("SetTextI18n")
private View.OnClickListener onClickListener(final int i) {
return new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v == btnNext) {
if (position < notes.size() - 1) {
position++;
note.setText(notes.get(position).subtitle + ": " + notes.get(position).text);
Toast.makeText(MediaPreviewActivity.this, "Next tip!", Toast.LENGTH_SHORT).show();
}
} else {
if (position > 0) {
position--;
Toast.makeText(MediaPreviewActivity.this, "Previous tip!", Toast.LENGTH_SHORT).show();
}
note.setText(notes.get(position).subtitle + ": " + notes.get(position).subtitle + ": " + notes.get(position).text);
}
}
};
}
private void locateImageView() {
imageView = (SubsamplingScaleImageView) findViewById(R.id.image);
imageView.setImage(ImageSource.asset("yeet_club_banner.png"));
}
private class Note {
private final String text;
private final String subtitle;
public Note(String subtitle, String text) {
this.subtitle = subtitle;
this.text = text;
}
}
}
例外:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void
com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView.setImage(com.davemorrissey.labs.subscaleview.ImageSource)' on a null object reference
at com.yitter.android.MediaPreviewActivity.locateImageView(MediaPreviewActivity.java:87)
at com.yitter.android.MediaPreviewActivity.onCreate(MediaPreviewActivity.java:32)