单击按钮时出现NullPointerException错误

时间:2016-02-22 04:21:19

标签: java android button nullpointerexception

当我在我的Android设备中部署我的应用程序并尝试单击注册按钮时,它会在行中抛出一个java null指针异常:

register(Fname,Lname,Email,Password)

Object result = client.execute("execute", callParams);

我的主要活动代码和堆栈跟踪如下所示。

请帮我解决这些错误。

public class MainActivity2 extends Activity  {
        public void onCreate(Bundle savedInstanceState ){
            super.onCreate(savedInstanceState);
            setContentView(R.layout.next);
            EditText ed1;
            ed1=(EditText) findViewById(R.id.editText);
           final String Fname=ed1.getText().toString();
            EditText ed2;
            ed2=(EditText)findViewById(R.id.editText2);
           final String Lname=ed2.getText().toString();
            EditText ed3;
            ed3=(EditText)findViewById(R.id.editText3);
           final String Email=ed3.getText().toString();
            EditText ed4;
            ed4=(EditText)findViewById(R.id.editText4);
           final String Password=ed4.getText().toString();
            Button btn3;
            btn3=(Button) findViewById(R.id.button3);
            btn3.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    try {
                        register(Fname, Lname, Email, Password);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            });
        }
        public Integer register(String Firstname,String Lastname,String Email,String Password)throws Exception{
            //log.debug("In Register...");
            Toast.makeText(getApplicationContext(), "1", Toast.LENGTH_LONG).show();

            XmlRpcClient client = getXmlRpcClient();
            try {
                System.out.println(getXmlRpcClient());
                HashMap<String, Object> fields = new HashMap<String, Object>();
                fields.put("name",Firstname );
                fields.put("display_name", Lastname);
                fields.put("email", Email);
                fields.put("password", Password);
                Toast.makeText(getApplicationContext(), "3", Toast.LENGTH_LONG).show();
                //log.debug("Got the customer fields, rpc call to create customer");

                List<Object> callParams = getCallParameters("res.partner", MainActivity.ActionType.CREATE);
                callParams.add(fields);
                Toast.makeText(getApplicationContext(), "4", Toast.LENGTH_LONG).show();
                Object result = client.execute("execute", callParams);
               Toast.makeText(getApplicationContext(), "5", Toast.LENGTH_LONG).show();
                Integer customerId = (result != null) ? (Integer) result : null;
                Toast.makeText(getApplicationContext(), "+customerId", Toast.LENGTH_LONG).show();
                //log.debug("Customer/Partner record created, Id = " + customerId);

                //log.debug("About to create a user login in res_users");
                 // registerUser(Email, Password,customerId);

                return customerId;

            } catch (Exception e) {
               Toast.makeText(getApplicationContext(), "7"+e, Toast.LENGTH_LONG).show();
                System.out.println("Excpt" + e);
                Log.d(null, Log.getStackTraceString(new Exception()));
                e.printStackTrace();
                throw e;
                //log.error(e.getMessage());
                //log.error(e.fillInStackTrace());e
            }
            //return 0;
        }

堆栈追踪:

This is my stack trace

即使我在我的编辑文本字段中输入了值,它确实取了我输入的值,这是我的代码

final  EditText ed1;
    ed1=(EditText) findViewById(R.id.editText);
      final String Fname=ed1.getText().toString();
    final EditText ed2;
        ed2=(EditText)findViewById(R.id.editText2);
        final String Lname=ed2.getText().toString();
     final EditText ed3;
        ed3=(EditText)findViewById(R.id.editText3);
      final String Email=ed3.getText().toString();
    final EditText ed4;
       ed4=(EditText)findViewById(R.id.editText4);
       final String Password=ed4.getText().toString();
        Button btn3;
        btn3=(Button) findViewById(R.id.button3);

        btn3.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                if (ed1.getText().toString().equalsIgnoreCase(""))
                {
                    // show message/toast for empty name
                    Toast.makeText(getApplicationContext(), "Enter the fname", Toast.LENGTH_LONG).show();

                }
                else if (ed2.getText().toString().equalsIgnoreCase(""))
                {
                    // show message/toast for empty Surname
                    Toast.makeText(getApplicationContext(), "Enter the lname", Toast.LENGTH_LONG).show();
                }
                else if (ed3.getText().toString().equalsIgnoreCase(""))
                {
                    // show message/toast for empty Email
                    Toast.makeText(getApplicationContext(), "Enter the Email", Toast.LENGTH_LONG).show();
                }
                else if(ed4.getText().toString().equalsIgnoreCase(""))
                {
                    // If above all conditions are false means all fields are not empty so put your action here
                    Toast.makeText(getApplicationContext(), "enter the Password", Toast.LENGTH_LONG).show();
                }
                else
                {
                   Toast.makeText(getApplicationContext(), Fname, Toast.LENGTH_LONG).show();

                  System.out.println(Fname);
                    register(Fname, Lname, Email, Password);
                }


            }

        });
    }

0 个答案:

没有答案