Issue
That’s the error:
The application is an interface with a dialogflow chatbot. The application is crushing when i set some text and i try to send it. Probably its because there is some depracated code, but i cant find a solution on the internet.
BTW that’s the 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();
}
}
}
I think the problem is here:
Objects.requireNonNull(chatView.getAdapter()).notifyDataSetChanged();
Objects.requireNonNull(chatView.getLayoutManager()).scrollToPosition(messageList.size() - 1);
If you need something else ask! Thanks
Solution
If the messageList
is empty by default (and looks like it is) you have an empty recycler view and thus there are no non-fatal errors in the logs informing you that the recycler view is missing a layout manager. Set the layout manager to your chatView
recycler view and it should be enough.
...
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() {
...
Answered By – Jenea Vranceanu