无法访问我的共享偏好并计算

时间:2013-09-15 06:04:11

标签: android sharedpreferences

我制作了一个Android应用程序,其中我在5个不同的活动中从用户接收文本和数字(十进制)值,然后将它们存储在我的共享偏好中。我可以使用editor()恢复所有值;在之前的所有5项活动中。

现在

在最终计算活动中,我恢复所有用户输入(文本和数字)并将它们存储在字符串和双变量中。在下面公布的代码活动中执行计算;

问题

我无法使用getText在textview中查看结果。

有什么遗漏?任何帮助将受到高度赞赏..

public class Xcalculateforall extends Activity {

    Button qcbut, coatsolbut;
    TextView TVqcbut, TVcoatsolbut, TVbrand;
    double tdrum, rqctotal;
    String a;
    public static String FILE1 = "MyPrefsFile";
    SharedPreferences abcPref;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.resultmain);

        qcbut = (Button) findViewById(R.id.bQCS);
        coatsolbut = (Button) findViewById(R.id.bCSP);

        TVqcbut = (TextView) findViewById(R.id.tvQCS);
        TVbrand = (TextView) findViewById(R.id.tvBrand);
        TVcoatsolbut = (TextView) findViewById(R.id.tvCSP);

        abcPref = this.getSharedPreferences(FILE1, 0);

        a = abcPref.getString("tA", ""); //receives text value and store in 'a' String

        double k = Integer.parseInt(abcPref.getString("tsK", ""));
        double l = Integer.parseInt(abcPref.getString("tsL", ""));
        double m = Integer.parseInt(abcPref.getString("tsM", ""));
        double n = Integer.parseInt(abcPref.getString("tsN", ""));
        double o = Integer.parseInt(abcPref.getString("tsO", ""));
        double p = Integer.parseInt(abcPref.getString("tsP", ""));
        tdrum = 20 / m;
        double samv = (tdrum / k) / l;
        double samv2 = (Math.sqrt(samv));
        double tsample = ((samv2 + 1) * k) * l;
        double rmicro = tsample * n;
        double rchem = tsample * o;
        double rother = tsample * p;
        double rinqc = rmicro + rchem + rother;
        rqctotal = rinqc - 2;

        qcbut.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                TVqcbut.setText(rqctotal + "Days");
                TVbrand.setText("" + a);
                // Intent results = new Intent("com.traviss.calculate.RESULT");
                // startActivity(results);
            }
        });

    }
}

更新查询---我接受用户输入的上一个活动

public class G2J extends Activity {
    Button two2five, save2;

    EditText edtG, edtH, edtI, edtJ, edtK;
    int tG, tH, tI, tJ, tK;
    String tsG, tsH, tsI, tsJ, tsK;
    public static String FileP2 = "MyPrefsFile";
    SharedPreferences abcPref;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.g2j);
        two2five = (Button) findViewById(R.id.btp2);

        edtG = (EditText) findViewById(R.id.etG);
        edtH = (EditText) findViewById(R.id.etH);
        edtI = (EditText) findViewById(R.id.etI);
        edtJ = (EditText) findViewById(R.id.etJ);
        edtK = (EditText) findViewById(R.id.etK);

        abcPref = getSharedPreferences(FileP2, 0);
        edtG.setText(abcPref.getString("tsG", ""));
        edtH.setText(abcPref.getString("tsH", ""));
        edtI.setText(abcPref.getString("tsI", ""));
        edtJ.setText(abcPref.getString("tsJ", ""));
        edtK.setText(abcPref.getString("tsK", ""));

        two2five.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if ((!edtG.getText().toString().equals(""))
                        && (!edtH.getText().toString().equals(""))
                        && (!edtI.getText().toString().equals(""))
                        && (!edtJ.getText().toString().equals(""))
                        && (!edtK.getText().toString().equals(""))) {
                    abcPref = G2J.this.getSharedPreferences(FileP2, 0);
                    SharedPreferences.Editor editor = abcPref.edit();
                    editor.putString("tsG", edtG.getText().toString());
                    editor.putString("tsH", edtH.getText().toString());
                    editor.putString("tsI", edtI.getText().toString());
                    editor.putString("tsJ", edtJ.getText().toString());
                    editor.putString("tsK", edtK.getText().toString());
                    editor.commit();
                    Toast message = Toast.makeText(G2J.this, "Values are saved", 2000);
                    message.setGravity(Gravity.BOTTOM, 0, 0);
                    message.show();
                    Intent openl2p = new Intent("com.traviss.calculate.L2P");
                    startActivity(openl2p);
                }
                else {
                    Toast failz = Toast.makeText(G2J.this,
                            "Values are not Entered", 2000);
                    failz.setGravity(Gravity.BOTTOM, 0, 0);
                    failz.show();
                }
            };
        });

    }
}

3 个答案:

答案 0 :(得分:0)

尝试 -

int k = Integer.parseInt(abcPref.getString("tsK", ""));

答案 1 :(得分:0)

您不会使用 Integer.parseInt(String)

获取小数值

这样做

 double k = Double.parseDouble(abcPref.getString("tsK", ""));

  a = String.valueOf(abcPref.getString("tA", ""));

答案 2 :(得分:0)

检查字符串变量和xml布局