Public void on activityresult上的结果_OK中出现java错误
我的MainActivity上有这段代码,但当我在fragment(我的HomeFragment)上尝试它时,我在结果\u OK(在ActivityResult上的Public void上)上出错
我不知道这有什么问题,有人知道吗? 总之,这是我的代码:
package com.thesis.artificialintelligence;
import 安卓.app.Activity;
import 安卓.content.ActivityNotFoundException;
import 安卓.content.ComponentName;
import 安卓.content.Intent;
import 安卓.media.AudioManager;
import 安卓.os.Bundle;
import 安卓.speech.RecognizerIntent;
import 安卓.speech.tts.TextToSpeech;
import 安卓.support.v4.app.Fragment;
import 安卓.view.LayoutInflater;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓.widget.Button;
import 安卓.widget.TextView;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Locale;
import java.util.Random;
import javax.xml.transform.Result;
public class HomeFragment extends Fragment
{
private TextView resultTEXT;
private TextView resultTEXT2;
private Button button;
TextToSpeech t1;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
t1 = new TextToSpeech(getActivity().getApplicationContext(), new TextToSpeech.OnInitListener()
{
@Override
public void onInit(int status)
{
if(status != TextToSpeech.ERROR)
{
t1.setLanguage(Locale.UK);
}
}
});
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_home, container, false);
resultTEXT = (TextView)view.findViewById(R.id.TVresult);
resultTEXT2 = (TextView)view.findViewById(R.id.TVresult2);
button = (Button)view.findViewById(R.id.imageButton);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if(v.getId() == R.id.imageButton)
{
promptSpeechInput();
}
}
});
return view;
}
public void promptSpeechInput()
{
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say Something!");
try
{
startActivityForResult(i, 100);
resultTEXT.setText("");
resultTEXT2.setText("");
}
catch(ActivityNotFoundException a)
{
//Toast.makeText.(MainActivity.this, "Sorry your device does not support speech language! ", Toast.LENGTH_LONG).show();
}
}
static final String[] texts =
{
"I am fine","I am Okay","I am Good","Well I am doing Good"
};
static final String[] what1 =
{
"Yes? what can I help you with?",
"What?",
"yes?",
"Yes? how can I help you?"
};
public void ReadText()
{
Random r = new Random();
String wow = texts[r.nextInt(4)];
String what = what1[r.nextInt(4)];
if(toSpeak.equals("how are you") || toSpeak.equals("hi Ashley how are you")|| toSpeak.equals("hey Ashley how are you"))
{
t1.speak(wow, TextToSpeech.QUEUE_FLUSH, null);
resultTEXT2.setText(wow);
}
else if(toSpeak.equals("hey Ashley") || toSpeak.equals("hey")|| toSpeak.equals("Hey")|| toSpeak.equals("Ashley"))
{
t1.speak(what, TextToSpeech.QUEUE_FLUSH, null);
resultTEXT2.setText(what);
}
}
public void onActivityResult(int request_code, int result_code, Intent i)
{
super.onActivityResult(request_code, result_code, i);
switch (request_code)
{
case 100: if(result_code == RESULT_OK && i != null) // I have an error at this line at "RESULT_OK"
{
ArrayList<String> result = i.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String r1 = result.get(0);
resultTEXT.setText(result.get(0));
ReadText();
}
break;
}
}
}
# 1 楼答案
由于您在
Fragment
上,因此必须调用Activity.RESULT_OK
,或者也可以按如下方式使用getActivity().RESULT_OK
:但是在等待结果时,不要忘记调用
getActivity().startActivityForResult(intent);
,例如,您的Intent
应该如下所示:顺便看看这个:
由于您正在传递一个
100
,我不知道您是否也想获得该100
,因此可能是另一个问题# 2 楼答案
RESULT_OK
是活动类的常量,您无法在片段中直接获取它。使用以下内容: