Note that there are some explanatory texts on larger screens.

plurals
  1. POChange image on click of button
    primarykey
    data
    text
    <p>I'm trying to do a basic app where once you click the button the image changes randomly to one of the 7 <code>.png</code>images I have loaded. I've tried several different ways but can't seem to get it to work. Here's my code as of now:</p> <p><strong>.xml</strong></p> <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/RelativeLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:background="@color/black" &gt; &lt;Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="40dp" android:background="@color/white" android:text="@string/black" android:onClick="onClick"/&gt; &lt;ImageView android:id="@+id/image1" android:layout_width="320dp" android:layout_height="250dp" android:scaleType="fitXY" android:contentDescription="@string/black" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p><strong>.java</strong></p> <pre><code>public class Black extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.black); } public void onClick() { ImageView imageView = (ImageView)findViewById(R.id.image1); int[] picture={R.drawable.black,R.drawable.blackairplane,R.drawable.blackbear,R.drawable.blackcircle,R.drawable.blackkite,R.drawable.blacksquare,R.drawable.blacktriangle}; Random r = new Random(); int n=r.nextInt(7); imageView.setImageResource(picture[n]); } } </code></pre> <p>My error message in Eclipse says:</p> <pre><code>java.lang.RuntimeException: Unable to instantiate activity ComponentInfo </code></pre> <p>Thanks for any help.</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.
 

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