Hello. I want to make such a small application that will show pictures in turn. There are two buttons back and forth for displaying the next and previous picture. Now what I have done-> I created the class Who, which has the fields name (String) and photo (ImageView). Also made a constructor, Who(String name,ImageView photo){this.name=name; this.photo = photo}; Who(String name,ImageView photo){this.name=name; this.photo = photo};

Next in the main activity class, I created a couple of variables of type ImageView. For each, I used the setImageResourse method. Then I created as many variables of the 'Who' type and sent the name and a variable of the ImageView type. Then all these variables of the form Who I saved in an ArrayList of the Who type. Next, by creating the showCurrentPerson() method, I assign an image to my image . But the program did not start and displayed an error. Where am I wrong? Can't I Assign ImageView Variables? The following is the MainActivity:

 public ArrayList<Who> list; public Button RightButton, WrongButton; public ImageView ImageOf; int counter = 0; int point = 0 ; TextView name; private ImageView i1, i2, i3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RightButton = (Button) findViewById(R.id.righButton); WrongButton = (Button) findViewById(R.id.wrongButton); ImageOf = (ImageView) findViewById(R.id.imageView) name = (TextView)findViewById(R.id.textView); i1.setImageResource(R.drawable.leo); i2.setImageResource(R.drawable.en); Who w1 = new Who("leo",i1); Who w2 = new Who("en",i2); list.add(w1); list.add(w2); showCurrentPerson(); } public void showCurrentPerson() { Who cur = new Who(); cur = list.get(counter); ImageOf = cur.photo; name.setText(cur.name); } public void Wrong(View view) { if(counter==list.size()) counter = 0 ; else counter++; } public void Right(View view) { point++; if(counter==list.size()) counter = 0 ; else counter++; } 
  • one
    Show the stack trace. - post_zeew

1 answer 1

Here in this place:

 i1.setImageResource(R.drawable.leo); i2.setImageResource(R.drawable.en); 

You are trying to manipulate uninitialized variables.


Well, in general, you have a very strange architecture.

It would be more correct to store in your Who class not an ImageView , but a resource identifier (in this case, images).

  • How to store the resource ID? What type of data? Who (String name,? Photo) What should be instead of '?' ? ; - abay
  • The resource identifier is of type int . - post_zeew
  • If you correct the question "code formatting" then try not only to remove the extra spaces, as well as wrap all the code in {} - Naumov
  • @Naumov; Here, on the contrary, it is necessary to remove the last closing bracket, because the method name is not specified. Or am I misunderstanding something? - post_zeew
  • @post_zeew it’s just not clear that this is a typo or a mistake, so you shouldn’t make such edits - Naumov