我在尝试setImageResource
到ImageView数组中的某些图像时,我有点困惑。我在一个更简单的项目中复制了错误,所以这里是代码。我确信这一定是一个愚蠢的错误,但我已经尝试了很多,无法弄清楚正确的执行。我无法理解为什么图像是一个空对象引用,因为我已经以我能想到的各种方式初始化它。我在代码中提到的行中收到此错误。
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference.
package com.example.test;
import android.media.Image;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private ImageView image1;
private ImageView image2;
private ImageView image3;
private ImageView image;
private ImageView[] images = {image1, image2, image3, image};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image1 =(ImageView) findViewById(R.id.image1);
image2 = (ImageView) findViewById(R.id.image2);
image3 = (ImageView) findViewById(R.id.image3);
image = (ImageView) findViewById(R.id.image);
setImages();
}
public void setImages() {
//This is where I am getting the error
images[0].setImageResource(R.drawable.a);
images[1].setImageResource(R.drawable.b);
images[2].setImageResource(R.drawable.c);
images[3].setImageResource(R.drawable.d);
}
}
答案 0 :(得分:1)
public class MainActivity extends AppCompatActivity {
private ImageView[] images = new ImageView[4];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
images [0]=(ImageView) findViewById(R.id.image1);
images [1] = (ImageView) findViewById(R.id.image2);
images [2] = (ImageView) findViewById(R.id.image3);
images [3] = (ImageView) findViewById(R.id.image);
setImages();
}
public void setImages() {
//This is where I am getting the error
images[0].setImageResource(R.drawable.a);
images[1].setImageResource(R.drawable.b);
images[2].setImageResource(R.drawable.c);
images[3].setImageResource(R.drawable.d);
}
答案 1 :(得分:0)
当你创建了如下图像的数组
private ImageView[] images;
在创建image1之前,image2就像它一样,它将始终返回null。
尝试使用以下代码。
private ImageView[] images = {image1, image2, image3, image};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image1 =(ImageView) findViewById(R.id.image1);
image2 = (ImageView) findViewById(R.id.image2);
image3 = (ImageView) findViewById(R.id.image3);
image = (ImageView) findViewById(R.id.image);
images = new ImageView[]{image1, image2, image3, image};
setImages();
}
希望这些可以帮助你。
答案 2 :(得分:0)
您需要在images
方法中获取值后初始化onCreate
数组,因为image1
,image2
,image3
和image
由于初始化器(用于图像的初始化器)在构造函数之前执行(并且肯定在null
方法之前),因此在初始化图像时onCreate()
为public class MainActivity extends AppCompatActivity {
private ImageView image1;
private ImageView image2;
private ImageView image3;
private ImageView image;
private ImageView[] images;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image1 =(ImageView) findViewById(R.id.image1);
image2 = (ImageView) findViewById(R.id.image2);
image3 = (ImageView) findViewById(R.id.image3);
image = (ImageView) findViewById(R.id.image);
images = new ImageView[4];
images[0] = image1;
image[1] = image2;
image[2] = image3;
image[3] = image;
setImages();
}
public void setImages() {
images[0].setImageResource(R.drawable.a);
images[1].setImageResource(R.drawable.b);
images[2].setImageResource(R.drawable.c);
images[3].setImageResource(R.drawable.d);
}
}
。请尝试以下方法,
core_url_rewrite.id_path LIKE 'category/%'
希望这有帮助