我的对话流聊天机器人应用程序的 Objects.requireNonNull 问题
Posted
技术标签:
【中文标题】我的对话流聊天机器人应用程序的 Objects.requireNonNull 问题【英文标题】:Objects.requireNonNull problem with my dialogflow chatbot app 【发布时间】:2021-12-27 15:25:02 【问题描述】:这是错误:
该应用程序是一个带有对话流聊天机器人的界面。当我设置一些文本并尝试发送它时,应用程序正在崩溃。可能是因为有一些过时的代码,但我在互联网上找不到解决方案。
顺便说一句,这是 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);
如果您需要其他内容,请咨询!谢谢
【问题讨论】:
似乎chatView.getAdapter()
或 chatView.getLayoutManager
返回 null。如果您确定参数不为空(或者您正在构建 API 并且 API 合同中的变量不为空),则应仅使用 Objects.requireNonNull
。
【参考方案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()
...
【讨论】:
以上是关于我的对话流聊天机器人应用程序的 Objects.requireNonNull 问题的主要内容,如果未能解决你的问题,请参考以下文章