【问题标题】:What is wrong with this Android code that uses the parse.com database to store data这个使用 parse.com 数据库存储数据的 Android 代码有什么问题
【发布时间】:2015-10-14 03:33:49
【问题描述】:
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:尝试在 null 上调用虚拟方法 'android.text.Editable android.widget.EditText.getText()' 对象引用 在 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) 在 java.lang.reflect.Method.invoke(本机方法) 在 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)

【问题讨论】:

  • 您在哪一行得到错误?您的一个edittext字段似乎为空。id 错误或不存在

标签: android parse-platform


【解决方案1】:

初始化“mobile” EditText 变量。

NullPointerException 是从这里引发的:

  mobile.getText().toString();

【讨论】:

    【解决方案2】:

    你正在初始化“年龄”两次:

    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();
    

    你得到一个空指针

    相反,两者之一应该是“移动”

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-08
      • 1970-01-01
      • 1970-01-01
      • 2012-06-03
      • 1970-01-01
      相关资源
      最近更新 更多