当我运行Android Studio项目时,显示应用程序不幸停止
以下是activity_main.xml
和MainActivity.java
代码。
activity_main.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/abc_primary_text_material_dark" >
<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/abc_primary_text_disable_only_material_light"
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"
android:onClick="onClick"/>
</RelativeLayout>
MainActivity.java
package com.sudheergv.pndu;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.view.View;
import java.util.*;
public class MainActivity extends Activity {
ImageView imageView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView= (ImageView)findViewById(R.id.image1);
}
public void onClick(View view) {
int[] picture=
{
R.drawable.android3d,R.drawable.android,R.drawable.image1};
Random r = new Random();
int n=r.nextInt(picture.length);
imageView.setImageResource(picture[n]);
}
}
答案 0 :(得分:2)
您应该在View view
方法中添加onClick
;
public void onClick(View view) {
int[] picture = {
R.drawable.android3d,R.drawable.android,R.drawable.image1};
Random r = new Random();
int n=r.nextInt(7);
imageView.setImageResource(picture[n]);
}
你应该像这样给ImageView
drawable
:
<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/abc_primary_text_material_dark" >
<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/abc_primary_text_disable_only_material_light"
android:text="@string/black"
android:onClick="onClick"/>
<ImageView
android:id="@+id/image1"
android:layout_width="320dp"
android:layout_height="250dp"
android:src="@drawable/your_drawable"
android:scaleType="fitXY"
android:contentDescription="@string/black" />
</RelativeLayout>