2012-07-07 132 views
0

我正在嘗試在共享首選項中保存用戶名。我使用Web服務從數據庫獲取用戶名和密碼,並且完美地工作。問題在於將用戶名保存在共享首選項中。我已經給出了下面的代碼。我已經評論問題出現的地方。我得到以下異常:在android中使用共享首選項存儲用戶名

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(); 
} 

}

回答

2

我沒有看到你做連接到一個視圖CB(複選框)?

你正在得到一個空指針,因爲cb什麼也沒有。你還沒有找到它的視角,但它只是一個成員,就是這樣。由於這也是你得到你的空指針的地方,所以這幾乎肯定是出了什麼問題。

+0

不能相信我錯過了它。非常感謝。深夜編碼不是一個好主意 – 2012-07-07 22:25:02

+0

沒問題。用來隨時發生在我身上:P。玩的開心。 – FabianCook 2012-07-07 22:30:36

1

CheckBox cb未設置!使用cb = (CheckBox) findViewById(R.id.somewhat);