会话类的java.lang.NullPointerException

时间:2014-05-07 04:41:48

标签: java android session login java.lang.class

我正在为登录类的会话管理创建一个session.class,但我的logcat显示错误java.lang.NullPointerException并且应用程序无法在模拟器上运行,请帮助

这是我的session.java:

public class Session {

    private SharedPreferences prefs;
    Editor editor = prefs.edit();

    public Session(Context cntx) {
        // TODO Auto-generated constructor stub
        prefs = PreferenceManager.getDefaultSharedPreferences(cntx);

    }

    public void setkdanggota(String kdanggota) {
        editor.putString("kdanggota", kdanggota).commit();
        editor.commit();
    }

    public String getusename() {
        String kdanggota = prefs.getString("kdanggota",null);
        return kdanggota;
    }
}

这是我的login.java:

public class login extends Activity{
    EditText kode,pw;
    TextView error;
    Button login;
    String i;
    private Session session;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        Context cntx = getApplicationContext();
        session = new Session(cntx );
        kode = (EditText) findViewById(R.id.kode);
        pw = (EditText) findViewById (R.id.password);
        login = (Button) findViewById (R.id.login);
        error = (TextView) findViewById (R.id.error);
        login.setOnClickListener(new View.OnClickListener(){
        @Override     
            public void onClick(View v) {
            ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
            postParameters.add(new BasicNameValuePair("kode", kode.getText().toString()));
            postParameters.add(new BasicNameValuePair("password", pw.getText().toString()));
            String response = null; 
            try { 
                response = CustomHttpClient.executeHttpPost("http://10.0.2.2/koperasidb/login.php", postParameters);
                String res = response.toString(); 
                res = res.trim();
                res = res.replaceAll("\\s+","");
                error.setText(res);
                if (res.equals("1")){
                    error.setText("Correct Username or Password");
                    session.setkdanggota(kode.getText().toString());
                    berhasil(v);  
                }
                else { 
                    error.setText("Sorry!! Wrong Username or Password Entered");
                }
            }
            catch (Exception e) {
                kode.setText(e.toString());
            }
            }
        });
    }

            public void berhasil (View theButton)
            {
                Intent s = new Intent (this, Home.class);
                startActivity(s);
            }
        }

1 个答案:

答案 0 :(得分:1)

当你这样做时,pref是未初始化的/ null,因此编辑器被设置为null。

Editor editor = prefs.edit();

所以在初始化prefs之后将编辑器初始化移动到构造函数应该有帮助

改变这个:

private SharedPreferences prefs;
    Editor editor = prefs.edit();

    public Session(Context cntx) {
        // TODO Auto-generated constructor stub
        prefs = PreferenceManager.getDefaultSharedPreferences(cntx);

    }

private SharedPreferences prefs;
    Editor editor;

    public Session(Context cntx) {
        // TODO Auto-generated constructor stub
        prefs = PreferenceManager.getDefaultSharedPreferences(cntx);
       editor = prefs.edit();

    }