Android Studio在启动时不断出错

时间:2017-04-29 14:45:32

标签: java android nullpointerexception

出于某种原因,我的应用程序会不断出现此错误。我在下面添加了错误日志,但无法解决问题所在。我已经多次尝试弄明白,但仍然没有到达任何地方。希望您能理解错误日志。

    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at .....buildaempiredesign.GameRegister.onCreate(GameRegister.java:34)

该代码是下面代码的bRegister.setOnClickListener部分。

            bRegister.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                final String email = etEmail.getText().toString();
                final String password = etPassword.getText().toString();
                @SuppressLint("SimpleDateFormat") final String birthday = new SimpleDateFormat("dd/MM/yyyy").format(etBirthday.getText().toString());
                final String playername = etPersonName.getText().toString();
                final String gender = etPersonName.getText().toString();
                final String gamename = etGameName.getText().toString();

                Response.Listener<String> responceListener = new Response.Listener<String>(){
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonResponce = new JSONObject(response);
                            boolean error = jsonResponce.getBoolean("error");

                            if (!error){
                                Intent intent = new Intent(GameRegister.this, GameLogin.class);
                                GameRegister.this.startActivity(intent);
                            } else {
                                AlertDialog.Builder builder = new AlertDialog.Builder(GameRegister.this);
                                builder.setMessage("Login Failed")
                                        .setNegativeButton("Retry", null)
                                        .create()
                                        .show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                };

                RegisterRequest registerRequest = new RegisterRequest(email, password, playername, gender, birthday, gamename, responceListener);
                queue.add(registerRequest);
            }
        });

1 个答案:

答案 0 :(得分:0)

第34行的代码在哪里? 确保您已初始化按钮。并且您的按钮ID没有错。

Button bRegister;

或按钮bRegister =(按钮)findViewById(R.id.yourbuttonid);