0

我正在尝试做一个基本的应用程序,一旦您单击按钮,图像就会随机更改为.png我加载的 7 个图像之一。我尝试了几种不同的方法,但似乎无法让它发挥作用。这是我现在的代码:

.xml

<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" >

   <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"/>

   <ImageView
        android:id="@+id/image1"
        android:layout_width="320dp"
        android:layout_height="250dp"
        android:scaleType="fitXY"
        android:contentDescription="@string/black" />

</RelativeLayout>

.java

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]);
    }
}

我在 Eclipse 中的错误消息说:

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo

谢谢你的帮助。

4

3 回答 3

1

试试看,

public class Black extends Activity implements onClickListener{
 ImageView imageView;

 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.black);
     imageView= (ImageView)findViewById(R.id.image1);
     imageView.setOnClickListener(this);
 }
@Override
public void onClick(View v) {

if(v == imgView){

    int[] picture=  

   {
     R.drawable.black,R.drawable.blackairplane,R.drawable.blackbear,R.drawable.blackcircle,R.drawabl    e.blackkite,R.drawable.blacksquare,R.drawable.blacktriangle};
     Random r = new Random();
     int n=r.nextInt(7);
     imageView.setImageResource(picture[n]);
  }
}
}
于 2013-04-26T06:16:09.090 回答
0

尝试这个..

public class Black extends Activity {
 ImageView imageView;

 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.black);
   imageView= (ImageView)findViewById(R.id.image1);
 }


public void onClick() {



    int[] picture=  

   {
     R.drawable.black,R.drawable.blackairplane,R.drawable.blackbear,R.drawable.blackcircle,R.drawabl    e.blackkite,R.drawable.blacksquare,R.drawable.blacktriangle};
     Random r = new Random();
     int n=r.nextInt(7);
     imageView.setImageResource(picture[n]);
  }
}
于 2013-04-26T06:09:07.047 回答
0

尝试这个

Random r=new Random(); r=(int)(Math.random()*7); imageview.setImageResource(picture[r]);

于 2013-04-26T06:18:45.330 回答