简体   繁体   English

接受Google文字转语音输入后,应用程序自动关闭

[英]App closes automatically after taking google text-to-speech input

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. 我正在开发一个Android应用程序,该应用程序可以接受语音输入并使用Google的API转换为文本,然后将文本放入文本字​​段。 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. 现在出现的问题是,当SpeechInput对话框出现并且接受语音输入后,应用程序关闭,这不会将转换后的语音放入Edittext字段中。 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. 您的onActivityResult()方法无法区分从不同活动收到的结果。 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 . 你有(至少),你推出一个独立的活动,并期望而产生的两个地方:在onCreate()当您尝试登录使用的请求码火力地堡SIGN_IN_REQUEST_CODEgetSpeechInput()当您使用启动语音识别活动请求代码为10。特别是,只要结果代码(不是请求代码)是SIGN_IN_REQUEST_CODE以外的任何其他值,您就在调用finish() (Note that RESULT_OK , the result code usually used to signal success, has a value of -1.) (请注意,通常用于表示成功的结果代码RESULT_OK的值为-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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM