Android:开关盒的致命异常

发布于 2024-11-16 06:10:43 字数 5048 浏览 4 评论 0原文

我在 register() 方法上方添加了一个开关盒,我收到致命异常,如果我删除开关盒,它工作正常。如果我在 register 方法中使用 onclicklistner 它也对我有用,但我想实现 switch case,我该怎么做?

提前致谢...!

这是我的代码。

Thread t;
ProgressDialog dialog;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button signin = (Button) findViewById(R.id.regsubmitbtn);

    signin.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            showDialog(0);
            t = new Thread() {
                public void run() {
                    register();
                }
            };
            t.start();
        }
    });
}

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case 0: {
        dialog = new ProgressDialog(this);
        dialog.setMessage("Please wait while connecting...");
        dialog.setIndeterminate(true);
        dialog.setCancelable(true);
        return dialog;
    }
    }
    return null;
}


        Button signin = (Button) findViewById(R.id.regsubmitbtn);

    signin.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            showDialog(0);
            t = new Thread() {
                public void run() {
                    register();
                }
            };
            t.start();
        }
    });
}


        String gender; 
    Button regmalebtn = (Button) findViewById(R.id.regmalebtn);
      Button regfemalebtn = (Button) findViewById(R.id.regfemalebtn);
         // Here in the above line I m getting the error//
      public void onClick(View v) {

          switch(v.getId()){

          case R.id.regmalebtn:

          gender = regmalebtn.getText().toString();
            gender.equals("M");
        //  request.addProperty("gender",gender );

               break;

          case R.id.regfemalebtn: 

             gender = regfemalebtn.getText().toString();
                gender.equals("F");
            //  request.addProperty("gender", gender);
               break;
          default:
              break;

          }
          } 


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); 
             // in the above line i m getting error//
      EditText etxt_email = (EditText) findViewById(R.id.regetemail); 
      EditText  etxt_password = (EditText) findViewById(R.id.regetpwd);
      EditText  etxt_confirmpassword = (EditText) findViewById(R.id.regetrepwd);
      EditText  etxt_mobno = (EditText) findViewById(R.id.regetmobno);
          Button regmalebtn = (Button) findViewById(R.id.regmalebtn);
                 // in the above line I m getting the error//
       Button regfemalebtn = (Button) findViewById(R.id.regfemalebtn);
    //  String deviceid = null;
      String fname = etxt_user.getText().toString();
      String lname = etxt_pass.getText().toString();
      String dob = etxt_dob.getText().toString(); 
      String contact = etxt_mobno.getText().toString();


      String password;
      String confirmpassword ;
      String email =  etxt_email.getText().toString(); 
      password =  etxt_password.getText().toString(); 
      confirmpassword =  etxt_confirmpassword.getText().toString();



     final SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    SoapSerializationEnvelope soapEnvelope = new  SoapSerializationEnvelope(SoapEnvelope.VER12); 
                // in the above line I m getting errors//      
        soapEnvelope.dotNet = true;
       soapEnvelope.setOutputSoapObject(request);
              // in the above line i m getting error
       HttpTransportSE aht = new HttpTransportSE(URL);

这些是我的 logcat 消息

 06-21 18:59:34.435: ERROR/AndroidRuntime(1164): FATAL EXCEPTION: main
 06-21 18:59:34.435: ERROR/AndroidRuntime(1164): java.lang.RuntimeException:
 Unable to instantiate activity ComponentInfo{com.soap/com.soap.Register}:  
   java.lang.NullPointerException
              06-21 18:59:34.435: ERROR/AndroidRuntime(1164):     
     ERROR/AndroidRuntime(1164): at  android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
           ERROR/AndroidRuntime(1164): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
           ERROR/AndroidRuntime(1164): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
            ERROR/AndroidRuntime(1164): at android.os.Handler.dispatchMessage(Handler.java:99) 
           ERROR/AndroidRuntime(1164): at android.os.Looper.loop(Looper.java:123) 
           ERROR/AndroidRuntime(1164): at android.app.ActivityThread.main(ActivityThread.java:4627)
           ERROR/AndroidRuntime(1164): at java.lang.reflect.Method.invokeNative(Native Method)
           ERROR/AndroidRuntime(1164): at java.lang.reflect.Method.invoke(Method.java:521)

I added a switch case above the register() method and I am getting Fatal exception and If i remove the switch case its working fine. If I go for onclicklistner inside the register method its also working for me but I want to implement the switch case, how can I do this?

Thanks in advance...!

this is my code.

Thread t;
ProgressDialog dialog;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button signin = (Button) findViewById(R.id.regsubmitbtn);

    signin.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            showDialog(0);
            t = new Thread() {
                public void run() {
                    register();
                }
            };
            t.start();
        }
    });
}

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case 0: {
        dialog = new ProgressDialog(this);
        dialog.setMessage("Please wait while connecting...");
        dialog.setIndeterminate(true);
        dialog.setCancelable(true);
        return dialog;
    }
    }
    return null;
}


        Button signin = (Button) findViewById(R.id.regsubmitbtn);

    signin.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            showDialog(0);
            t = new Thread() {
                public void run() {
                    register();
                }
            };
            t.start();
        }
    });
}


        String gender; 
    Button regmalebtn = (Button) findViewById(R.id.regmalebtn);
      Button regfemalebtn = (Button) findViewById(R.id.regfemalebtn);
         // Here in the above line I m getting the error//
      public void onClick(View v) {

          switch(v.getId()){

          case R.id.regmalebtn:

          gender = regmalebtn.getText().toString();
            gender.equals("M");
        //  request.addProperty("gender",gender );

               break;

          case R.id.regfemalebtn: 

             gender = regfemalebtn.getText().toString();
                gender.equals("F");
            //  request.addProperty("gender", gender);
               break;
          default:
              break;

          }
          } 


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); 
             // in the above line i m getting error//
      EditText etxt_email = (EditText) findViewById(R.id.regetemail); 
      EditText  etxt_password = (EditText) findViewById(R.id.regetpwd);
      EditText  etxt_confirmpassword = (EditText) findViewById(R.id.regetrepwd);
      EditText  etxt_mobno = (EditText) findViewById(R.id.regetmobno);
          Button regmalebtn = (Button) findViewById(R.id.regmalebtn);
                 // in the above line I m getting the error//
       Button regfemalebtn = (Button) findViewById(R.id.regfemalebtn);
    //  String deviceid = null;
      String fname = etxt_user.getText().toString();
      String lname = etxt_pass.getText().toString();
      String dob = etxt_dob.getText().toString(); 
      String contact = etxt_mobno.getText().toString();


      String password;
      String confirmpassword ;
      String email =  etxt_email.getText().toString(); 
      password =  etxt_password.getText().toString(); 
      confirmpassword =  etxt_confirmpassword.getText().toString();



     final SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    SoapSerializationEnvelope soapEnvelope = new  SoapSerializationEnvelope(SoapEnvelope.VER12); 
                // in the above line I m getting errors//      
        soapEnvelope.dotNet = true;
       soapEnvelope.setOutputSoapObject(request);
              // in the above line i m getting error
       HttpTransportSE aht = new HttpTransportSE(URL);

these r my logcat messages

 06-21 18:59:34.435: ERROR/AndroidRuntime(1164): FATAL EXCEPTION: main
 06-21 18:59:34.435: ERROR/AndroidRuntime(1164): java.lang.RuntimeException:
 Unable to instantiate activity ComponentInfo{com.soap/com.soap.Register}:  
   java.lang.NullPointerException
              06-21 18:59:34.435: ERROR/AndroidRuntime(1164):     
     ERROR/AndroidRuntime(1164): at  android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
           ERROR/AndroidRuntime(1164): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
           ERROR/AndroidRuntime(1164): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
            ERROR/AndroidRuntime(1164): at android.os.Handler.dispatchMessage(Handler.java:99) 
           ERROR/AndroidRuntime(1164): at android.os.Looper.loop(Looper.java:123) 
           ERROR/AndroidRuntime(1164): at android.app.ActivityThread.main(ActivityThread.java:4627)
           ERROR/AndroidRuntime(1164): at java.lang.reflect.Method.invokeNative(Native Method)
           ERROR/AndroidRuntime(1164): at java.lang.reflect.Method.invoke(Method.java:521)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情释 2024-11-23 06:10:43

regmalebtnregfemalebtn 可能为 null。确保在调用 findViewById() 之前调用 Activity 中的 setContentView。否则,它们将返回 null。

regmalebtn or regfemalebtn are likely null. Make sure setContentView in your activity is called before the findViewById() calls. Otherwise, they'll return null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文