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

  • Home
  • SEARCH
  • 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 931909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:34:44+00:00 2026-05-15T20:34:44+00:00

I’m trying to just make an activity that simply loads a random image when

  • 0

I’m trying to just make an activity that simply loads a random image when it’s loaded. It compiles fine and loads fine. But simply doesn’t work. Any ideas what i’m doing wrong? It’s making my eyes bleed.

————Here my RandomImage Class ————————————–

package com.package.name; 

import java.util.Random; 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.ImageView; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.View.OnClickListener; 

public class RandomImage extends Activity implements OnClickListener{ 
    private static final Random rgenerator = new Random(); 
    Integer [] mImageIds = { 
            R.drawable.pictureone, 
            R.drawable.picturetwo, 
            R.drawable.picturethree, 
            }; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.randomimage); 
    Integer q = mImageIds[rgenerator.nextInt(mImageIds.length)]; 
    ImageView iv = (ImageView) findViewById(R.id.imageviewyeah); 
    iv.setTag(q); 
    View nextButton = findViewById(R.id.next_image_button); 
    nextButton.setOnClickListener(this); 
} 

    @Override 
    public void onClick(View v) { 
    switch (v.getId()) { 
    case R.id.next_image_button: 
        Intent i = new Intent(this, RandomImage.class); 
        startActivity(i); 
        break; 
    } 
} 

    @Override 
        public boolean onCreateOptionsMenu (Menu menu) { 
                super.onCreateOptionsMenu(menu); 
                MenuInflater inflater = getMenuInflater(); 
                inflater.inflate(R.menu.menu3, menu); 
                return true; 
        } 
@Override 
public boolean onOptionsItemSelected (MenuItem item) { 
                switch (item.getItemId()) { 
                case R.id.menu: 
                        startActivity(new Intent(this, Main.class)); 
                        return true; 
} 

return false; 
        } 
}

————Here my layout ————————————–

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    android:orientation="vertical" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/widget0" 
android:background="@drawable/nhiebg" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
    android:layout_gravity="center" > 
<ImageView 
        android:id="@+id/imageviewyeah" 
        android:tag="q" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center"> 
</ImageView> 
<Button 
        android:id="@+id/next_image_button" 
        android:text="Next Image" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:padding="10dip" 
        android:typeface="serif"/> 
</LinearLayout>

Thanks you’ve been a huge help, well i tried what you said. But, it just loads a random image on start, the button just does nothing. I press it then the image that was up goes away and nothing loads in its place. A whole bunch of stuff goes thru logcat, but here’s the first two lines

07-17 04:15:33.102: WARN/ResourceType(30476): No package identifier when getting value for resource number 0x00000002
07-17 04:15:33.112: WARN/ImageView(30476): Unable to find resource: 2

—————————-WHAT I HAVE NOW—————————-

public class RandomImage extends Activity implements OnClickListener{


    private Integer [] mImageIds = { 
            R.drawable.one, 
            R.drawable.two, 
            R.drawable.three, 
            };
    private static final Random rgenerator = new Random();

    private ImageView iv;



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


    Integer q = mImageIds[rgenerator.nextInt(mImageIds.length)];

    iv = (ImageView) findViewById(R.id.imageviewyeah);
    iv.setImageResource(q);


    View nextButton = findViewById(R.id.next_image_button);
    nextButton.setOnClickListener(this);

}


    @Override
    public void onClick(View v) {
    switch (v.getId()) {
    case R.id.next_image_button:
        iv.setImageResource(rgenerator.nextInt(mImageIds.length));
        break;
    }

}

I’d also like to point out, i tried removing the lines

Integer q = mImageIds[rgenerator.nextInt(mImageIds.length)];

and the line

and iv.setImageResource(q);

and it still didn’t work

  • 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-15T20:34:45+00:00Added an answer on May 15, 2026 at 8:34 pm

    Instead of iv.setTag(q), try using iv.setImageResource(q).

    Additional improvements:

    Your current method works in the sense that it displays a new random image, but it also ends up needlessly creating a new RandomImage activity, which can eat up memory and processing power (every time the button is pressed).

    Here is a more efficient way; declare iv as an instance variable of your activity:

    private ImageView iv;
    

    and instantiate it in your onCreate() (just remove “ImageView”):

    iv = (ImageView) findViewById(R.id.imageviewyeah);
    

    This means you only have to set it once during your activity. Then, in onClick(), replace

    Intent i = new Intent(this, RandomImage.class); 
    startActivity(i); 
    break; 
    

    with

    iv.setImageResource(mImageIds[rgenerator.nextInt(mImageIds.length)]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.