The problem is this: when you click on ImageButton, the image in ImageView should be replaced

The code seems to be written correctly, but when the application starts it stops immediately after clicking on this ImageButton .

Tell me, please, what is the problem.

My java code is:

 package com.example.killerlii.nedaibogzafeilit; import android.graphics.Bitmap; import android.graphics.Typeface; import android.media.MediaPlayer; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.FocusFinder; import android.view.View; import android.widget.Button; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.ScrollView; import android.widget.TextView; public class main extends AppCompatActivity { TextView t; ImageView image; @Override protected void onCreate(Bundle savedInstanceState) { getSupportActionBar().setDisplayShowHomeEnabled(true); getSupportActionBar().setIcon(R.drawable.ic_logo); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); t = (TextView) findViewById(R.id.nazvanie); Typeface meCustomFont1 = Typeface.createFromAsset(getAssets(), "fonts/LegionaryBold.otf"); t.setTypeface(meCustomFont1); t = (TextView) findViewById(R.id.predpisanie); Typeface meCustomFont2 = Typeface.createFromAsset(getAssets(), "fonts/LegionaryItalic.otf"); t.setTypeface(meCustomFont2); t = (TextView) findViewById(R.id.opisan); Typeface meCustomFont3 = Typeface.createFromAsset(getAssets(), "fonts/Legionary.otf"); t.setTypeface(meCustomFont3); RelativeLayout mainpage = (RelativeLayout) findViewById(R.id.mainpage); ScrollView Opisanie = (ScrollView) findViewById(R.id.Opisanie); final ImageView imageView = (ImageView) findViewById(R.id.ImageView); final TextView nazvanie = (TextView) findViewById(R.id.nazvanie); final TextView predpisanie = (TextView) findViewById(R.id.predpisanie); final TextView opisan = (TextView) findViewById(R.id.opisan); ScrollView Perecheslenie = (ScrollView) findViewById(R.id.Perecheslenie); final ImageButton imageButton = (ImageButton) findViewById(R.id.imageButton); final ImageButton imageButton2 = (ImageButton) findViewById(R.id.imageButton2); final ImageButton imageButton3 = (ImageButton) findViewById(R.id.imageButton3); final ImageButton imageButton4 = (ImageButton) findViewById(R.id.imageButton4); final ImageButton imageButton5 = (ImageButton) findViewById(R.id.imageButton5); final ImageButton imageButton6 = (ImageButton) findViewById(R.id.imageButton6); final ImageButton imageButton7 = (ImageButton) findViewById(R.id.imageButton7); final ImageButton imageButton8 = (ImageButton) findViewById(R.id.imageButton8); final ImageButton imageButton9 = (ImageButton) findViewById(R.id.imageButton9); final ImageButton imageButton10 = (ImageButton) findViewById(R.id.imageButton10); imageButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { MediaPlayer ring= MediaPlayer.create(main.this,R.raw.firsts); ring.start(); nazvanie.setText(getResources().getString(R.string.imageButton1)); predpisanie.setText(getResources().getString(R.string.imageButton1_1)); opisan.setText(getResources().getText(R.string.imageButton1_2)); image.setImageResource(R.drawable.ic_launcher_2); } }); } } 
  • By the way, I advise you not to call the view in the same way and give them almost the same meaningless id. This is your code! And you are doing yourself awkward here and get confused. - Flippy

1 answer 1

The variable ImageView image declared in your code, but it is not initialized :)

Add a line

 image = (ImageView)findViewById(R.id.imageId); 
  • You don’t have prices, thank you very much) I would like to ask a question, but not a drop on this topic. How can I stop a song on this button when I press it again? If you can help here, I will be very grateful - Artem
  • In general, it’s customary to create new questions, this is a completely different problem on a different topic, ask another new question, I’ll be happy to answer :) - Flippy
  • Mark the answer as true by checking the box to the left of it. - Flippy