Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6778721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:17:59+00:00 2026-05-26T16:17:59+00:00

I am new to android, I am in a completing stage in creating one

  • 0

I am new to android, I am in a completing stage in creating one application.
But I unfortunately some error gets raised when running it in the android phone, but working perfectly in android emulator…

Here is my java code:

package com.VRG;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Vector;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Animalsgame extends Activity implements OnClickListener {
    // int answer=0;
    Button voice;
    MediaPlayer mp;
    TextView time;// count down timer
    ImageView img_view1;
    ImageView img_view2;
    ImageView img_view3;
    ImageView img_view4;
    private ImageView selectedImageView;
    static int index = 0;
    static int index1 = 0;
    static int noOfLapse;
    static int count;
    static int score = 1;
    static boolean isSoundPlay;
    CountDownTimer CountDownTimer;
    int idx;
    Integer next;

    private Random random;
    private ArrayList<MediaPlayer> sounds;
    private ArrayList<MediaPlayer> tempSounds;
    // Track the sound which is playing
    private int soundSelectedIndex;

    private Vector matchImages = null;
    List<Integer> generated;

    // Total number of image boxes

    private static final int NO_IMAGE_BOXES = 4;

    final int[] imageViews = { R.id.img_view1, R.id.img_view2, R.id.img_view3,
            R.id.img_view4 };// Array of image views

    int[] Animals_images = { R.drawable.cow, R.drawable.lion, R.drawable.dog,
            R.drawable.sheep, R.drawable.elephant, R.drawable.camel,
            R.drawable.rhinos, R.drawable.horse, R.drawable.donkey,
            R.drawable.wolf }; // Array of Animal images

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);

        time = (TextView) this.findViewById(R.id.time);

        img_view1 = (ImageView) this.findViewById(R.id.img_view1);
        img_view2 = (ImageView) this.findViewById(R.id.img_view2);
        img_view3 = (ImageView) this.findViewById(R.id.img_view3);
        img_view4 = (ImageView) this.findViewById(R.id.img_view4);

        img_view1.setOnClickListener(this);
        img_view2.setOnClickListener(this);
        img_view3.setOnClickListener(this);
        img_view4.setOnClickListener(this);

        // Count down timer
        voice = (Button) this.findViewById(R.id.voice);// Animals sounds

        voice.setOnClickListener(this);

        // Initialise Lapse counter
        noOfLapse = 0;
        isSoundPlay = true;

        random = new Random();

        sounds = new ArrayList<MediaPlayer>();
        sounds.add(MediaPlayer.create(this, R.raw.cow));
        sounds.add(MediaPlayer.create(this, R.raw.lion));
        sounds.add(MediaPlayer.create(this, R.raw.dog));
        sounds.add(MediaPlayer.create(this, R.raw.sheep));
        sounds.add(MediaPlayer.create(this, R.raw.elephant));
        sounds.add(MediaPlayer.create(this, R.raw.camel));
        sounds.add(MediaPlayer.create(this, R.raw.rhino));
        sounds.add(MediaPlayer.create(this, R.raw.horse));
        sounds.add(MediaPlayer.create(this, R.raw.donkey));
        sounds.add(MediaPlayer.create(this, R.raw.wolf));

        tempSounds = (ArrayList<MediaPlayer>) sounds.clone();

        enableDisableImageView(false);

    }// End of Oncreate method

    public void setDuration(int duration) {

    }

    @Override
    public void onClick(View v) {
        Log.i("voice", "voice button clicking");
        enableDisableImageView(true);

        if (v instanceof ImageView) {

            count++;
            // Toast.makeText(getApplicationContext(), "Wrong",
            // Toast.LENGTH_SHORT).show();
            enableDisableImageView(false);

        }

        if (v == selectedImageView) {
            Toast.makeText(getApplicationContext(), "Correct!",
                    Toast.LENGTH_SHORT).show();
            score++;
            Log.i("11111", "count value:" + score);
            Log.i("22222", "count value:" + count);
            enableDisableImageView(false);

        }

        if (v == img_view1) {

            if (mp != null) {
                if (mp.isPlaying()) {
                    mp.stop();
                }
            }

            time.setText("");
            if (CountDownTimer != null) {
                CountDownTimer.cancel();
            }
            enableDisableButton(true);
        }
        if (v == voice) {

            // answer++;
            freezeButtonAndPlaySoundThenUnfreezeButton();
            loadImages();
            countDown();
            // enableDisableButton(true);
        }

        if (v == img_view2) {
            if (mp != null) {
                if (mp.isPlaying()) {
                    mp.stop();
                }
            }

            enableDisableButton(true); // Re-enable buttons
        }
        // else if (v == voice) {
        // loadImages();
        // countDown();
        // enableDisableButton(false);
        // }

        if (v == img_view3) {
            if (mp != null) {
                if (mp.isPlaying()) {
                    mp.stop();
                }
            }

            enableDisableButton(true); // Re-enable buttons
        }
        // else if (v == voice) {
        // loadImages();
        // countDown();
        // enableDisableButton(false);
        // }

        if (v == img_view4) {
            if (mp != null) {
                if (mp.isPlaying()) {
                    mp.stop();
                }
            }

            enableDisableButton(true); // Re-enable buttons
        }
        // else if (v == voice) {
        // loadImages();
        // countDown();
        // enableDisableButton(false);
        // }

        if (count > 8) {
            AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
            setDuration(0);
            mp = MediaPlayer.create(this, R.raw.claps);
            if (mp != null) {
                if (mp.isPlaying()) {
                    mp.stop();
                }
            }
            mp.start();
            Toast.makeText(getApplicationContext(), "Your Score : " + score,
                    Toast.LENGTH_SHORT).show();
            // set the message to display
            alertbox.setMessage("Game Over..!" + " " + "Your score is:" + " "
                    + score + " " + " Do you want to play again?");
            // set a positive/yes button and create a listener
            alertbox.setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {

                        // do something when the button is clicked
                        public void onClick(DialogInterface arg0, int arg1) {
                            if (mp != null) {
                                if (mp.isPlaying()) {
                                    mp.stop();
                                }
                            }
                            index = 0;
                            index1 = 0;
                            count = 0;
                            score = 1;
                            Intent i = new Intent(Animalsgame.this,
                                    Animalsgame.class);
                            startActivity(i);
                            finish();
                        }
                    });

            // set a negative/no button and create a listener
            alertbox.setNegativeButton("No",
                    new DialogInterface.OnClickListener() {

                        // do something when the button is clicked
                        public void onClick(DialogInterface arg0, int arg1) {
                            if (mp != null) {
                                if (mp.isPlaying()) {
                                    mp.stop();
                                }
                            }
                            count = 0;
                            score = 0;
                            finish();
                            // Intent i = new Intent(VRGActivity2.this,
                            // VRGActivity1.class);
                            // startActivity(i);

                        }
                    });

            // display box
            alertbox.show();
            enableDisableButton(false);
        }
    }// End of voice on click

    private void countDown() {

        CountDownTimer = new CountDownTimer(6000, 10000) {

            public void onTick(long millisUntilFinished) {
                if (isSoundPlay) {
                    // freezeButtonAndPlaySoundThenUnfreezeButton();
                    isSoundPlay = false;
                }
                time.setText("" + "" + millisUntilFinished / 10000);

            }

            public void onFinish() {

                // time.setText("UP");
                setDuration(0);
                // Toast.makeText(getApplicationContext(), "Time Up",
                // Toast.LENGTH_SHORT).show();

                isSoundPlay = true;

            }// End of on finish
        }.start(); // End of countDown Constructor
    }// End of countDown method

    public void enableDisableButton(Boolean state) {

        voice.setEnabled(state);

    }// End of enableDisableButton(Boolean state)

    public void enableDisableImageView(Boolean state) {

        img_view1.setEnabled(state);
        img_view2.setEnabled(state);
        img_view3.setEnabled(state);
        img_view4.setEnabled(state);

    }// End of enableDisableImageView(Boolean state)

    private void loadImages() {

        Random rng = new Random(); // Random function

        if (generated == null) {
            generated = new ArrayList<Integer>();
        } else {
            generated.clear();
        }

        Log.i(this.toString(), "soundSelectedIndex " + soundSelectedIndex);
        generated.add(soundSelectedIndex);
        Log.i(this.toString(), "Initial Array Count " + generated.size());
        Boolean flag;

        for (int i = 0; i < NO_IMAGE_BOXES - 1; i++) {
            Log.i(this.toString(), "For i " + i);
            flag = true;
            while (flag) {
                next = rng.nextInt(9);
                index = next;
                if (!generated.contains(next)) {

                    Log.i(this.toString(), "Generated Array Contents " + next);
                    generated.add(next);
                    // ImageView iv = (ImageView) findViewById(imageViews[i]);
                    // iv.setImageResource(Animals_images[next]);
                    flag = !flag;

                }// End of if

                // break;

            }// While loop

        }// End of for loop

        // Set the last image

        // Choose the random position

        int pos = rng.nextInt(3);
        int temp = generated.get(0);
        generated.set(0, generated.get(pos));
        generated.set(pos, temp);
        selectedImageView = (ImageView) findViewById(imageViews[pos]);
        for (int i = 0; i < generated.size(); i++) {
            Log.i("Array Element in the list", "   " + generated.get(i));
            ImageView iv = (ImageView) findViewById(imageViews[i]);

            iv.setImageResource(Animals_images[generated.get(i)]);

        }

    }// End of loadImages()

    public void freezeButtonAndPlaySoundThenUnfreezeButton() {
        enableDisableButton(false); // disable buttons
        int randomInt = random.nextInt(tempSounds.size());

        // Set the selected sounds

        Log.i(this.toString(), "Random Generated Sounds " + randomInt);

        // soundSelectedIndex = randomInt;
        mp = tempSounds.get(randomInt);

        // if (sounds.contains(tempSounds.get(randomInt))){

        // Get the actual index from the sounds list

        soundSelectedIndex = sounds.indexOf(tempSounds.get(randomInt));

        Log.i(this.toString(), "freezeButtonAndPlaySoundThenUnfreezeButton "
                + soundSelectedIndex);

        // }

        tempSounds.remove(randomInt);

        mp.seekTo(0);
        mp.start();
        mp.setOnCompletionListener(new OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mpalmost) {

            }// End of on completion method

        });// End of set on click listener method

    }// End of freezeButtonAndPlaySoundThenUnfreezeButton method

    @Override
    // while back pressed the task is refreshed
    public void onBackPressed() {

        if (mp != null) {
            if (mp.isPlaying()) {
                mp.stop();
            }// End of if (mp.isPlaying())

        }// End of if (mp != null)

    }// End of back pressed

    @Override
    protected void onPause() {

        super.onPause();

    }// End of on pause method

    @Override
    protected void onStart() {

        super.onStart();

    }// End of on start method

    @Override
    protected void onResume() {

        super.onResume();

    }// End of on resume method

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        onBackPressed();
        finish();

        return true;

    }// End of onKeyDown method

    // // Define method for choosing matching image
    //
    // void chooseImages() {
    //
    // }// End of choosing images
    //
    // // Define method for getting random values
    //
    // int getRandom(int value) {
    //
    // if (random == null) {
    //
    // random = new Random();
    //
    // }
    //
    // return random.nextInt(value);
    //
    // }// End of getRandom(int value) method

}// End of Animalsgame activity



I am using one sound button, and four image views. When i click the sound button, the sound get raised and simultaneously, imageviews are filled with images. But in phone when i run it. it shows error in some instance, when i click the sound button. 

My part of java coding, in which i am working is mentioned above..  

The error which i am getting is **Null pointer expection** and the error pointed out is in freezeButtonAndPlaySoundThenUnfreezeButton. Plz do me a needful..!
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:17:59+00:00Added an answer on May 26, 2026 at 4:17 pm

    Im guessing that you are missing the sounds (mp variable is null on line 394). And you shouldn’t random between 0 and int randomInt = random.nextInt(tempSounds.size()); because there is no element and location tempSounds.size(). It should be (tempSounds.size()-1).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am creating a new android application.i am using the table layout. I have
When creating a new Android application, Eclipse complains Package name must have at least
i'm a new android developer. I have an application installed in phone. This application
My new Android app is crashing when I run it on my phone, but
I am a new android developer. I am creating a map view application Where
I am new android app developer i want make app for tablets and phone
I'm new to Android development and I'm currently going through some tutorials. When I
I'm pretty new to android, and just finished setup my environment and reading some
Using my new Android phone established a small problem: I'm living in country A
When creating a new android project, the build target has two choices: Android 2.2,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.