Async Task error: no text is given when user had inserted a string
Async Task error: no text is given when user had inserted a string
已解决:有人可以检查我的代码并提供帮助吗?我正在使用情感识别 API,它根据用户输入的文本生成字符串。我正在学习使用 AsyncTask 来加载结果,但即使用户在 edittext 中插入字符串,也会显示错误 "No text given",从 doInBackground 中的 try{} 块的 'catch' 异常中检测到错误方法。
public class Main3Activity extends AppCompatActivity {
private TextView textmain1, textmain2;
private EditText editText;
private Button submitBtn;
private String userInputText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
textmain1 = (TextView) findViewById(R.id.detected3);
textmain2 = (TextView) findViewById(R.id.sentences3);
editText = (EditText) findViewById(R.id.edittext3);
submitBtn = (Button) findViewById(R.id.submitBtn3);
userInputText = editText.getText().toString();
submitBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyAsyncTask myAsyncTask = new MyAsyncTask();
myAsyncTask.execute(userInputText);
}
});
}
private class MyAsyncTask extends AsyncTask<String, Integer, Boolean> {
private String detectedResponse, sentencesResponse;
private ProgressBar progressBar;
@Override
protected void onPreExecute() {
progressBar = (ProgressBar) findViewById(R.id.progressbar);
progressBar.setVisibility(View.VISIBLE);
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}
@Override
protected Boolean doInBackground(String... strings) {
String myString = strings[0];
try {
List<ToneScore> documentTones = new ArrayList<>();
List<SentenceAnalysis> sentenceDetectedTones = new ArrayList<>();
IamAuthenticator authenticator = new IamAuthenticator(authenticator);
ToneAnalyzer toneAnalyzer = new ToneAnalyzer("2020-02-22", authenticator);
toneAnalyzer.setServiceUrl(url);
ToneOptions toneOptions = new ToneOptions.Builder().text(myString).build();
ToneAnalysis toneAnalysis = toneAnalyzer.tone(toneOptions).execute().getResult();
documentTones = toneAnalysis.getDocumentTone().getTones();
if (documentTones == null || documentTones.isEmpty()) {
detectedResponse = "No tones are detected :(";
} else {
StringBuilder detectedTones = new StringBuilder();
for (ToneScore score : documentTones) {
if (score.getScore() > 0.5f) {
detectedTones.append(score.getToneName()).append(" \n").append(score.getScore()).append("\n\n");
}
}
detectedResponse = detectedTones.toString();
}
sentenceDetectedTones = toneAnalysis.getSentencesTone();
if (sentenceDetectedTones == null || sentenceDetectedTones.isEmpty()) {
sentencesResponse = "Oops! No sentence analysis is available for this one";
} else {
StringBuilder sentenceTones = new StringBuilder();
for (SentenceAnalysis sentenceAnalysis : sentenceDetectedTones) {
List<ToneScore> singleScoreBlock = sentenceAnalysis.getTones();
for (ToneScore toneScore : singleScoreBlock) {
if (toneScore.getScore() > 0.5) {
sentenceTones.append("\"").append(sentenceAnalysis.getText()).append("\"");
sentenceTones.append("\n").append(toneScore.getToneName()).append(": ").append(toneScore.getScore()).append("\n\n");
}
}
}
sentencesResponse = sentenceTones.toString();
}
} catch (ArrayIndexOutOfBoundsException | IllegalArgumentException | ServiceResponseException e) {
Main3Activity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(Main3Activity.this, "API error here: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
publishProgress();
return true;
}
@Override
protected void onPostExecute(Boolean s) {
progressBar.setVisibility(View.GONE);
editText.setText("");
if (s.equals(true)) {
textmain1.setText(detectedResponse);
textmain2.setText(sentencesResponse);
} else {
Toast.makeText(Main3Activity.this, "Error :(", Toast.LENGTH_SHORT).show();
}
}
}
}
您必须在单击按钮时获取文本
submitBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
userInputText = editText.getText().toString();
MyAsyncTask myAsyncTask = new MyAsyncTask();
myAsyncTask.execute(userInputText);
}
});
已解决:有人可以检查我的代码并提供帮助吗?我正在使用情感识别 API,它根据用户输入的文本生成字符串。我正在学习使用 AsyncTask 来加载结果,但即使用户在 edittext 中插入字符串,也会显示错误 "No text given",从 doInBackground 中的 try{} 块的 'catch' 异常中检测到错误方法。
public class Main3Activity extends AppCompatActivity {
private TextView textmain1, textmain2;
private EditText editText;
private Button submitBtn;
private String userInputText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
textmain1 = (TextView) findViewById(R.id.detected3);
textmain2 = (TextView) findViewById(R.id.sentences3);
editText = (EditText) findViewById(R.id.edittext3);
submitBtn = (Button) findViewById(R.id.submitBtn3);
userInputText = editText.getText().toString();
submitBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyAsyncTask myAsyncTask = new MyAsyncTask();
myAsyncTask.execute(userInputText);
}
});
}
private class MyAsyncTask extends AsyncTask<String, Integer, Boolean> {
private String detectedResponse, sentencesResponse;
private ProgressBar progressBar;
@Override
protected void onPreExecute() {
progressBar = (ProgressBar) findViewById(R.id.progressbar);
progressBar.setVisibility(View.VISIBLE);
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}
@Override
protected Boolean doInBackground(String... strings) {
String myString = strings[0];
try {
List<ToneScore> documentTones = new ArrayList<>();
List<SentenceAnalysis> sentenceDetectedTones = new ArrayList<>();
IamAuthenticator authenticator = new IamAuthenticator(authenticator);
ToneAnalyzer toneAnalyzer = new ToneAnalyzer("2020-02-22", authenticator);
toneAnalyzer.setServiceUrl(url);
ToneOptions toneOptions = new ToneOptions.Builder().text(myString).build();
ToneAnalysis toneAnalysis = toneAnalyzer.tone(toneOptions).execute().getResult();
documentTones = toneAnalysis.getDocumentTone().getTones();
if (documentTones == null || documentTones.isEmpty()) {
detectedResponse = "No tones are detected :(";
} else {
StringBuilder detectedTones = new StringBuilder();
for (ToneScore score : documentTones) {
if (score.getScore() > 0.5f) {
detectedTones.append(score.getToneName()).append(" \n").append(score.getScore()).append("\n\n");
}
}
detectedResponse = detectedTones.toString();
}
sentenceDetectedTones = toneAnalysis.getSentencesTone();
if (sentenceDetectedTones == null || sentenceDetectedTones.isEmpty()) {
sentencesResponse = "Oops! No sentence analysis is available for this one";
} else {
StringBuilder sentenceTones = new StringBuilder();
for (SentenceAnalysis sentenceAnalysis : sentenceDetectedTones) {
List<ToneScore> singleScoreBlock = sentenceAnalysis.getTones();
for (ToneScore toneScore : singleScoreBlock) {
if (toneScore.getScore() > 0.5) {
sentenceTones.append("\"").append(sentenceAnalysis.getText()).append("\"");
sentenceTones.append("\n").append(toneScore.getToneName()).append(": ").append(toneScore.getScore()).append("\n\n");
}
}
}
sentencesResponse = sentenceTones.toString();
}
} catch (ArrayIndexOutOfBoundsException | IllegalArgumentException | ServiceResponseException e) {
Main3Activity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(Main3Activity.this, "API error here: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
publishProgress();
return true;
}
@Override
protected void onPostExecute(Boolean s) {
progressBar.setVisibility(View.GONE);
editText.setText("");
if (s.equals(true)) {
textmain1.setText(detectedResponse);
textmain2.setText(sentencesResponse);
} else {
Toast.makeText(Main3Activity.this, "Error :(", Toast.LENGTH_SHORT).show();
}
}
}
}
您必须在单击按钮时获取文本
submitBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
userInputText = editText.getText().toString();
MyAsyncTask myAsyncTask = new MyAsyncTask();
myAsyncTask.execute(userInputText);
}
});