使用parse.com数据库存储数据的Android代码有什么问题

时间:2015-07-23 17:13:13

标签: android parse-platform

String nametxt;
String bloodtxt;
String agetxt;
String mobiletxt;
Button register;
EditText name;
EditText age;
EditText blood;
EditText mobile;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);
    name = (EditText) findViewById(R.id.name);
    age = (EditText) findViewById(R.id.age);
    blood = (EditText)findViewById(R.id.blood);
    age = (EditText)findViewById(R.id.age);
    register =(Button)findViewById(R.id.register);
    register.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            nametxt = name.getText().toString();
            agetxt = age.getText().toString();
            bloodtxt = blood.getText().toString();//logcat pointing exception here//
            mobiletxt = mobile.getText().toString();
            if (nametxt.equals("") && agetxt.equals("")) {
                Toast.makeText(getApplicationContext(),
                        "Please complete the sign up form",
                        Toast.LENGTH_LONG).show();
            }
            else
            {
                ParseUser user = new ParseUser();
                user.setUsername(nametxt);
                user.setPassword(agetxt);
                user.put("blood", bloodtxt);
                user.put("Mobile",mobiletxt);
                user.signUpInBackground(new SignUpCallback() {
                    @Override
                    public void done(ParseException e) {
                        if (e == null) {
                            Toast.makeText(getApplicationContext(), "Succesfully registered", Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(getApplicationContext(), "hey %d you are already registered in blood database.." + nametxt, Toast.LENGTH_LONG).show();


                        }
                    }
                });
            }

        }
    });

}

我收到此错误:

  

流程:com.blooddata.lemuriadigitallab.blooddata,PID:26845       java.lang.NullPointerException:尝试调用虚方法' android.text.Editable android.widget.EditText.getText()'在null   对象参考               在com.blooddata.lemuriadigitallab.blooddata.Register $ 1.onClick(Register.java:43)               在android.view.View.performClick(View.java:5181)               在android.view.View $ PerformClick.run(View.java:20887)               在android.os.Handler.handleCallback(Handler.java:739)               在android.os.Handler.dispatchMessage(Handler.java:95)               在android.os.Looper.loop(Looper.java:145)               在android.app.ActivityThread.main(ActivityThread.java:5942)               at java.lang.reflect.Method.invoke(Native Method)               在java.lang.reflect.Method.invoke(Method.java:372)               在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1400)               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)

2 个答案:

答案 0 :(得分:3)

初始化“移动” EditText变量。

从这里引发NullPointerException:

  mobile.getText().toString();

答案 1 :(得分:1)

您正在初始化"年龄"两次:

name = (EditText) findViewById(R.id.name);
age = (EditText) findViewById(R.id.age);
blood = (EditText)findViewById(R.id.blood);
age = (EditText)findViewById(R.id.age);

所以这里:

mobiletxt = mobile.getText().toString();

你得到一个空指针

相反,2中的一个应该是"移动"

name = (EditText) findViewById(R.id.name);
age = (EditText) findViewById(R.id.age);
blood = (EditText)findViewById(R.id.blood);
mobile = (EditText)findViewById(R.id.mobile);