从call.eneque添加代码后,我从标题中得到了错误,提示我的clickListener上出现Nullpointerexception,但是一切都已初始化,或者我忽略了什么?我花了好几个小时才能尝试其他教程和修复程序,但找不到任何东西。
这是我的代码:
public void openDialog(){
AlertDialog.Builder formBuilder = new AlertDialog.Builder(FriendListActivity.this);
View formView = getLayoutInflater().inflate(R.layout.contact_add_form,null);
final AlertDialog dialog = formBuilder.create();
final EditText nameText = dialog.findViewById(R.id.nameInput);
formBuilder.setView(formView);
dialog.show();
// Buttons
sButton = dialog.findViewById(R.id.saveButton);
sButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PalaverService friendsAddService = PalaverApiClient.getInstance();
final String FriendName = nameText.getText().toString();
Call<ApiResponse> call = friendsAddService.addFriend("", "", FriendName);
call.enqueue(new Callback<ApiResponse>() {
@Override
public void onResponse(Call<ApiResponse> call, Response<ApiResponse> response) {
ApiResponse responseSuccess = response.body();
if(responseSuccess.getMessageType().equals(1)){
friendListAdapter.add(FriendName);
}
dialog.hide();
}
@Override
public void onFailure(Call<ApiResponse> call, Throwable t) {
}
});
}
});
abbrechen = dialog.findViewById(R.id.abbrechen);
abbrechen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.hide();
}
});
}
这是我的错误:
Process: de.paluno.se.palaver, PID: 16209
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
我的setOnclickListener出现错误。希望有人能给我指点。
答案 0 :(得分:1)
您必须从formView
View formView = getLayoutInflater().inflate(R.layout.contact_add_form,null);
final EditText nameText = formView.findViewById(R.id.nameInput);
sButton = formView.findViewById(R.id.saveButton);