Android Studio 3.1.3 recognizes as "null my images"

1

When I try to reference an image from the code of an activity I get the problem:

  

java.lang.RuntimeException: Unable to start activity   ComponentInfo {com.example.deybin.figuremapsearching / com.example.deybin.figuremapsearching.MainActivity}:   java.lang.NullPointerException: Attempt to invoke virtual method 'void   android.widget.ImageView.setImageResource (int) 'on a null object   reference

This is the main problem:

  

Attempt to invoke virtual method 'void   android.widget.ImageView.setImageResource (int) 'on a null object   reference

I would really appreciate any response

I have my Image View, and I also have it linked to my xml.

I suspect that my problem is: Compatibility or maybe depending on the place in the code where you referred them.

I will not include the code that you request because I consider it has no purpose, thank you very much

    
asked by Deybi Cocluso 22.10.2018 в 02:52
source

3 answers

1

Actually the problem is not the images, it is the view which does not exist, to get the reference of the view, in this case the ImageView , this does not exist therefore you can not load the images using setImageResource() :

ImageView.setImageResource(int)

You may not be getting the ImageView reference through findViewById() or the layout where you are looking for the reference, there is no ImageView .

Add how you get the reference and your layout to expand the answer.

    
answered by 22.10.2018 в 22:58
0

Check the layout that loads the activity that is correct, since the error that shows you

  

java.lang.NullPointerException: Attempt to invoke virtual method 'void   android.widget.ImageView.setImageResource (int) 'on a null object   reference

Tells you that the object is null, so you can not operate with it.

As an alternative, I see that you have just upgraded android studio

File\Invalidate Caches and Restart and recompile

    
answered by 23.10.2018 в 23:44
0

ImageView probelma;

@Oberride Oncreate (....... { { Problem = (ImageView) findViewbyId (R.id.iVisormagen); }

Public void unaFuncion (View view) {    While (condition) {       If (condition) {}       else {           switch (value) {              Case (1): {                     Probelma.setImageResource (R.drawable.laimagen);                     // I've also tried the setImageDrawable                     // Getting the same exception.

               // en mi carpeta( res/drawables) tengo mi png "laimagen".

This is the context in which I received the problem.

Thank you very much to all who have responded. I regret that I did not include the code from the beginning. I added the schema

    
answered by 24.10.2018 в 20:34