erences将backgroundresource设置为Ima

erences将backgroundresource设置为Ima

本文介绍了Android-从SharedPreferences将backgroundresource设置为ImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从sharedpreferences中将BackgroundResource设置为ImageView.

I am trying to set a BackgroundResource to a ImageView from a sharedpreferences..

我的代码是

活动1中按钮的点击事件

On Click Event of Button from Activity 1

    SharedPreferences settings;
            SharedPreferences.Editor editor;

            settings = getSharedPreferences(PREFS_NAME, 0);
            editor = settings.edit();
            editor.putInt(GameScreen_SliderPuzzle1.key_image_id, R.drawable.image_3);

            editor.commit();

活动2

SharedPreferences image_display;
    String image_id;

    image_display = context.getSharedPreferences(GameScreen_SliderPuzzle1.PREFS_NAME, Context.MODE_MULTI_PROCESS);
    image_id = image_display.getString("key_image_id", "");
    imageview.setBackgroundResource(image_id);

我的问题是sharedpreferences的值是字符串,而 setBackgroundResource 的值是Int我试图将字符串转换为int.但是应用程序崩溃了. TypeCast错误

my problem is sharedpreferences value is in string and setBackgroundResource value is IntI tried to convert string o int. but application crashes. TypeCast Error

推荐答案

尝试

image_id = image_display.getInt("key_image_id", 0);

因为您需要int而不是String.

as you need int not String.

这篇关于Android-从SharedPreferences将backgroundresource设置为ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 04:08