Sahand.Setoodeh
Sahand.Setoodeh

Reputation: 41

shared preferences : float

something is wrong with it

package com.sahandxx3.Countries;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

/**
 * Created by Setude on 17-Dec-15.
 */
public class Setting extends Activity {
    SharedPreferences sh;
    TextView txtflag,txt_test;
    SeekBar sbflag;
    int progress = 10;
    float size=10;
    float txt_size;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting);
        init();
        txtflag.setText("Flag size : "+sbflag.getProgress());

        sbflag.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                txt_size=sh.getFloat("size",0);
                progress= i;
                size=(progress*5)+5;
                txtflag.setText("Flag size : "+progress);
                txt_test.setTextSize(size);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

                txtflag.setText("Flag size : "+progress);
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                txtflag.setText("Flag size : "+progress);
                sh=PreferenceManager.getDefaultSharedPreferences(Setting.this);
                SharedPreferences.Editor edit=sh.edit();
                edit.putFloat("size",size);
                edit.commit();
            }
        });
    }
    private void init() {
        txtflag= (TextView) findViewById(R.id.txtflag);
        sbflag= (SeekBar) findViewById(R.id.sbflag);
        txt_test= (TextView) findViewById(R.id.btn_test);
    }
}

so my problem is with this line:

 txt_size=sh.getFloat("size",0);

When I try to change the seekbar,It stops working.

then I want to use txt_size in txt_test();

Upvotes: 0

Views: 326

Answers (4)

BJDM
BJDM

Reputation: 101

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.setting);
    sh=PreferenceManager.getDefaultSharedPreferences(this);
}

Upvotes: 0

Hiren Patel
Hiren Patel

Reputation: 52800

Put below code on onCreate():

sh=PreferenceManager.getDefaultSharedPreferences(Setting.this);

Your onCreate() method should looks like below:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting);
sh=PreferenceManager.getDefaultSharedPreferences(Setting.this);

.. 
.. 

}

Hope this will help you.

Upvotes: 2

Vishal Gadhiya
Vishal Gadhiya

Reputation: 598

I think you should use

 float size=10f;
 txt_size=sh.getFloat("size",0f);

Upvotes: 0

Yury Fedorov
Yury Fedorov

Reputation: 14938

My best guess is that your SharedPreferences instance sh is null at the moment you try to access it. Try to move sh=PreferenceManager.getDefaultSharedPreferences(Setting.this); to your onCreate().

Upvotes: 2

Related Questions