java.lang.ClassCastException:java.lang.String无法强制转换为java.lang.Integer

时间:2017-03-01 11:33:00

标签: java android

我尝试在按钮点击时在共享偏好中保存图像的资源ID,然后尝试将该残差设置为图像。但我得到“字符串无法转换为整数”错误。我的问题可能很愚蠢,但我想解决它。我将非常感谢。我的代码在这里。

b4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int res=R.drawable.kiss;
            image.setImageResource(res);
            editor.putInt(getString(R.string.keyimage1),res);
            editor.apply();
        }
    });

如果密钥不存在,则为默认值 int defaultimageres=R.mipmap.ic_launcher;

错误我在下面排队。

int storedImageres=sharedPreferences.getInt(getString(R.string.keyimage1),defaultimageres);

在这里我将resId设置为图像

image.setImageResource(storedImageres);

logcat的:

FATAL EXCEPTION: main

              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pra1.savingdatasample/com.example.pra1.savingdatasample.MainActivity}: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
                  at android.app.ActivityThread.access$1100(ActivityThread.java:221)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:158)
                  at android.app.ActivityThread.main(ActivityThread.java:7225)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
               Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
                  at android.app.SharedPreferencesImpl.getInt(SharedPreferencesImpl.java:242)
                  at com.example.pra1.savingdatasample.MainActivity.onCreate(MainActivity.java:47)
                  at android.app.Activity.performCreate(Activity.java:6876)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) 
                  at android.app.ActivityThread.access$1100(ActivityThread.java:221) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:158) 
                  at android.app.ActivityThread.main(ActivityThread.java:7225) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

这是我的onCreate方法:我知道它的完整混乱:P

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    b1=(Button)findViewById(R.id.button);
    b2=(Button)findViewById(R.id.button2);
    b3=(Button)findViewById(R.id.button3);
    b4=(Button)findViewById(R.id.button4);
    ed1=(EditText)findViewById(R.id.editText1);
    ed2=(EditText)findViewById(R.id.editText2);
    tv1=(TextView)findViewById(R.id.textView);
    tv2=(TextView)findViewById(R.id.textView2);
    image=(ImageView)findViewById(R.id.imageView);

    SharedPreferences sharedPreferences=this.getPreferences(Context.MODE_PRIVATE);
    final SharedPreferences.Editor editor=sharedPreferences.edit();
    int defaultimageres=R.mipmap.ic_launcher;
    String defaultvaluetext1=getResources().getString(R.string.text1);
    String storedtext1=sharedPreferences.getString(getString(R.string.keytextview1),defaultvaluetext1);
    int storedImageres=sharedPreferences.getInt(getString(R.string.keyimage1),defaultimageres);
    image.setImageResource(storedImageres);
    tv1.setText(storedtext1);

    String defaultvaluetext2=getResources().getString(R.string.text2);
    String storedtext2=sharedPreferences.getString(getString(R.string.keytextview2),defaultvaluetext2);
    tv2.setText(storedtext2);
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          String text1=ed1.getText().toString();
            if(text1.equals(null)||text1.equals("")){
               Toast.makeText(getApplicationContext(),"empty text",Toast.LENGTH_SHORT).show();
            }else {
                tv1.setText(text1);
                editor.putString(getString(R.string.keytextview1), text1);
                editor.apply();
                ed1.setText("");
                InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                manager.hideSoftInputFromWindow(ed1.getWindowToken(), 0);
            }
        }
    });
    b2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          String text2= ed2.getText().toString();
            if(text2.equals(null)||text2.equals("")){
                Toast.makeText(getApplicationContext(),"empty text",Toast.LENGTH_SHORT).show();
            }
            else {
                tv2.setText(text2);
                editor.putString(getString(R.string.keytextview2), text2);
                editor.apply();
                ed2.setText("");
                InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                manager.hideSoftInputFromWindow(ed2.getWindowToken(), 0);
            }
        }
    });
    b3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int res=R.drawable.rtl;
            image.setImageResource(res);
            editor.putInt(getString(R.string.keyimage1),res);
            editor.commit();
        }
    });
    b4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int res=R.drawable.kiss;
            image.setImageResource(res);
            editor.putInt(getString(R.string.keyimage1),res);
            editor.commit();
        }
    });
}

1 个答案:

答案 0 :(得分:0)

嗨朋友感谢您的评论,但我自己解决了。我将int转换为String并使用editor.putString方法

进行尝试 onclick方法中的

代码:

 b4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int res=R.drawable.kiss;
            String resnew=String.valueOf(res);
            image.setImageResource(res);
            editor.putString(IMAGE_KEY,resnew);
            editor.apply();
        }
    });

这是我的关键

 private static final String IMAGE_KEY="com.SavingDataSample.image";

这些是变化

    int defaultimageres=R.mipmap.ic_launcher;
    String deafultimageValue=String.valueOf(defaultimageres);

    String storedImageres=sharedPreferences.getString(IMAGE_KEY,deafultimageValue);
    image.setImageResource(Integer.parseInt(storedImageres));