android中的java.lang.nullpointer错误

android中的java.lang.nullpointer错误,java,android-studio,nullpointerexception,Java,Android Studio,Nullpointerexception,我正在制作一个应用程序,它将显示名人的图像和4个名字,其中一个是正确的 我添加了一个“更改”按钮来更改图像中的名人,但按下该按钮后,我的应用程序崩溃,显示null java.lang.null指针异常 我在oncreate方法中有用于加载图像和四个名称的代码,该方法在应用程序首次启动时加载图像和四个名称,但在调用函数时按下按钮时,会生成错误,尽管我在其中有相同的代码 public class MainActivity extends AppCompatActivity { //decla

我正在制作一个应用程序,它将显示名人的图像和4个名字,其中一个是正确的

我添加了一个“更改”按钮来更改图像中的名人,但按下该按钮后,我的应用程序崩溃,显示null java.lang.null指针异常

我在oncreate方法中有用于加载图像和四个名称的代码,该方法在应用程序首次启动时加载图像和四个名称,但在调用函数时按下按钮时,会生成错误,尽管我在其中有相同的代码

public class MainActivity extends AppCompatActivity {
    //declaring all the widgets to be used
    Bitmap celebimg;
    Random rand;
    ImageView img;
    Button btn0, btn1, btn2, btn3;
    int optionnum, imgnum;
    ArrayList<String> celebnames = new ArrayList<String>();
    ArrayList<String> celebimages = new ArrayList<String>();
    ArrayList<String> buttonoptions = new ArrayList<String>(); 

    //Invoked when one out of the four options is clicked
    public void row(View view) {
        if (view.getTag().toString().equals(Integer.toString(optionnum))){
            Toast.makeText(this, "correct Answer", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "wrong Answer, It was" + 
            celebnames.get(imgnum), Toast.LENGTH_SHORT).show();
        }
    }

    //Class for downloading of image
    public class Imagedownload extends AsyncTask<String, Void, Bitmap> {     
    @Override
    protected Bitmap doInBackground(String... strings) {

        try {
            URL url = new URL(strings[0]);
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            connection.connect();
            InputStream in = connection.getInputStream();
            Bitmap myimage = BitmapFactory.decodeStream(in);
            return myimage;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
}

/**
  * Class for downloading the website html code
  */
public class Download extends AsyncTask<String, Void, String > {
    @Override
    protected String doInBackground(String... strings) {
        String result = "";
        URL url;
        HttpURLConnection connection = null;

        try {
            url = new URL(strings[0]);
            connection = (HttpURLConnection)url.openConnection();
            InputStream input = connection.getInputStream();
            InputStreamReader reader = new InputStreamReader(input);
            int data = reader.read();
            while(data != -1) {
                char current = (char)data;
                result += current;
                data = reader.read();
            }
            return result;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
}
public类MainActivity扩展了AppCompatActivity{
//声明要使用的所有小部件
位图;
随机兰德;
图像视图img;
按钮btn0、btn1、btn2、btn3;
int optionnum,imgnum;
ArrayList celebnames=新的ArrayList();
ArrayList celebimages=新的ArrayList();
ArrayList buttonoptions=新建ArrayList();
//单击四个选项中的一个时调用
公共无效行(视图){
if(view.getTag().toString().equals(Integer.toString(optionnum))){
Toast.makeText(这是“正确答案”,Toast.LENGTH_SHORT).show();
}否则{
Toast.makeText(这个“错误答案,是”+
celebnames.get(imgnum),Toast.LENGTH_SHORT.show();
}
}
//用于下载图像的类
公共类Imagedownload扩展异步任务{
@凌驾
受保护位图doInBackground(字符串…字符串){
试一试{
URL=新URL(字符串[0]);
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
connection.connect();
InputStream in=connection.getInputStream();
位图myimage=BitmapFactory.decodeStream(在中);
返回myimage;
}捕获(格式错误){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
返回null;
}
}
/**
*用于下载网站html代码的类
*/
公共类下载扩展异步任务{
@凌驾
受保护的字符串背景(字符串…字符串){
字符串结果=”;
网址;
HttpURLConnection=null;
试一试{
url=新url(字符串[0]);
connection=(HttpURLConnection)url.openConnection();
InputStream输入=连接。getInputStream();
InputStreamReader reader=新的InputStreamReader(输入);
int data=reader.read();
while(数据!=-1){
当前字符=(字符)数据;
结果+=电流;
data=reader.read();
}
返回结果;
}捕获(格式错误){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
返回null;
}
}
单击更改按钮时要执行的代码

public void change(View view) {
    rand = new Random();
    imgnum = rand.nextInt(celebimages.size());
    Imagedownload image = new Imagedownload();

    try {
        celebimg = image.execute(celebimages.get(imgnum)).get();
        if (celebimg == null)
            Log.i("its", "null douchebag");
        img.setImageBitmap(celebimg);
    } catch (InterruptedException e) {
        if (celebimg == null)
            Log.i("its", "null douchebag");
        e.printStackTrace();
    } catch (ExecutionException e) {
        if (celebimg == null)
            Log.i("its", "null douchebag");
        e.printStackTrace();
    }

    optionnum = rand.nextInt(4);

    for(int i = 0;i < 4;i++){
        if(i == optionnum)
            buttonoptions.add(celebnames.get(imgnum));
        else {
            int random = rand.nextInt(celebnames.size());
            while (celebnames.get(imgnum) == celebnames.get(random)){
                random = rand.nextInt(celebnames.size());
            }
            buttonoptions.add(celebnames.get(random));
        }
    }
    btn0.setText(buttonoptions.get(0));
    btn1.setText(buttonoptions.get(1));
    btn2.setText(buttonoptions.get(2));
    btn3.setText(buttonoptions.get(3));
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImageView img = (ImageView)findViewById(R.id.celeb);

    btn0 = (Button)findViewById(R.id.btn0);
    btn1 = (Button)findViewById(R.id.btn1);
    btn2 = (Button)findViewById(R.id.btn2);
    btn3 = (Button)findViewById(R.id.btn3);
    String data = "";
    Download load = new Download();
    try {
        data = load.execute("http://www.posh24.se/kandisar").get();
        String[] splitdata = data.split("<div class=\"title\">Lista:</div>");

        // seperating out the required img src from the html code
        Pattern p = Pattern.compile("src=\"(.*?)\"");
        Matcher M = p.matcher(splitdata[1]);

        while (M.find()){
            //adding all the img src values to celebimages arraylist
            celebimages.add(M.group(1));
        }
        Pattern pi = Pattern.compile("alt=\"(.*?)\"");
        Matcher Mi = pi.matcher(splitdata[1]);

        while (Mi.find()) {
           // adding all the alt values to celebnames arraylist
            celebnames.add(Mi.group(1));
        }

    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
    Random rand = new Random();
    imgnum = rand.nextInt(celebimages.size());
    Imagedownload image = new Imagedownload();
    Bitmap celebimg;
    try {
        //downloading the image from stored img src values from celebimages 
        //arraylist
        celebimg = image.execute(celebimages.get(imgnum)).get();
        img.setImageBitmap(celebimg);
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
    //Setting the correct value in one button and random values in other 
    //three
    optionnum = rand.nextInt(4);

    for(int i = 0;i < 4;i++){
        if(i == optionnum)
            buttonoptions.add(celebnames.get(imgnum));
        else {
            int random = rand.nextInt(85);
            while (celebnames.get(imgnum) == celebnames.get(random)){
                random = rand.nextInt(celebnames.size());
            }
            buttonoptions.add(celebnames.get(random));
        }
    }
    btn0.setText(buttonoptions.get(0));
    btn1.setText(buttonoptions.get(1));
    btn2.setText(buttonoptions.get(2));
    btn3.setText(buttonoptions.get(3));
}
公共作废更改(视图){
rand=新随机数();
imgnum=rand.nextInt(celebimages.size());
Imagedownload image=新的Imagedownload();
试一试{
celebimg=image.execute(celebimages.get(imgnum)).get();
if(celebimg==null)
Log.i(“its”、“空垃圾袋”);
img.setImageBitmap(celebimg);
}捕捉(中断异常e){
if(celebimg==null)
Log.i(“its”、“空垃圾袋”);
e、 printStackTrace();
}捕获(执行例外){
if(celebimg==null)
Log.i(“its”、“空垃圾袋”);
e、 printStackTrace();
}
optionnum=rand.nextInt(4);
对于(int i=0;i<4;i++){
if(i==optionnum)
add(celebnames.get(imgnum));
否则{
int random=rand.nextInt(celebnames.size());
while(celebnames.get(imgnum)=celebnames.get(random)){
random=rand.nextInt(celebnames.size());
}
add(celebnames.get(random));
}
}
btn0.setText(buttonoptions.get(0));
btn1.setText(buttonoptions.get(1));
btn2.setText(buttonoptions.get(2));
btn3.setText(buttonoptions.get(3));
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView img=(ImageView)findViewById(R.id.celeb);
btn0=(按钮)findViewById(R.id.btn0);
btn1=(按钮)findViewById(R.id.btn1);
btn2=(按钮)findViewById(R.id.btn2);
btn3=(按钮)findViewById(R.id.btn3);
字符串数据=”;
下载加载=新下载();
试一试{
数据=加载。执行(“http://www.posh24.se/kandisar).get();
String[]splitdata=data.split(“Lista:”);
//从html代码中分离出所需的IMGSRC
模式p=Pattern.compile(“src=\”(.*?\”);
Matcher M=p.Matcher(splitdata[1]);
while(M.find()){
//将所有img src值添加到celebimages arraylist
添加(M组(1));
}
Pattern pi=Pattern.compile(“alt=\”(.*?\”);
Matcher Mi=pi.Matcher(splitdata[1]);
while(Mi.find()){
//将所有alt值添加到celebnames arraylist
celebnames.add(Mi.group(1));
}
}捕捉(中断异常e){
e、 printStackTrace();
}捕获(执行例外){
e、 printStackTrace();
}
Random rand=新的Random();
imgnum=rand.nextInt(celebimages.size());
Imagedownload image=新的Imagedownload();
位图;
试一试{
//从celebimages存储的img src值下载图像
//arraylist
celebimg=image.execute(celebimages.get(imgnum)).get();
img.setImageBitmap(celebimg);
}捕捉(中断异常e){
e、 printStackTrace();
}捕获(执行例外){
e、 printStackTrace();
}
//塞特
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImageView img = (ImageView)findViewById(R.id.celeb);

    ...
}
img = (ImageView)findViewById(R.id.celeb);
Bitmap celebimg;
Random rand;
ImageView img;
Button btn0, btn1, btn2, btn3;
Bitmap mBmpCeleb;
Random mRndSomeId;
ImageView mImvCeleb;
Button mBtnImageOne, mBtnImageTwo, mBtnImageThree, mBtnImageThree;
ImageView imvCeleb = (ImageView) findViewById(R.id.celeb);