首页 > 解决方案 > 当已有用户输入时,变量为空

问题描述

我正在创建一个函数来在我的应用程序片段中添加用户输入值。这应该提交给我的 api,但是当我按下neuralButton我的AlertDialog表单时,应用程序崩溃了。我试图找出使用调试器可能出现的问题,它表明我的achievement变量中没有值。它只是停止调试String achievement = addAchievement.getText().toString()。我试图按照不同的方法解决问题,但现在我不确定可能是什么问题。

 Button btn1 = RootView.findViewById(R.id.addBtn1);

        btn1.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onClick(View view) {

                AlertDialog.Builder alert = new AlertDialog.Builder(getContext());
                alert.setView(R.layout.add_fragment);
                alert.setTitle("Add");
                alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
                alert.setNeutralButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Gson gson = new Gson();
                        String loginModel = myPrefs.getString("LoginModel", "");
                        LoginModel loginModel1 = gson.fromJson(loginModel, LoginModel.class);
                        API requests = new API();

                        //Description
                        EditText addAchievement = (EditText) RootView.findViewById(R.id.addAchievement);
//                            addTraining = (EditText) RootView.findViewById(R.id.addTraining);
//                            addSkills = (EditText) RootView.findViewById(R.id.addSkills);
//                            addEdu = (EditText) RootView.findViewById(R.id.addEdu);
//                            addReviews = (EditText) RootView.findViewById(R.id.addReviews);
                        String achievement = addAchievement.getText().toString();
//                            String training = addTraining.getText().toString();
//                            String skills = addSkills.getText().toString();
//                            String edu = addEdu.getText().toString();
//                            String reviews = addReviews.getText().toString();
                        HashMap<String, String> params = new HashMap<>();


                        try {
                            JSONObject obj = new JSONObject();
                            params.put("description", achievement);


                            JSONObject res = requests.postRequest("employee/profile",params,loginModel1);
                            boolean error = res.getBoolean("error");

                            if (error) {
                                new AlertDialog.Builder(getContext())
                                        .setTitle("OOPS")
                                        .setMessage("Something went wrong please try again, if the problem persists contact the admin")
                                        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog, int which) {
                                                // Continue with delete operation
                                            }
                                        })
                                        // A null listener allows the button to dismiss the dialog and take no further action.
                                        .setNegativeButton(android.R.string.no, null)
                                        .setIcon(android.R.drawable.ic_dialog_alert)
                                        .show();
                            } else {
                                new AlertDialog.Builder(getContext())
                                        .setTitle("Success")
                                        .setMessage("Contract Submitted Successfully")

                                        // Specifying a listener allows you to take an action before dismissing the dialog.
                                        // The dialog is automatically dismissed when a dialog button is clicked.
                                        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog, int which) {
                                                // Continue with delete operation
                                            }
                                        })
                                        // A null listener allows the button to dismiss the dialog and take no further action.
                                        .setNegativeButton(android.R.string.no, null)
                                        .setIcon(android.R.drawable.ic_dialog_alert)
                                        .show();
                            }


                        } catch (JSONException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    }
                });

add_fragment 代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"``
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp">


        <EditText
            android:id="@+id/addAchievement"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Add Achievement"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.546" />




        <!--        <EditText-->
        <!--            android:id="@+id/addTraining"-->
        <!--            android:layout_width="match_parent"-->
        <!--            android:layout_height="wrap_content"-->
        <!--            android:hint="Add Training"-->
        <!--            app:layout_constraintBottom_toBottomOf="parent"-->
        <!--            app:layout_constraintEnd_toEndOf="parent"-->
        <!--            app:layout_constraintStart_toStartOf="parent"-->
        <!--            app:layout_constraintTop_toTopOf="parent"-->
        <!--            app:layout_constraintVertical_bias="0.27" />-->

        <!--        <Space-->
        <!--            android:layout_width="match_parent"-->
        <!--            android:layout_height="20dp"-->
        <!--            app:layout_constraintBottom_toBottomOf="parent"-->
        <!--            app:layout_constraintEnd_toEndOf="parent"-->
        <!--            app:layout_constraintStart_toStartOf="parent"-->
        <!--            app:layout_constraintTop_toTopOf="parent"-->
        <!--            app:layout_constraintVertical_bias="0.178" />-->

        <!--        <EditText-->
        <!--            android:id="@+id/addSkills"-->
        <!--            android:layout_width="match_parent"-->
        <!--            android:layout_height="wrap_content"-->
        <!--            android:hint="Add Skills"-->
        <!--            app:layout_constraintBottom_toBottomOf="parent"-->
        <!--            app:layout_constraintEnd_toEndOf="parent"-->
        <!--            app:layout_constraintStart_toStartOf="parent"-->
        <!--            app:layout_constraintTop_toTopOf="parent" />-->

        <!--        <Space-->
        <!--            android:layout_width="match_parent"-->
        <!--            android:layout_height="20dp"-->
        <!--            app:layout_constraintBottom_toBottomOf="parent"-->
        <!--            app:layout_constraintEnd_toEndOf="parent"-->
        <!--            app:layout_constraintStart_toStartOf="parent"-->
        <!--            app:layout_constraintTop_toTopOf="parent"-->
        <!--            app:layout_constraintVertical_bias="0.618" />-->

        <!--        <EditText-->
        <!--            android:id="@+id/addEdu"-->
        <!--            android:layout_width="match_parent"-->
        <!--            android:layout_height="wrap_content"-->
        <!--            android:hint="Add Education"-->
        <!--            app:layout_constraintBottom_toBottomOf="parent"-->
        <!--            app:layout_constraintEnd_toEndOf="parent"-->
        <!--            app:layout_constraintStart_toStartOf="parent"-->
        <!--            app:layout_constraintTop_toTopOf="parent"-->
        <!--            app:layout_constraintVertical_bias="0.746" />-->

        <!--        <Space-->
        <!--            android:layout_width="match_parent"-->
        <!--            android:layout_height="20dp"-->
        <!--            app:layout_constraintBottom_toBottomOf="parent"-->
        <!--            app:layout_constraintEnd_toEndOf="parent"-->
        <!--            app:layout_constraintStart_toStartOf="parent"-->
        <!--            app:layout_constraintTop_toTopOf="parent"-->
        <!--            app:layout_constraintVertical_bias="0.836" />-->

        <!--        <EditText-->
        <!--            android:id="@+id/addReviews"-->
        <!--            android:layout_width="match_parent"-->
        <!--            android:layout_height="wrap_content"-->
        <!--            android:hint="Add Reviews"-->
        <!--            app:layout_constraintBottom_toBottomOf="parent"-->
        <!--            app:layout_constraintEnd_toEndOf="parent"-->
        <!--            app:layout_constraintStart_toStartOf="parent"-->
        <!--            app:layout_constraintTop_toTopOf="parent"-->
        <!--            app:layout_constraintVertical_bias="1.0" />-->

        <!--        <Space-->
        <!--            android:layout_width="match_parent"-->
        <!--            android:layout_height="20dp"-->
        <!--            app:layout_constraintBottom_toBottomOf="parent"-->
        <!--            app:layout_constraintEnd_toEndOf="parent"-->
        <!--            app:layout_constraintStart_toStartOf="parent"-->
        <!--            app:layout_constraintTop_toTopOf="parent"-->
        <!--            app:layout_constraintVertical_bias="0.387" />-->

    </androidx.constraintlayout.widget.ConstraintLayout>
</RelativeLayout>

标签: javaandroidnode.jsandroid-studio

解决方案


替换代码中的这一行:

alert.setNeutralButton("OK", new DialogInterface.OnClickListener() {

用这条线:

alert.setPositiveButton("OK", (dialog, which) -> {

推荐阅读