Objects.requireNonNull 我的 dialogflow 聊天机器人应用有问题

Objects.requireNonNull problem with my dialogflow chatbot app

那是错误:

该应用程序是一个带有 dialogflow 聊天机器人的界面。当我设置一些文本并尝试发送它时,应用程序崩溃了。可能是因为有一些过时的代码,但我无法在互联网上找到解决方案。

顺便说一句,那是 MainActivity.java:

public class MainActivity extends AppCompatActivity implements BotReply {

RecyclerView chatView;
ChatAdapter chatAdapter;
ArrayList<Message> messageList = new ArrayList<>();
EditText editMessage;
Button btnSend;

//dialogFlow
private SessionsClient sessionsClient;
private SessionName sessionName;
private final String uuid = UUID.randomUUID().toString();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    chatView = findViewById(R.id.rv_message);
    editMessage = findViewById(R.id.et_message);
    btnSend = findViewById(R.id.btn_mic);

    chatAdapter = new ChatAdapter(messageList, this);
    chatView.setAdapter(chatAdapter);

    btnSend.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View view) {
            String message = editMessage.getText().toString();
            if (!message.isEmpty()) {
                messageList.add(new Message(message, false));
                editMessage.setText("");
                sendMessageToBot(message);
                Objects.requireNonNull(chatView.getAdapter()).notifyDataSetChanged();
                Objects.requireNonNull(chatView.getLayoutManager())
                        .scrollToPosition(messageList.size() - 1);
            } else {
                Toast.makeText(MainActivity.this, "Please enter text!", Toast.LENGTH_SHORT).show();
            }
        }
    });

    setUpBot();
}

private void setUpBot() {
    String TAG = "mainactivity";
    try {
        InputStream stream = this.getResources().openRawResource(R.raw.credentials);
        GoogleCredentials credentials = GoogleCredentials.fromStream(stream)
                .createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));
        String projectId = ((ServiceAccountCredentials) credentials).getProjectId();

        SessionsSettings.Builder settingsBuilder = SessionsSettings.newBuilder();
        SessionsSettings sessionsSettings = settingsBuilder.setCredentialsProvider(
                FixedCredentialsProvider.create(credentials)).build();
        sessionsClient = SessionsClient.create(sessionsSettings);
        sessionName = SessionName.of(projectId, uuid);

        Log.d(TAG, "projectId : " + projectId);
    } catch (Exception e) {
        Log.d(TAG, "setUpBot: " + e.getMessage());
    }
}



private void sendMessageToBot(String message) {
    QueryInput input = QueryInput.newBuilder()
            .setText(TextInput.newBuilder().setText(message).setLanguageCode("en-US")).build();
    new SendMessageInBg(this, sessionName, sessionsClient, input).execute();
}



@Override
public void callback(DetectIntentResponse returnResponse) {
    if(returnResponse!=null) {
        String botReply = returnResponse.getQueryResult().getFulfillmentText();
        if(!botReply.isEmpty()){
            messageList.add(new Message(botReply, true));
            chatAdapter.notifyDataSetChanged();
            Objects.requireNonNull(chatView.getLayoutManager()).scrollToPosition(messageList.size() - 1);
        }else {
            Toast.makeText(this, "something went wrong", Toast.LENGTH_SHORT).show();
        }
    } else {
        Toast.makeText(this, "failed to connect!", Toast.LENGTH_SHORT).show();
    }
}

}

我认为问题出在这里:

Objects.requireNonNull(chatView.getAdapter()).notifyDataSetChanged();
Objects.requireNonNull(chatView.getLayoutManager()).scrollToPosition(messageList.size() - 1);

如果您需要其他东西,请询问!谢谢

如果 messageList 默认情况下为空(看起来是这样),则您的回收器视图为空,因此日志中没有非致命错误通知您回收器视图缺少布局管理器。将布局管理器设置为您的 chatView 回收站视图,这应该足够了。

...
chatAdapter = new ChatAdapter(messageList, this);
chatView.setAdapter(chatAdapter);

// Add these two lines
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
chatView.setLayoutManager(layoutManager);

btnSend.setOnClickListener(new View.OnClickListener() {
...