Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed help displaying randomly chosen image on activity load and button press
    primarykey
    data
    text
    <p>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.</p> <p>------------Here my RandomImage Class --------------------------------------</p> <pre><code>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; } } </code></pre> <p>------------Here my layout --------------------------------------</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;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" &gt; &lt;ImageView android:id="@+id/imageviewyeah" android:tag="q" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"&gt; &lt;/ImageView&gt; &lt;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"/&gt; &lt;/LinearLayout&gt; </code></pre> <hr> <p>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 </p> <p>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</p> <p>----------------------------WHAT I HAVE NOW----------------------------</p> <pre><code>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; } } </code></pre> <p>I'd also like to point out, i tried removing the lines</p> <pre><code>Integer q = mImageIds[rgenerator.nextInt(mImageIds.length)]; </code></pre> <p>and the line</p> <pre><code>and iv.setImageResource(q); </code></pre> <p>and it still didn't work</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload