我正在尝试制作一个在加载时仅加载随机图像的活动。它编译得很好并且加载得很好。但根本行不通。任何想法我做错了什么?它让我的眼睛流血。
------------这是我的 RandomImage 类 ---------------------------------- ----
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;
}
}
------------这是我的布局----------------------------------- ---
<?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>
谢谢你的帮助很大,我试过你说的。但是,它只是在开始时加载一个随机图像,按钮什么也不做。我按下它,然后上升的图像消失了,没有任何东西加载到它的位置。一大堆东西通过 logcat,但这是前两行
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);
它仍然没有工作