Я делаю простое приложение для Android, которое сохраняет имя пользователя и адрес электронной почты, используя общие настройки. Но проблема в том, что всякий раз, когда я объявляю общие настройки, приложение вылетает. Когда я удаляю код общих настроек, приложение работает нормально.
Может ли кто-нибудь увидеть проблему?
Вот мой код:
public class PreferencesActivity extends Activity implements OnClickListener {
private TextView textUserName;
private TextView textEmail;
private String userName;
private String email;
public static final String MyPREFERENCES = "MyPrefs" ;
SharedPreferences sharedPref;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedPref = this.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
setContentView(R.layout.activity_preferences);
textUserName = (TextView)findViewById(R.id.txtUserName);
userName = textUserName.getText().toString();
textEmail = (TextView)findViewById(R.id.txtEmail2);
email = textEmail.getText().toString();
Button saveButton = (Button)findViewById(R.id.btnSave);
saveButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Editor editor = sharedPref.edit();
if(v.getId() == R.id.btnSave) {
editor.putString(userName, email);
editor.commit();
}
}
}
РЕДАКТИРОВАТЬ
Логкат:
10-30 20:41:25.246: E/AndroidRuntime(2797): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lab4ex1preferencesactivity/com.example.lab4ex1preferencesactivity.Prefe rencesActivity}: java.lang.ClassCastException: android.widget.EditText cannot be cast to android.widget.Button
btnSave
вactivity_preferences.xml
имеет типButton
, а неEditText
? - person Lasse Magnussen   schedule 30.10.2014