Java 每当我点击按钮时,它都会给出最后一个值结果非当前值
无论何时执行数据函数,它都会存储正确的查询值,但当我返回JSON时。它给我最后一个值的结果,而不是给我新值的结果。函数数据或函数异步中有错误 我给您的错误日志中没有错误。查询字符串包含正确的值,但结果是最后一个字符串Java 每当我点击按钮时,它都会给出最后一个值结果非当前值,java,android,Java,Android,无论何时执行数据函数,它都会存储正确的查询值,但当我返回JSON时。它给我最后一个值的结果,而不是给我新值的结果。函数数据或函数异步中有错误 我给您的错误日志中没有错误。查询字符串包含正确的值,但结果是最后一个字符串 public class MainActivity extends AppCompatActivity{ public static String QUERY = null; public static String DATA = null;
public class MainActivity extends AppCompatActivity{
public static String QUERY = null;
public static String DATA = null;
SpeechRecognizer speechRecognizer;
Intent speechIntent;
TextView textView;
Button button;
TextView textView1;
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
textView = (TextView) findViewById (R.id.text);
textView1 = (TextView) findViewById (R.id.text1);
requestPermissions (new String[]{Manifest.permission.INTERNET, Manifest.permission.RECORD_AUDIO}, 10);
speechRecognizer = SpeechRecognizer.createSpeechRecognizer (this);
speechRecognizer.setRecognitionListener (new RecognitionListener () {
@Override
public void onReadyForSpeech(Bundle bundle) {
}
@Override
public void onBeginningOfSpeech() {
}
@Override
public void onRmsChanged(float v) {
}
@Override
public void onBufferReceived(byte[] bytes) {
}
@Override
public void onEndOfSpeech() {
}
@Override
public void onError(int i) {
}
@Override
public void onResults(Bundle bundle) {
ArrayList<String> arrayList = bundle.getStringArrayList (SpeechRecognizer.RESULTS_RECOGNITION);
if(arrayList!=null){
textView.setText (arrayList.get (0));
QUERY = arrayList.get (0);
}else {
Toast.makeText (MainActivity.this, "Array List is null", Toast.LENGTH_SHORT).show ();
}
}
@Override
public void onPartialResults(Bundle bundle) {
}
@Override
public void onEvent(int i, Bundle bundle) {
}
});
speechIntent = new Intent (RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
speechIntent.putExtra (RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
speechIntent.putExtra (RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault ());
}
public void start(View v) {
speechRecognizer.startListening (speechIntent);
}
public void data(View v){
Toast.makeText (this, QUERY, Toast.LENGTH_SHORT).show ();
Async async = new Async ();
async.execute ();
if(DATA!=null){
textView1.setText (DATA);
}
} }
class Async extends AsyncTask<Void, Void, Void>{
String line = "";
String data = "";
@Override
protected Void doInBackground(Void... voids) {
try {
data=null;
Log.e("Query in url", MainActivity.QUERY);
URL url = new URL ("https://api.dialogflow.com/v1/query?v=20150910&contexts=[]&lang=en&query="
+ MainActivity.QUERY +"&sessionId=bee67580-d05c-47f6-8d64-a6218c3913e1");
URLConnection httpURLConnection = url.openConnection ();
httpURLConnection.setRequestProperty ("Authorization", "Bearer
CONFIDENTIAL KEY");
InputStream inputStream = httpURLConnection.getInputStream ();
BufferedReader bufferedReader = new BufferedReader (new
InputStreamReader (inputStream));
while ((line = bufferedReader.readLine ()) != null) {
data += line;
}
} catch (MalformedURLException e) {
Log.i ("PROBLEM", "URL");
} catch (IOException e) {
Log.i ("PROBLEM", "IOEXCEPTIONe");
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
MainActivity.DATA = data;
super.onPostExecute (aVoid);
} }
public类MainActivity扩展了AppCompatActivity{
公共静态字符串查询=null;
公共静态字符串数据=null;
语音识别器语音识别器;
意图、言语、意图;
文本视图文本视图;
按钮;
文本视图文本视图1;
@RequiresApi(api=Build.VERSION\u CODES.M)
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(textView)findViewById(R.id.text);
textView1=(TextView)findViewById(R.id.text1);
requestPermissions(新字符串[]{Manifest.permission.INTERNET,Manifest.permission.RECORD_AUDIO},10);
speechRecognizer=speechRecognizer.createSpeechRecognizer(此);
speechRecognizer.setRecognitionListener(新的RecognitionListener(){
@凌驾
ReadyForSpeech上的公共无效(捆绑){
}
@凌驾
开始时的公共无效fSpeech(){
}
@凌驾
公开无效(浮动v){
}
@凌驾
接收到的公用void onbuffer(字节[]字节){
}
@凌驾
公共无效onEndOfSpeech(){
}
@凌驾
公共无效申报人(int i){
}
@凌驾
公共void onResults(Bundle){
ArrayList ArrayList=bundle.getStringArrayList(SpeechRecognitor.RESULTS\u RECOGNITION);
if(arrayList!=null){
textView.setText(arrayList.get(0));
QUERY=arrayList.get(0);
}否则{
Toast.makeText(MainActivity.this,“数组列表为空”,Toast.LENGTH_SHORT.show();
}
}
@凌驾
公共void onPartialResults(Bundle){
}
@凌驾
公共void onEvent(int i,Bundle){
}
});
speechIntent=新意图(RecognizerIntent.ACTION\u RECOGNIZE\u SPEECH);
speechIntent.putExtra(识别器Intent.EXTRA语言模型,识别器Intent.LANGUAGE模型自由形式);
speechIntent.putExtra(RecognizerIntent.EXTRA_语言,Locale.getDefault());
}
公共无效开始(视图v){
speechRecognizer.startListening(speechIntent);
}
公共无效数据(视图v){
Toast.makeText(this,QUERY,Toast.LENGTH_SHORT).show();
Async Async=new Async();
async.execute();
如果(数据!=null){
textView1.setText(数据);
}
} }
类Async扩展了AsyncTask{
字符串行=”;
字符串数据=”;
@凌驾
受保护的空位背景(空位…空位){
试一试{
数据=空;
Log.e(“url中的查询”,MainActivity.Query);
URL=新URL(“https://api.dialogflow.com/v1/query?v=20150910&contexts=[]&lang=en&query=”
+MainActivity.QUERY+“&sessionId=bee67580-d05c-47f6-8d64-a6218c3913e1”);
URLConnection httpURLConnection=url.openConnection();
httpURLConnection.setRequestProperty(“授权”、“承载”
机密密钥);
InputStream InputStream=httpURLConnection.getInputStream();
BufferedReader BufferedReader=新的BufferedReader(新的
InputStreamReader(inputStream));
而((line=bufferedReader.readLine())!=null){
数据+=行;
}
}捕获(格式错误){
Log.i(“问题”、“URL”);
}捕获(IOE异常){
Log.i(“问题”,“IOExceptionOne”);
}
返回null;
}
@凌驾
受保护的void onPostExecute(void避免){
MainActivity.DATA=数据;
super.onPostExecute(避免);
} }
问题是您正在调用一个异步任务
,并且在它之后访问同一个变量
,该变量在异步任务
中进行修改 Async async = new Async ();
async.execute ();
if(DATA!=null){
textView1.setText (DATA);
}
在这里,async
将在后台线程上执行,但主线程将继续执行,因此每次都将设置最后一个DATA
值
解决方案您最好将
setText()
code移动到onPostExecute()
onPostExecute()
在主线程上运行,所以您可以轻松访问其中的Ui元素
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute (aVoid);
MainActivity.DATA = data;
if(DATA!=null){
textView1.setText (DATA);
}
}
您正在设置要执行的异步完成之前的文本。你在打电话吗
async.execute ();
if(DATA!=null){ textView1.setText (DATA);
async.execute
立即返回,因此数据仍然具有旧值。
您需要做的是在
onPostExecute
函数中设置文本视图文本。如果这个答案对您有帮助,请对答案进行投票。一个建议是,如果实例变量可以工作,就不要使用静态变量。Thx。(为什么要贴标签?请检查是否符合标签说明。)