1

这是我的代码。我想将我的字符串保存到共享首选项中。

private String sla;
private String b;
private String c;
//EditText aa=(EditText)findViewById(R.id.et1);
EditText aa2=(EditText)findViewById(R.id.et2);
EditText aa3=(EditText)findViewById(R.id.et3);
SharedPreferences settings = getSharedPreferences ("PREF_DEMO", 0);

 /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.setup);

         RadioGroup rdg=(RadioGroup)findViewById(R.id.ragp);

         rdg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                public void onCheckedChanged(RadioGroup group, final int checkedId) {
                    switch (checkedId) {
                        case R.id.radio0:
                            sla = "au";
                            Toast.makeText(group.getContext(), "Auto", Toast.LENGTH_LONG).show();
                            break;
                        case R.id.radio1:
                             sla = "ma";
                             Toast.makeText(group.getContext(), "Manual", Toast.LENGTH_LONG).show();
                            break;
                        }
                };
            });
         final Button set = (Button) findViewById(R.id.nn);
         set.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 // Perform action on click
                // a=aa.getText().toString();
                 b=aa2.getText().toString();
                 c=aa3.getText().toString();
                 SharedPreferences.Editor editor = settings.edit();

                 editor.putString("sp", b);
                 editor.putString("tp", c);
                 editor.putString("op", sla);
                 editor.commit();
                 Toast.makeText(v.getContext(), "saved", Toast.LENGTH_LONG).show();
             }
         });

所以,我在“EditText aa3=(EditText)findViewById(R.id.et3);”行的 logcat 中遇到了一个异常。这段代码没有解析错误。

4

1 回答 1

1

在 setContentView(R.layout.main) 之后添加这一行因为之前无法从布局 xml 文件中获得参考

  EditText aa2=(EditText)findViewById(R.id.et2); 
  EditText aa3=(EditText)findViewById(R.id.et3);

添加喜欢....

  EditText aa2;
  EditText aa3;

 public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.setup);

         aa2=(EditText)findViewById(R.id.et2);
         aa3=(EditText)findViewById(R.id.et3);
于 2012-04-25T13:53:53.243 回答