会话类上的java.lang.NullPointerException

会话类上的java.lang.NullPointerException,java,android,session,login,java.lang.class,Java,Android,Session,Login,Java.lang.class,我正在为登录类的会话管理创建session.class,但我的logcat显示错误java.lang.NullPointerException,并且应用程序无法在emulator上运行,请提供帮助 下面是我的session.java: public class Session { private SharedPreferences prefs; Editor editor = prefs.edit(); public Session(Context cntx) {

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

下面是我的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);
            }
        }
公共类登录扩展活动{
EditText kode,pw;
文本视图错误;
按钮登录;
第i串;
非公开会议;
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
Context cntx=getApplicationContext();
会话=新会话(cntx);
kode=(EditText)findViewById(R.id.kode);
pw=(EditText)findViewById(R.id.password);
login=(按钮)findviewbyd(R.id.login);
error=(TextView)findViewById(R.id.error);
login.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
ArrayList后参数=新的ArrayList();
添加(新的BasicNameValuePair(“kode”,kode.getText().toString());
添加(新的BasicNameValuePair(“密码”,pw.getText().toString());
字符串响应=null;
试试{
响应=CustomHttpClient.executeHttpPost(“http://10.0.2.2/koperasidb/login.php“,后参数);
String res=response.toString();
res=res.trim();
res=res.replaceAll(“\\s+”,”);
错误.setText(res);
如果(相对等于(“1”)){
错误.setText(“正确的用户名或密码”);
session.setkdanggota(kode.getText().toString());
贝哈西尔(v);
}
否则{
error.setText(“对不起!!输入了错误的用户名或密码”);
}
}
捕获(例外e){
kode.setText(e.toString());
}
}
});
}
public void berhasil(查看按钮)
{
意向s=新意向(此,Home.class);
星触觉;
}
}

执行此操作时,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);

    }


也发布您的日志猫。@user3488298很乐意帮助您。如果这对你有帮助,请点击我答案左边的勾号接受答案。
private SharedPreferences prefs;
    Editor editor;

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

    }