Java 在android中使用共享首选项存储用户名

Java 在android中使用共享首选项存储用户名,java,android,nullpointerexception,sharedpreferences,Java,Android,Nullpointerexception,Sharedpreferences,我正在尝试将用户名保存在共享首选项中。我使用一个web服务从数据库中获取用户名和密码,它工作得非常好。问题在于在共享首选项中保存用户名。我已经给出了下面的代码。我已经评论了问题出现的地方。我得到以下例外情况: java.lang.NullPointerException 我找不出有什么问题。请帮忙 ///登录类//// public class Login extends Activity{ private static final String SOAP_ACTION = "http

我正在尝试将用户名保存在共享首选项中。我使用一个web服务从数据库中获取用户名和密码,它工作得非常好。问题在于在共享首选项中保存用户名。我已经给出了下面的代码。我已经评论了问题出现的地方。我得到以下例外情况:

   java.lang.NullPointerException
我找不出有什么问题。请帮忙

///登录类////

public class Login extends Activity{

private static final String SOAP_ACTION = "http://tempuri.org/checkLogin";
private static final String OPERATION_NAME = "checkLogin";
private static final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
private static final String SOAP_ADDRESS = "http://10.0.2.2:54714/WebSite1/Service.asmx";

Button sqllogin;
EditText sqlusername, sqlpassword;
TextView tvData1;
CheckBox cb;

 @Override
    protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    sqlusername = (EditText) findViewById(R.id.etuname1);
    sqlpassword = (EditText) findViewById(R.id.etpass);
    tvData1 = (TextView)findViewById(R.id.textView1); 
    sqllogin = (Button) findViewById(R.id.bLogin);


    sqllogin.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            switch (v.getId()){
            case R.id.bLogin:
            String username = sqlusername.getText().toString();
            String password = sqlpassword.getText().toString();
            SoapObject Request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
            Request.addProperty("uname", String.valueOf(username));
            Request.addProperty("pwd", String.valueOf(password));

                    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
                    envelope.dotNet = true;
                    envelope.setOutputSoapObject(Request);
                    HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);

                   try  {                    
                    httpTransport.call(SOAP_ACTION, envelope);                       
                    SoapObject result = (SoapObject) envelope.bodyIn;
                    int response =  Integer.parseInt(result.getProperty(0).toString());
                    if(response == 2)//Response is 2 when username and password valid
                        {
                        tvData1.setText("You have logged in successfully"); 
                        savePrefs("CHECKBOX",cb.isChecked()); //This line and the following 3 lines is where the problem is.
                        if (cb.isChecked())
                        {
                            savePrefs("NAME",sqlusername.getText().toString());
                        }
                        Intent openhomepage = new Intent("com.android.disasterAlertApp.HOME");
                        startActivity(openhomepage);
                        }
                    else
                        {
                        tvData1.setText("Invalid Username or password"); 
                        }
                    }  catch (Exception exception)   {
                                  tvData1.setText(exception.toString());                    
                    }
            break;

        }

        }
    });
}

     private void savePrefs(String key, boolean value){
     SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); 
     Editor edit = sp.edit();
     edit.putBoolean(key, value);
     edit.commit();
 }

 private void savePrefs(String key,String value){
     SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); 
     Editor edit = sp.edit();
     edit.putString(key, value);
     edit.commit(); 
 }

}

我看不出您在何处将cb(复选框)附加到视图


您将得到一个空指针,因为cb什么都不是。您还没有找到它的视图,它只是一个成员,仅此而已。因为这也是你得到空指针的地方,这几乎肯定是出了什么问题

我看不出您在哪里将cb(复选框)附加到视图


您将得到一个空指针,因为cb什么都不是。您还没有找到它的视图,它只是一个成员,仅此而已。因为这也是你得到空指针的地方,这几乎肯定是出了什么问题

未设置复选框cb!使用
cb=(复选框)findviewbyd(R.id.d)

未设置复选框cb!使用
cb=(复选框)findviewbyd(R.id.d)

真不敢相信我错过了。谢谢。深夜编码不是一个好问题。过去总是发生在我身上:P。玩得开心。真不敢相信我错过了。谢谢。深夜编码不是一个好问题。经常发生在我身上:P.玩得开心。