I have a problem in my android app. The problem is Speech Recogntion is not working when it reaches the method startActivityForResult (intent, int) it shows that I have to add Bundle. The Speech recognition is added in a class that is called inside Adaper class not in MainActivity as people doing.
Here is the code for Speech Recognition
public class SpeechRecog extends Activity {
private static final int VOICE_RECOGNITION_REQUEST_CODE = 1001;
ArrayList<String> thingsYouSaid = new ArrayList<String>();
public void Start(Intent i, Context c) {
i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
i.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Are You Done , yet? ");
//try{
startActivityForResult(i, VOICE_RECOGNITION_REQUEST_CODE);
//}catch(Exception e){
//Toast.makeText(c, "SpeechRecogntion is not avalible on your device ", Toast.LENGTH_LONG).show();
//}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if ((requestCode ==
VOICE_RECOGNITION_REQUEST_CODE) && (resultCode == RESULT_OK)) {
thingsYouSaid = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if (checkResult())
Toast.makeText(getApplicationContext(),
"You are done ", Toast.LENGTH_LONG).show();
else
Toast.makeText(getApplicationContext(),
"You are not done ", Toast.LENGTH_LONG).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
public boolean checkResult() {
for (int i = 0; i < thingsYouSaid.size(); i++) {
if ((thingsYouSaid.get(i).equals("done"))
|| (thingsYouSaid.get(i).equals("yes")))
return true;
}
//if (textResult.equals("done")||(textResult.equals("yes")))
//return true ;
return false;
}
}
Here the OnclickListenre inside adapter class
Lh.btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String[] items = db.GetItemArray(Lh.text);
String[] itemFlags = db.GetItemsFlagsArray(Lh.text);
//final itemHolder ihc = new itemHolder();
if (Lh.btn.getText().equals("read")) {
Lh.btn.setText("Pause");
int i;
for (i = 0; i < items.length; i++) {
if (Integer.valueOf(itemFlags[i]) == 1) {
continue;
}
int timeofitem = Integer.parseInt(db.getItemTime(items[i]));
//Toast.makeText(context, items[i] , Toast.LENGTH_SHORT).show();
TTS.Talk(items[i], 1);
TTS.Scilence(timeofitem);
SpeechRecog SP = new SpeechRecog();
SP.Start(new Intent(), context);
// checking items
ItemsFields itemToBeChanged = new ItemsFields(Lh.text, items[i], 0, timeofitem);
itemToBeChanged.setFlag(1);
db.updateItems(itemToBeChanged);
//ihc.checkbox.setChecked(true);
}
TTS.Talk("Congrats ,Your Checklist is Completed ", 1);
} else {
Lh.btn.setText("read");
TTS.Talk("The Checklist is paused , Press again if you want to continue , Bye", 0);
return;
}
}
});
Here is what Debug says
Thread [<1> main] (Suspended (exception NullPointerException))
<VM does not provide monitor information>
SpeechRecog(Activity).startActivityForResult(Intent, int, Bundle) line: 3464
SpeechRecog(Activity).startActivityForResult(Intent, int) line: 3425
SpeechRecog.Start(Intent, Context) line: 24
ViewAdapter$1.onClick(View) line: 140
Button(View).performClick() line: 4475
View$PerformClick.run() line: 18786
Handler.handleCallback(Message) line: 730
ViewRootImpl$ViewRootHandler(Handler).dispatchMessage(Message) line: 92
Looper.loop() line: 176
ActivityThread.main(String[]) line: 5419
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 525
ZygoteInit$MethodAndArgsCaller.run() line: 1046
ZygoteInit.main(String[]) line: 862
NativeStart.main(String[]) line: not available [native method]
It shows this weird code after debging with highlight on * <var>flagsMask</var>
/**
* Like {@link #startActivityForResult(Intent, int)}, but allowing you
* to use a IntentSender to describe the activity to be started. If
* the IntentSender is for an activity, that activity will be started
* as if you had called the regular {@link #startActivityForResult(Intent, int)}
* here; otherwise, its associated action will be executed (such as
* sending a broadcast) as if you had called
* {@link IntentSender#sendIntent IntentSender.sendIntent} on it.
*
* @param intent The IntentSender to launch.
* @param requestCode If >= 0, this code will be returned in
* onActivityResult() when the activity exits.
* @param fillInIntent If non-null, this will be provided as the
* intent parameter to {@link IntentSender#sendIntent}.
* @param flagsMask Intent flags in the original IntentSender that you
* would like to change.
* @param flagsValues Desired values for any bits set in
* <var>flagsMask</var>
* @param extraFlags Always set to 0.
* @param options Additional options for how the Activity should be started.
* See {@link android.content.Context#startActivity(Intent, Bundle)
* Context.startActivity(Intent, Bundle)} for more details. If options
* have also been supplied by the IntentSender, options given here will
* override any that conflict with those given by the IntentSender.
*/
public void startIntentSenderForResult(IntentSender intent, int requestCode,
Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags,
Bundle options) throws IntentSender.SendIntentException {
if (mParent == null) {
startIntentSenderForResultInner(intent, requestCode, fillInIntent,
flagsMask, flagsValues, this, options);
} else if (options != null) {
mParent.startIntentSenderFromChild(this, intent, requestCode,
fillInIntent, flagsMask, flagsValues, extraFlags, options);
} else {
// Note we want to go through this call for compatibility with
// existing applications that may have overridden the method.
mParent.startIntentSenderFromChild(this, intent, requestCode,
fillInIntent, flagsMask, flagsValues, extraFlags);
}
}
Here is what log cat says
05-04 23:44:00.845: D/libEGL(24183): loaded /system/lib/egl/libEGL_mali.so
05-04 23:44:00.850: D/libEGL(24183): loaded /system/lib/egl/libGLESv1_CM_mali.so
05-04 23:44:00.855: D/libEGL(24183): loaded /system/lib/egl/libGLESv2_mali.so
05-04 23:44:00.865: E/(24183): Device driver API match
05-04 23:44:00.865: E/(24183): Device driver API version: 23
05-04 23:44:00.865: E/(24183): User space API version: 23
05-04 23:44:00.865: E/(24183): mali: REVISION=Linux-r3p2-01rel3 BUILD_DATE=Wed Oct 9 21:05:57 KST 2013
05-04 23:44:00.970: D/OpenGLRenderer(24183): Enabling debug mode 0
05-04 23:44:01.020: D/AbsListView(24183): unregisterIRListener() is called
05-04 23:44:01.045: D/AbsListView(24183): unregisterIRListener() is called
05-04 23:44:01.080: D/AbsListView(24183): unregisterIRListener() is called
05-04 23:44:01.170: D/AbsListView(24183): unregisterIRListener() is called
05-04 23:44:02.630: D/AbsListView(24183): Get MotionRecognitionManager
05-04 23:44:02.785: W/ResourceType(24183): Invalid package identifier when getting bag for resource number 0xffffffff
05-04 23:44:02.795: W/ResourceType(24183): Invalid package identifier when getting bag for resource number 0xffffffff
05-04 23:44:02.805: W/ResourceType(24183): Invalid package identifier when getting bag for resource number 0xffffffff
05-04 23:44:02.810: W/ResourceType(24183): Invalid package identifier when getting bag for resource number 0xffffffff
Never instantiate activities with new
.
Use an Intent
to launch new activities or use an existing activity instantiated by the framework for onActivityResult()
.
For the latter, replace
SpeechRecog SP = new SpeechRecog();
SP.Start(new Intent(), context);
with the code from Start()
(ie setting up an Intent
and calling startActivityForResult()
and pull onActivityResult()
to that activity.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.