1

5 つの異なるアクティビティでユーザーからテキストと数値 (10 進数) を受け取り、共有設定に保存する Android アプリを作成しました。editor(); を使用してすべての値を復元できます。過去5回の活動すべてで。

最終的な計算アクティビティでは、すべてのユーザー入力 (テキストと数値) を復元し、それらを string 変数と double 変数に格納しています。以下の投稿されたコードアクティビティで計算を実行します。

問題

getText を使用してテキストビューで結果を表示できません。

何か足りないものはありますか?

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();
                }
            };
        });

    }
}
4

3 に答える 3

0

Integer.parseInt(String)を使用して小数値を取得することはありません

これを行う

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

  a = String.valueOf(abcPref.getString("tA", ""));
于 2013-09-15T06:25:36.797 に答える
0

試す -

int k = Integer.parseInt(abcPref.getString("tsK", ""));
于 2013-09-15T06:08:30.017 に答える
0

文字列変数と xml レイアウトを確認してください

于 2013-09-16T07:12:08.980 に答える