SharedPreferences crashes my app in Studio

I want to use SharedPreferences to save settings but it crashes my app! I had used it once in Eclipse.

    SharedPreferences prefs= PreferenceManager.getDefaultSharedPreferences(this);
    String font =prefs.getString("font", "");
    int size =prefs.getInt("size", 10);
    int space =prefs.getInt("space", 7);

    final TextView txt = (TextView) findViewById(R.id.textView5);
    SeekBar seeksize = (SeekBar) findViewById(R.id.seekBar1);
    SeekBar fasele = (SeekBar) findViewById(R.id.seekBar2);

    seeksize.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStartTrackingTouch(SeekBar arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
            // TODO Auto-generated method stub
            //txt1.setTextSize(arg1);
            txt.setTextSize(arg1);
        }
    });

    fasele.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStartTrackingTouch(SeekBar arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
            // TODO Auto-generated method stub
            //txt1.setTextSize(arg1);
            txt.setLineSpacing(arg1,1);
        }
    });
    final Typeface zar = Typeface.createFromAsset(getAssets(), "font/BZAR.TTF");
    final Typeface nazanin = Typeface.createFromAsset(getAssets(), "font/BNAZANIN.TTF");
    final Typeface kodak = Typeface.createFromAsset(getAssets(), "font/BKOODKBD.TTF");

    RadioButton rb_nazanin = (RadioButton) findViewById(R.id.radio0);
    RadioButton rb_zar = (RadioButton) findViewById(R.id.radio1);
    RadioButton rb_kodak = (RadioButton) findViewById(R.id.radio2);


    rb_nazanin.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @SuppressLint("NewApi")
        @Override
        public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
            // TODO Auto-generated method stub
            if (arg1 == true)
            {
                txt.setTypeface(nazanin);
                SharedPreferences shared = getSharedPreferences("Prefs", MODE_PRIVATE);
                SharedPreferences.Editor editor = shared.edit();
                editor.putString("font", "nazanin");
                editor.apply();


            }
        }
    });

    rb_zar.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
            // TODO Auto-generated method stub
            if (arg1 == true)
            {
                txt.setTypeface(zar);
                SharedPreferences shared = getSharedPreferences("Prefs", MODE_PRIVATE);
                SharedPreferences.Editor editor = shared.edit();
                editor.putString("font", "zar");
                editor.apply();
            }
        }
    });

    rb_kodak.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
  // TODO Auto-generated method stub
            if (arg1 == true)
            {
                txt.setTypeface(kodak);
                SharedPreferences shared = getSharedPreferences("Prefs", MODE_PRIVATE);
                SharedPreferences.Editor editor = shared.edit();
                editor.putString("font", "kodak");
                editor.apply();
            }
        }
    });

Logcat errors

03-17 02:24:46.113 1700-1700/com.example.material.testnavi E/AndroidRuntime: FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.material.testnavi/com.example.material.testnavi.MainTan}: java.lang.RuntimeException: native typeface cannot be made at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) at android.app.ActivityThread.access$600(ActivityThread.java:141) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5041) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560 ) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.RuntimeException: native typeface cannot be made at android.graphics.Typeface.(Typeface.java:175) at android.graphics.Typeface.createFromAsset(Typeface.java:149) at com.example.material.testnavi.MainTan.onCreate(MainTan.java:79) at android.app.Activity.performCreate(Activity.java:5104) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) at android.app.ActivityThread.access$600(ActivityThread.java:141) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5041) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) at dalvik.system.NativeStart.main(Native Method)


I think I might assume what your error is (without the log, I might be wrong), but you're getting the values from the defaultSharedPrefs:

SharedPreferences prefs= PreferenceManager.getDefaultSharedPreferences(this);

And setting them in another one:

                SharedPreferences shared = getSharedPreferences("Prefs", MODE_PRIVATE);

Which is probably why none of the values you retrieve are good. Replace this last line with:

SharedPreferences prefs= PreferenceManager.getDefaultSharedPreferences(this);

Use the same preferences for getting and setting values.

链接地址: http://www.djcxy.com/p/31438.html

上一篇: 致命错误:主要。 editText等于在android studio中登录

下一篇: SharedPreferences在Studio中崩溃我的应用程序