I decided to write a program that displays a specific image by pressing a certain button, but the application crashes when this button is pressed. How can this be fixed?

package com.example.fando.trenazher; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.ImageView; public class game1 extends AppCompatActivity { Button buttonQ; Button buttonW; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_game1); buttonQ = (Button) findViewById(R.id.button4); buttonW = (Button) findViewById(R.id.button5); buttonQ.setOnClickListener(new View.OnClickListener() { public void onClick(View View) { ImageView imageView = (ImageView) findViewById(R.id.image); imageView.setImageResource(R.drawable.petuh); } }); buttonW.setOnClickListener(new View.OnClickListener() { public void onClick(View View) { ImageView imageView = (ImageView) findViewById(R.id.image); imageView.setImageResource(R.drawable.lion); } }); }} 

in the logs the error is described as

 11-07 15:23:35.819 25682-25682/com.example.fando.trenazher E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.fando.trenazher, PID: 25682 java.lang.NullPointerException at com.example.fando.trenazher.game1$2.onClick(game1.java:30) at android.view.View.performClick(View.java:4438) at android.view.View$PerformClick.run(View.java:18422) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method) 

ZY Do not judge strictly, I'm just learning (:

  • Perhaps you should look into the log about this error and, preferably, put the necessary part here? - ahgpoug
  • one
    Just launched at home and does not crash. Indeed it is better to run step by step and identify the error. During the testing process, it is advisable to look at the console or in the Android Monitor, there the Wednesday will even tell you which line has crashed and why - Andrew Romanenko
  • Check the availability of resources corresponding to the identifiers that you have in the code. - post_zeew 8:39
  • in layout (R.layout.activity_game1) is there an ImageView with id image ??? most likely it is not, the point is this - Suleymanovtat
  • Yes, it would be great to see another layout. - dramf

1 answer 1

Try this

 // принять переменную ImageView private ImageView mImageView; // Связать ImageView с идентификатором вашего XML в mImageView = (ImageView)findViewById(R.id.mImageView); // Набор ресурсов для ImageView mImageView.setImageResource(R.drawable.your_image_name); 
  • First, you need to look for the view in the onCreate method. To do this, for example, in the CL - Flippy