按钮值的空指针异常

时间:2011-06-16 06:48:33

标签: android web-services

我的网络服务出了问题。我的布局包含这些字段

username  edittextbox

password  edittextbox

gender   button(male), button(female)

mailid   edittextbox

每当我点击性别选项的男性按钮时,它应该将字符串值视为男性,而对于女性则应该采用女性。我无法编写确切的代码,因为我是android becoz的新手,我在logcat中得到空指针异常。

我想添加这些字符串值来请求我的webservices的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

1 个答案:

答案 0 :(得分:4)

你需要一个:

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

或类似的东西。

我无法看到你在其他地方指定你的regmalebtn的位置,所以这个

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

因为你只有这个

会导致空指针
Button regmalebtn;

大气压