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 8065055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:33:08+00:00 2026-06-05T11:33:08+00:00

I am trying to emulate dice using ImageView. When I click the button, nothing

  • 0

I am trying to emulate dice using ImageView. When I click the button, nothing seems to happen. I have hard coded this example to replace the image with imageView4 for debugging purposes (I was making sure the random wasn’t fail). Now the application is crashing when I run it on a 2.2 vanilla emulator. Can anyone point out what I am doing wrong? I am new to Java, Eclipse and Android so I’m sure I’ve probably made more than one mistake.

Java:

package mobile.applications.yahztee4;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;

public class Yahtzee4Activity extends Activity {
    /** Called when the activity is first created. */

    ImageButton dice1, dice2, dice3, dice4, dice5;
    Button roll, begin;

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

        PlayGame();

    }

    public void PlayGame()  
    {  
        dice1 = (ImageButton)findViewById(R.id.btndice1);  

        begin = (Button)findViewById(R.id.btnroll);
        roll = (Button)findViewById(R.id.btnbegin);

        begin.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Random rand = new Random();     
                int rndInt = rand.nextInt(6) + 1; // Random number between 1 and 6

                String imgName = "die" + rndInt;

                int id = getResources().getIdentifier(imgName, "drawable", getPackageName());  
                dice1.setImageResource(id);                 
            }
        });  
    }  
}

XML for the button:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" 
    android:baselineAligned="false" >

    <TableLayout
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="1.13" >


        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView5"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="1&apos;s" />

            <TextView
                android:id="@+id/textView1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="TextView" />

            <TextView
                android:id="@+id/textView9"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="3 of Kind" />

            <TextView
                android:id="@+id/textView10"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="TextView" />

        </TableRow>


        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="2&apos;s" />

            <TextView
                android:id="@+id/textView6"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="TextView" />


            <TextView
                android:id="@+id/textView11"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="4 of Kind" />

            <TextView
                android:id="@+id/textView12"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="TextView" />
        </TableRow>


        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView3"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="3&apos;s" />

            <TextView
                android:id="@+id/textView7"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="TextView" />

            <TextView
                android:id="@+id/textView13"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Full House" />

            <TextView
                android:id="@+id/textView14"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="TextView" />
        </TableRow>


        <TableRow
            android:id="@+id/tableRow4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView4"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="4&apos;s" />

            <TextView
                android:id="@+id/textView8"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="TextView" />

            <TextView
                android:id="@+id/textView15"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Sm Straight" />

            <TextView
                android:id="@+id/textView16"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="TextView" />
        </TableRow>


         <TableRow
             android:id="@+id/tableRow5"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content" >

             <TextView
                 android:id="@+id/textView17"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:layout_weight="1"
                 android:text="5&apos;s" />

             <TextView
                 android:id="@+id/textView18"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:layout_weight="1"
                 android:text="TextView" />

             <TextView
                 android:id="@+id/textView19"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:layout_weight="1"
                 android:text="Lg Straight" />

             <TextView
                 android:id="@+id/textView20"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:layout_weight="1"
                 android:text="TextView" />
         </TableRow>

         <TableRow
             android:id="@+id/tableRow6"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content" >

             <TextView
                 android:id="@+id/textView21"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:layout_weight="1"
                 android:text="6&apos;s" />

             <TextView
                 android:id="@+id/textView22"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:layout_weight="1"
                 android:text="TextView" />

             <TextView
                 android:id="@+id/textView23"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:layout_weight="1"
                 android:text="Yahtzee" />

             <TextView
                 android:id="@+id/textView24"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:layout_weight="1"
                 android:text="TextView" />
         </TableRow>

         <TableRow
             android:id="@+id/tableRow7"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content" >

             <TextView
                 android:id="@+id/textView25"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:layout_weight="1"
                 android:text="Bonus (35)" />

             <TextView
                 android:id="@+id/textView26"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:layout_weight="1"
                 android:text="TextView" />

             <TextView
                 android:id="@+id/textView27"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:layout_weight="1"
                 android:text="Chance" />

             <TextView
                 android:id="@+id/textView28"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:layout_weight="1"
                 android:text="TextView" />
         </TableRow>

         <TableRow
             android:id="@+id/tableRow8"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content" >

             <TextView
                 android:id="@+id/textView29"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:layout_weight="1"
                 android:text="Upper Score" />

             <TextView
                 android:id="@+id/textView30"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:layout_weight="1"
                 android:text="TextView" />

             <TextView
                 android:id="@+id/textView31"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:layout_weight="1"
                 android:text="Lower Score" />

             <TextView
                 android:id="@+id/textView32"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:layout_weight="1"
                 android:text="TextView" />
         </TableRow>

         <TableRow
             android:id="@+id/tableRow9"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content" >

             <TextView
                 android:id="@+id/textView33"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:layout_weight="1"
                 android:text="Yz Bonus" />

             <TextView
                 android:id="@+id/textView34"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:layout_weight="1"
                 android:text="TextView" />

             <TextView
                 android:id="@+id/textView35"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:layout_weight="1"
                 android:text="Total Score" />

             <TextView
                 android:id="@+id/textView36"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:layout_weight="1"
                 android:text="TextView" />
         </TableRow>

         <TableRow
             android:id="@+id/tableRow10"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content" >

             <TextView
                 android:id="@+id/textView37"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:layout_weight="1"
                 android:text="Turn#" />

             <TextView
                 android:id="@+id/textView38"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:layout_weight="1"
                 android:text="TextView" />

             <TextView
                 android:id="@+id/textView39"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:layout_weight="1"
                 android:text="Total Score" />

             <TextView
                 android:id="@+id/textView40"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:layout_weight="1"
                 android:text="Roll #" />
         </TableRow>

         <TableRow
              android:id="@+id/tableRow11"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:gravity="center" >

              <ImageView
                  android:id="@+id/btndice1"
                  android:layout_width="60dip"
                  android:layout_height="70dip"
                  android:padding="2dip" />              
              <ImageView
                  android:id="@+id/btndice2"
                  android:layout_width="60dip"
                  android:layout_height="70dip"
                  android:padding="2dip" />

              <ImageView
                  android:id="@+id/btndice3"
                  android:layout_width="60dip"
                  android:layout_height="70dip"
                  android:padding="2dip" />

              <ImageView
                  android:id="@+id/btndice4"
                  android:layout_width="60dip"
                  android:layout_height="70dip"
                  android:padding="2dip" />

              <ImageView
                  android:id="@+id/btndice5"
                  android:layout_width="60dip"
                  android:layout_height="70dip"
                  android:padding="2dip" />

        </TableRow>

          <Button
              android:id="@+id/btnroll"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/roll" 
          />

          <Spinner
              android:id="@+id/spinner"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:prompt="@string/score_prompt_spinner" />

          <Button
              android:id="@+id/btnbegin"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/begin" />

    </TableLayout>


</LinearLayout>

Error Log File

06-10 18:21:31.674: D/AndroidRuntime(705): Shutting down VM
06-10 18:21:31.674: W/dalvikvm(705): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-10 18:21:31.684: E/AndroidRuntime(705): FATAL EXCEPTION: main
06-10 18:21:31.684: E/AndroidRuntime(705): java.lang.RuntimeException: Unable to start activity ComponentInfo{michelle.harris.yahztee4/michelle.harris.yahztee4.Yahtzee4Activity}: java.lang.ClassCastException: android.widget.ImageView
06-10 18:21:31.684: E/AndroidRuntime(705):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-10 18:21:31.684: E/AndroidRuntime(705):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-10 18:21:31.684: E/AndroidRuntime(705):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-10 18:21:31.684: E/AndroidRuntime(705):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-10 18:21:31.684: E/AndroidRuntime(705):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-10 18:21:31.684: E/AndroidRuntime(705):  at android.os.Looper.loop(Looper.java:123)
06-10 18:21:31.684: E/AndroidRuntime(705):  at android.app.ActivityThread.main(ActivityThread.java:4627)
06-10 18:21:31.684: E/AndroidRuntime(705):  at java.lang.reflect.Method.invokeNative(Native Method)
06-10 18:21:31.684: E/AndroidRuntime(705):  at java.lang.reflect.Method.invoke(Method.java:521)
06-10 18:21:31.684: E/AndroidRuntime(705):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-10 18:21:31.684: E/AndroidRuntime(705):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-10 18:21:31.684: E/AndroidRuntime(705):  at dalvik.system.NativeStart.main(Native Method)
06-10 18:21:31.684: E/AndroidRuntime(705): Caused by: java.lang.ClassCastException: android.widget.ImageView
06-10 18:21:31.684: E/AndroidRuntime(705):  at michelle.harris.yahztee4.Yahtzee4Activity.PlayGame(Yahtzee4Activity.java:28)
06-10 18:21:31.684: E/AndroidRuntime(705):  at michelle.harris.yahztee4.Yahtzee4Activity.onCreate(Yahtzee4Activity.java:22)
06-10 18:21:31.684: E/AndroidRuntime(705):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-10 18:21:31.684: E/AndroidRuntime(705):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-10 18:21:31.684: E/AndroidRuntime(705):  ... 11 more
  • 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-06-05T11:33:10+00:00Added an answer on June 5, 2026 at 11:33 am

    You’re doing nothing to the view itself. You are currently creating a new ImageView object, and assign the drawable to it, but you’re not displaying it anywhere.
    Either in onCreate get your ImageView by findViewById(..) or in the onMyButtonClick you should be able to cast the View you’re getting as parameter to ImageView and then change the drawable / resource depending on your “rndInt”.

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

Sidebar

Related Questions

I'm trying to emulate this graph: If I have a correlation matrix how can
I have a link that i am trying to emulate a click on. I
I am trying to emulate click events in Swing using following code: event =
I am trying to emulate a software keyboard using an Activity with Theme.Dialog. The
I'm trying to emulate the Safari / Chrome tabs using CSS (without using tables).
I am trying to emulate the Maps 'Share Location' via email functionality. I have
I'm trying to emulate the behavior of this drag n drop found here How
I'm using String.format() in Java trying to emulate the printf() control channel available in
I'm trying to emulate what growl does with an app that I have. At
Is it possible to run a MySQL query using jQuery? I'm trying to emulate

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.