当已有用户输入时,java变量为null
我正在创建一个函数,在我的应用程序片段中添加用户输入值。这应该提交到我的api,但当我在AlertDialog
窗体上按neuralButton
时,应用程序崩溃。我尝试使用调试器找出问题所在,结果显示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(安卓.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(安卓.R.string.no, null)
.setIcon(安卓.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(安卓.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(安卓.R.string.no, null)
.setIcon(安卓.R.drawable.ic_dialog_alert)
.show();
}
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
添加\u片段代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"``
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:padding="16dp">
<安卓x.constraintlayout.widget.ConstraintLayout
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:padding="16dp">
<EditText
安卓:id="@+id/addAchievement"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓: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-->
<!-- 安卓:id="@+id/addTraining"-->
<!-- 安卓:layout_width="match_parent"-->
<!-- 安卓:layout_height="wrap_content"-->
<!-- 安卓: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-->
<!-- 安卓:layout_width="match_parent"-->
<!-- 安卓: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-->
<!-- 安卓:id="@+id/addSkills"-->
<!-- 安卓:layout_width="match_parent"-->
<!-- 安卓:layout_height="wrap_content"-->
<!-- 安卓:hint="Add Skills"-->
<!-- app:layout_constraintBottom_toBottomOf="parent"-->
<!-- app:layout_constraintEnd_toEndOf="parent"-->
<!-- app:layout_constraintStart_toStartOf="parent"-->
<!-- app:layout_constraintTop_toTopOf="parent" />-->
<!-- <Space-->
<!-- 安卓:layout_width="match_parent"-->
<!-- 安卓: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-->
<!-- 安卓:id="@+id/addEdu"-->
<!-- 安卓:layout_width="match_parent"-->
<!-- 安卓:layout_height="wrap_content"-->
<!-- 安卓: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-->
<!-- 安卓:layout_width="match_parent"-->
<!-- 安卓: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-->
<!-- 安卓:id="@+id/addReviews"-->
<!-- 安卓:layout_width="match_parent"-->
<!-- 安卓:layout_height="wrap_content"-->
<!-- 安卓: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-->
<!-- 安卓:layout_width="match_parent"-->
<!-- 安卓: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" />-->
</安卓x.constraintlayout.widget.ConstraintLayout>
</RelativeLayout>
# 1 楼答案
在代码中替换这一行:
用这句话: