2011-06-16 62 views
0

我的網絡服務出現問題。我的佈局包含這些領域按鈕值的空指針異常

username edittextbox 

password edittextbox 

gender button(male), button(female) 

mailid edittextbox 

每當我點擊按鈕的男性對性別的選擇應採取的字符串值作爲男性,女性應該採取女性。我無法寫出確切的代碼,因爲我是新來的android becoz,我得到了logcat中的空指針異常。

我想添加這些字符串值來請求我的web服務的obj將數據發送到服務器,如request.addProperty(「gender」,gender);根據用戶選擇

有人請幫我寫的性別選擇確切的代碼,這樣我就不會在logcat中得到任何NPE

代碼

Button regmalebtn; 
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
public void register() { 
    Log.v(TAG, "Trying to Login"); 

    EditText etxt_user = (EditText) findViewById(R.id.regetfirstname); 
    EditText etxt_pass = (EditText) findViewById(R.id.regetlastname); 
    EditText etxt_dob = (EditText) findViewById(R.id.regetdob); 
    EditText etxt_email = (EditText) findViewById(R.id.regetemail); 
    EditText etxt_password = (EditText) findViewById(R.id.regetpwd); 
    EditText etxt_mobno = (EditText) findViewById(R.id.regetmobno); 
    EditText deviceid = null; 
    String fname = etxt_user.getText().toString(); 
    String lname = etxt_pass.getText().toString(); 
    String dob = etxt_dob.getText().toString(); 
    String email = etxt_email.getText().toString(); 
    String password = etxt_password.getText().toString(); 
    String mobno = etxt_mobno.getText().toString(); 
    String gender= regmalebtn.getText().toString(); 
    int latitude =**; 
    int longitude= **; 
    String device_id= "12345"; 
    // regmalebtn.sette 

// DefaultHttpClient client = new DefaultHttpClient(); 
    //HttpPost httppost = new HttpPost(
    //  "http://....."); 
    SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
      SoapEnvelope.VER11); 

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
    soapEnvelope.dotNet = true; 
    soapEnvelope.setOutputSoapObject(request); 



    //String response1 = request.getProperty(0).toString() ; 
    request.addProperty("fname", fname); 
    request.addProperty("lname", lname); 
    request.addProperty("dateofbirth", dob); 
    request.addProperty("email", email); 
    request.addProperty("password", password); 
    request.addProperty("mobno", mobno); 
    request.addProperty("latitude", latitude); 
    request.addProperty("longitude", longitude); 
    request.addProperty("device_id", device_id); 
    request.addProperty("gender", gender); 
    //request.addProperty("latitude',latitude); 





    try { 
     androidHttpTransport.call(SOAP_ACTION, soapEnvelope); 

     SoapPrimitive resultsRequestSOAP = (SoapPrimitive) soapEnvelope 
       .getResponse(); 
     //SoapObject result = (SoapObject) soapEnvelope.getResponse(); 
     String resultData; 
     resultData = request.getProperty(0).toString(); 
    } catch (Exception e) { 

     e.printStackTrace(); 
    } 

} 

這些都是logcat的錯誤

06-16 11:55:58.955: ERROR/AndroidRuntime(471): FATAL EXCEPTION: Thread-8 
06-16 11:55:58.955: ERROR/AndroidRuntime(471): java.lang.NullPointerException 
06-16 11:55:58.955: ERROR/AndroidRuntime(471):  at com.soap.Register.register(Register.java:125) 
06-16 11:55:58.955: ERROR/AndroidRuntime(471):  at com.soap.Register$1$1.run(Register.java:66) 
06-16 11:55:58.965: WARN/ActivityManager(58): Force finishing activity com.soap/.Register 
+0

它看起來就像你在'com.soap.Register'類中的內部'Runnable'類中有NPE一樣。 – inazaruk 2011-06-16 06:53:39

+0

發佈您的代碼,其中來自TextView的 – ngesh 2011-06-16 06:54:26

+0

您的初始化按鈕,獲取串@inazaruk喜,其實即時通訊呼叫按鈕內部的寄存器方法點擊這樣signin.setOnClickListener(新OnClickListener(){ \t \t \t公共無效的onClick(視圖v) { \t \t \t \t的ShowDialog(0); \t \t 噸=新主題(){ \t \t \t \t \t公共無效的run(){ \t \t \t \t \t \t register(); \t \t \t \t \t} \t \t \t \t}; \t \t \t \t t。開始(); \t \t \t} \t \t}); \t} – Randroid 2011-06-16 06:55:44

回答

4

您需要:

Button regmalebtn = (Button) findViewById(R.id.regetmalebtn); 

或類似的東西。

我不能看到你指定的其他地方你regmalebtn,所以這

String gender= regmalebtn.getText().toString(); 

會導致空指針,因爲你只有這

Button regmalebtn; 

ATM

+0

非常感謝你的回覆,現在沒有錯誤即時消息但我們如何在模擬器上顯示消息是否成功註冊。我們應該使用哪種功能來打印消息或對話框 – Randroid 2011-06-16 07:52:52

+0

我清除了異常,但在logcat中我收到了這條消息06-16 15: 43:41.565:VERBOSE/HELLO(885):試圖登錄 06-16 15:43:43.265:VERBOSE/TAG(885):-1爲什麼我會變成-1 – Randroid 2011-06-16 10:30:06

+0

不知道你得到的錯誤logcat的。你應該檢查吐司([鏈接](http://developer.android.com/reference/android/widget/Toast.html))打印一些簡單的信息。 – OhSnapple 2011-06-16 16:04:36