I am trying to just do an operation that just loads a random image on boot. It compiles fine and loads fine. But just doesn’t work. Any ideas what I'm doing wrong? It makes my eyes bleed.
------------ Here is 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 is 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>
, , , . , . , , , , . ,
07-17 04: 15: 33.102: WARN/ResourceType (30476): 0x00000002
07-17 04: 15: 33.112: WARN/ImageView (30476): : 2
---------------------------- --------------- -------------
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;
}
}
,
Integer q = mImageIds[rgenerator.nextInt(mImageIds.length)];
and iv.setImageResource(q);