我正在开发一款测验应用程序。在其中,我将问题和选项显示在列表中。我在顶部放置了一个“下一步”按钮,当点击该按钮时,我会重复调用相同的页面,直到测试结束。
由于某些问题包含图片,因此我在问题下方显示了一个按钮,当单击该按钮时,图像将显示在新页面中。
现在我的问题是,有时当我单击按钮以显示图像时,它会显示“outofmemory”异常。
"image.java"
错误显示在这行代码myimgview.setImageResource(imageResource);。我不知道问题出在哪里。我在drawable文件夹中保存了400张图片,因为我需要在我的项目中使用所有这些图片。请有经验的人帮助我解决这个问题。我正在为此问题苦苦挣扎。非常感谢任何帮助。
"image.java"
public class image extends Activity {
ImageView myimgview;
ImageView buttondismiss;
public static String image_url;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image);
Intent intent = getIntent();
image_url = intent.getStringExtra(List.IMAGE_NAME);
buttondismiss = (ImageView) findViewById(R.id.dismiss1);
myimgview = (ImageView) findViewById(R.id.imageView100);
int in = image_url.indexOf(".");
String index = Integer.toString(in);
String picturename = image_url.substring(0, in);
int imageResource = getResources().getIdentifier(picturename,"drawable", "com.quiz.test"); // getPackageName());
myimgview = (ImageView) findViewById(R.id.imageView100);
myimgview.setImageResource(imageResource);
}
}
日志记录:
08-18 11:11:01.063: E/AndroidRuntime(552): FATAL EXCEPTION: main
08-18 11:11:01.063: E/AndroidRuntime(552): java.lang.OutOfMemoryError
08-18 11:11:01.063: E/AndroidRuntime(552): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
08-18 11:11:01.063: E/AndroidRuntime(552): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:483)
08-18 11:11:01.063: E/AndroidRuntime(552): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
08-18 11:11:01.063: E/AndroidRuntime(552): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:773)
08-18 11:11:01.063: E/AndroidRuntime(552): at android.content.res.Resources.loadDrawable(Resources.java:1937)
08-18 11:11:01.063: E/AndroidRuntime(552): at android.content.res.Resources.getDrawable(Resources.java:664)
08-18 11:11:01.063: E/AndroidRuntime(552): at android.widget.ImageView.resolveUri(ImageView.java:542)
08-18 11:11:01.063: E/AndroidRuntime(552): at android.widget.ImageView.setImageResource(ImageView.java:315)
08-18 11:11:01.063: E/AndroidRuntime(552): at com.quiz.test.image.onCreate(image.java:48)
08-18 11:11:01.063: E/AndroidRuntime(552): at android.app.Activity.performCreate(Activity.java:4465)
08-18 11:11:01.063: E/AndroidRuntime(552): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-18 11:11:01.063: E/AndroidRuntime(552): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
08-18 11:11:01.063: E/AndroidRuntime(552): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
08-18 11:11:01.063: E/AndroidRuntime(552): at android.app.ActivityThread.access$600(ActivityThread.java:122)
08-18 11:11:01.063: E/AndroidRuntime(552): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
08-18 11:11:01.063: E/AndroidRuntime(552): at android.os.Handler.dispatchMessage(Handler.java:99)
08-18 11:11:01.063: E/AndroidRuntime(552): at android.os.Looper.loop(Looper.java:137)
08-18 11:11:01.063: E/AndroidRuntime(552): at android.app.ActivityThread.main(ActivityThread.java:4340)
08-18 11:11:01.063: E/AndroidRuntime(552): at java.lang.reflect.Method.invokeNative(Native Method)
08-18 11:11:01.063: E/AndroidRuntime(552): at java.lang.reflect.Method.invoke(Method.java:511)
08-18 11:11:01.063: E/AndroidRuntime(552): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-18 11:11:01.063: E/AndroidRuntime(552): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-18 11:11:01.063: E/AndroidRuntime(552): at dalvik.system.NativeStart.main(Native Method)
错误显示在这行代码myimgview.setImageResource(imageResource);。我不知道问题出在哪里。我在drawable文件夹中保存了400张图片,因为我需要在我的项目中使用所有这些图片。请有经验的人帮助我解决这个问题。我正在为此问题苦苦挣扎。非常感谢任何帮助。