按照老师的操作下来,程序会崩溃,求解答。。。
package com.xiaoma.myapplication01;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class Main2Activity extends AppCompatActivity {
EditText etUserName,etUserPass;
CheckBox checkBox;
Button btn1;
SharedPreferences mypre;
SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etUserName= (EditText) findViewById(R.id.btnyonghu);
etUserPass= (EditText) findViewById(R.id.btnmima);
checkBox= (CheckBox) findViewById(R.id.che);
btn1= (Button) findViewById(R.id.btndenglu);
mypre=getSharedPreferences("mypre",MODE_PRIVATE);
mypre.edit();
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = etUserName.getText().toString();
String mima = etUserPass.getText().toString();
if ("Amintor".equals(name) && "123456".equals(mima)){
editor.putString("etUserName",name); //只要有这三行代码,运行程序就会崩溃,why?
editor.putString("etUserPass",mima); //只要有这三行代码,运行程序就会崩溃,why?
editor.commit(); //只要有这三行代码,运行程序就会崩溃,why?
Toast.makeText(Main2Activity.this, "登陆成功", Toast.LENGTH_SHORT).show();
} else {
etUserPass.setText("");
etUserName.setText("");
Toast.makeText(Main2Activity.this, "请输入正确的用户名与密码", Toast.LENGTH_SHORT).show();
}
}
});
String name=mypre.getString("name","");
String mima=mypre.getString("mima","");
if(name!=null&&mima!=null&&checkBox.isChecked()){
etUserName.setText(name);
etUserPass.setText(mima);
}
}
}