Android 活动未从片段启动:无错误?

Android 活动未从片段启动:无错误?,android,Android,我在一个片段中为各种按钮设置了一个侦听器,其中一个应该是注销,将用户返回到MainActivity。然而,出于某种原因,它似乎并没有在点击上做任何事情;无错误,无活动启动。我看不出我的代码有任何明显的错误,有人能提供一个建议,说明为什么它看起来是不活动的吗 这是switch语句中的最后一种情况 //this handles our button click event @Override public void onClick(View v) { switch

我在一个片段中为各种按钮设置了一个侦听器,其中一个应该是注销,将用户返回到MainActivity。然而,出于某种原因,它似乎并没有在点击上做任何事情;无错误,无活动启动。我看不出我的代码有任何明显的错误,有人能提供一个建议,说明为什么它看起来是不活动的吗

这是switch语句中的最后一种情况

 //this handles our button click event
    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.btnHelp1:
                // Help message for title
                //First we instantiate the dialog
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

                //then set the strings for dialogtitle and message
                builder.setMessage(R.string.dialog_help1)
                        .setTitle(R.string.dialog_title_help1);

                //set the OK button
                builder.setPositiveButton(R.string.dialog_btn_ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // User clicked OK button, dismiss dialog
                        dialog.dismiss();
                    }
                });

                //then we pop it on screen
                AlertDialog dialog = builder.create();
                dialog.show();
                break;

            case R.id.btnHelp2:
                // Help message for name
                AlertDialog.Builder builder2 = new AlertDialog.Builder(getActivity());

                builder2.setMessage(R.string.dialog_help2)
                        .setTitle(R.string.dialog_title_help2);

                builder2.setPositiveButton(R.string.dialog_btn_ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // User clicked OK button, dismiss dialog
                        dialog.dismiss();
                    }
                });

                AlertDialog dialog2 = builder2.create();
                dialog2.show();
                break;

            case R.id.btnHelp3:
                // Help message for surname
                AlertDialog.Builder builder3 = new AlertDialog.Builder(getActivity());

                builder3.setMessage(R.string.dialog_help3)
                        .setTitle(R.string.dialog_title_help3);

                builder3.setPositiveButton(R.string.dialog_btn_ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // User clicked OK button, dismiss dialog
                        dialog.dismiss();
                    }
                });

                AlertDialog dialog3 = builder3.create();
                dialog3.show();
                break;

            case R.id.btnHelp4:
                // Help message for other name
                AlertDialog.Builder builder4 = new AlertDialog.Builder(getActivity());

                builder4.setMessage(R.string.dialog_help4)
                        .setTitle(R.string.dialog_title_help4);

                builder4.setPositiveButton(R.string.dialog_btn_ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // User clicked OK button, dismiss dialog
                        dialog.dismiss();
                    }
                });

                AlertDialog dialog4 = builder4.create();
                dialog4.show();
                break;

            case R.id.btnHelp5:
                // Help message for name
                AlertDialog.Builder builder5 = new AlertDialog.Builder(getActivity());

                builder5.setMessage(R.string.dialog_help5)
                        .setTitle(R.string.dialog_title_help5);

                builder5.setPositiveButton(R.string.dialog_btn_ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // User clicked OK button, dismiss dialog
                        dialog.dismiss();
                    }
                });

                AlertDialog dialog5 = builder5.create();
                dialog5.show();
                break;

            case R.id.btnSave1:
                // Save button onClick()
                //set input strings
                String title = inputTitle.getText().toString();
                String surname = inputSurname.getText().toString();
                String name = inputName.getText().toString();
                String other = inputOther.getText().toString();
                String mother = inputMother.getText().toString();

                //check they have been completed
                if (!title.isEmpty() &&
                        !surname.isEmpty() &&
                        !name.isEmpty() &&
                        !other.isEmpty() &&
                        !mother.isEmpty()) {
                    updateUserA(email, title, surname, name, other, mother);
                } else {
                    Toast.makeText(getActivity(),
                            "Please enter your details!", Toast.LENGTH_LONG)
                            .show();
                }
                break;

            case R.id.btnLog1:
                // logout user
                session.setLogin(false);

                db.deleteUsers();

                // Launching the main activity
                Intent intent = new Intent(getActivity(), MainActivity.class);
                startActivity(intent);

                break;

            default:
                break;
        }
    }
用这个

getActivity().startActivity(...);

如果出现单击,请尝试调试,如果没有,我猜是ID错误,或者您忘记设置侦听器或将其分配到错误的视图。

检查片段的layout.xml。大多数情况下,它的上下文是错误的。

我是个白痴,我从来没有在onCreateView中添加过按钮。。。对不起,伙计们,漫长的一天

我会先仔细检查一下你们的身份证。然后把日志放在箱子里,看看会发生什么。可能数据库操作挂起了?您能否通过IDE验证此案例是否被击中?这很可能是罪魁祸首。就像@AedonEtLIRA所说的那样-您的db调用可能会导致问题,但您应该会看到堆栈跟踪之类的东西。我是个白痴,我从来没有在onCreateView中添加过按钮。。。抱歉,伙计们,漫长的一天。哈哈。喝杯咖啡