I am developing an android application that can take speech input and convert to text using Google's API and then put the text into a text field. And that text can be sent using a button. The problem that occurs now, when the SpeechInput dialogue appears and after taking the speech input, the app closes down, that doesn't put the converted speech into the Edittext field. The app just closes down. What is the issue, any help would be appreciated. Here's a snippet of my source code:
public class MainActivity extends AppCompatActivity {
TextView tr;
ImageButton m;
String x;
EditText i;
private static int SIGN_IN_REQUEST_CODE = 1;
FirebaseListAdapter<ChatMessage> adapter;
RelativeLayout activity_main;
FloatingActionButton fab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activity_main = findViewById(R.id.activity_main);
fab = (FloatingActionButton) findViewById(R.id.fab);
//m = (ImageButton) findViewById(R.id.imageButton3);
i = (EditText) findViewById(R.id.getSpeechInput);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getSpeechInput();
i.setText(x, TextView.BufferType.EDITABLE);
FirebaseDatabase.getInstance().getReference().push().setValue(new ChatMessage(i.getText().toString(), FirebaseAuth.getInstance().getCurrentUser().getEmail()));
i.setText("");
}
});
if (FirebaseAuth.getInstance().getCurrentUser() == null) {
startActivityForResult(AuthUI.getInstance().createSignInIntentBuilder().build(), SIGN_IN_REQUEST_CODE);
} else {
Snackbar.make(activity_main, "Welcome " + FirebaseAuth.getInstance().getCurrentUser().getEmail(), Snackbar.LENGTH_LONG).show();
displayChatMessage();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.menu_sign_out) {
AuthUI.getInstance().signOut(this).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Snackbar.make(activity_main, "You have been signed out..", Snackbar.LENGTH_SHORT).show();
finish();
}
});
}
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == SIGN_IN_REQUEST_CODE) {
Snackbar.make(activity_main, "Sussesfully Signed in.Welcome!!", Snackbar.LENGTH_SHORT).show();
displayChatMessage();
} else {
Snackbar.make(activity_main, "We could not Sign in", Snackbar.LENGTH_SHORT).show();
finish();
}
}
private void displayChatMessage() {
ListView l = (ListView) findViewById(R.id.list_of_massage);
adapter = new FirebaseListAdapter<ChatMessage>(this, ChatMessage.class, R.layout.list_item, FirebaseDatabase.getInstance().getReference()) {
@Override
protected void populateView(View v, ChatMessage model, int position) {
TextView m1, m2, m3;
m1 = (TextView) v.findViewById(R.id.message_text);
m2 = (TextView) v.findViewById(R.id.message_user);
m3 = (TextView) v.findViewById(R.id.message_time);
m1.setText(model.getMessageText());
m2.setText(model.getMessageUser());
m3.setText(DateFormat.format("dd-MM-YYYY(HH:mm:ss)", model.getMessageTime()));
}
};
l.setAdapter(adapter);
}
/*private void displayChatMassage(){
}*/
public void getSpeechInput() {
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());
if (i.resolveActivity(getPackageManager()) != null) {
startActivityForResult(i, 10);
} else {
Toast.makeText(getApplicationContext(), "Input is not supported in the device", Toast.LENGTH_LONG).show();
}
}
}
Your onActivityResult()
method is not distinguishing between results received from different activities. You have (at least) two places where you launch a separate activity and expect a result: in onCreate()
when you attempt a Firebase sign-in using a request code of SIGN_IN_REQUEST_CODE
and in getSpeechInput()
when you launch a speech recognition activity using a request code of 10. In particular, you are calling finish()
whenever the result code (not the request code) is anything other than SIGN_IN_REQUEST_CODE
. (Note that RESULT_OK
, the result code usually used to signal success, has a value of -1.)
Try something like this:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SIGN_IN_REQUEST_CODE) { // NOT resultCode
if (resultCode == RESULT_OK) {
Snackbar.make(activity_main, "Sussesfully Signed in.Welcome!!", Snackbar.LENGTH_SHORT).show();
displayChatMessage();
} else {
Snackbar.make(activity_main, "We could not Sign in", Snackbar.LENGTH_SHORT).show();
finish();
}
} else if (requestCode == 10) {
// process result of speech recognition activity
}
}