麦克风输入

时间:2019-03-18 10:45:06

标签: android nullpointerexception

我正在尝试从麦克风获取输入,并在我的edittext中显示该信息,而我曾尝试在其他活动中进行此操作,它可以正常工作,但是当我尝试将其附加到我的登录活动中时,一旦完成输入就可以了从麦克风我的应用程序崩溃。在其他活动上运行正常,它在我的edittext上显示了我的输入,但是此活动出了点问题,我找不到解决方法

public class login extends AppCompatActivity implements View.OnClickListener {
    private TextToSpeech tts;
    private EditText txv;

    public boolean dispatchKeyEvent(KeyEvent event) {
        int action = event.getAction();
        int keyCode = event.getKeyCode();
        switch (keyCode) {
            case KeyEvent.KEYCODE_VOLUME_UP:
                if (action == KeyEvent.ACTION_DOWN) {
                    int count=1;
                    if(count==1){
                    Intent in1=new Intent(login.this, Signup.class);
                    startActivity(in1);}
                    if(count==2)
                    {

                    }
                }
                return true;
            case KeyEvent.KEYCODE_VOLUME_DOWN:
                if (action == KeyEvent.ACTION_DOWN) {

                    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());

                    if (intent.resolveActivity(getPackageManager()) != null) {
                        startActivityForResult(intent, 10);
                    } else {
                        Toast.makeText(this, "Your Device Don't Support Speech Input", Toast.LENGTH_SHORT).show();
                    }
                }
                return true;
            default:
                return super.dispatchKeyEvent(event);
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        txv = (EditText) findViewById(R.id.tx2);
        setContentView(R.layout.activity_login);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        findViewById(R.id.signup).setOnClickListener(this);
        initializeTextToSpeech();

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {



            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    private void initializeTextToSpeech() {
        tts= new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if(tts.getEngines().size()==0) {
                    Toast.makeText(login.this, "There's no TTS engine on your Device"
                            , Toast.LENGTH_LONG).show();
                    finish();
                } else{
                    tts.setLanguage(Locale.US);
                    tts.setSpeechRate(0.7f);
                    speak(" For new account Press volume up button 1 time and for login press volume up button 2 times.......To enter your username, press volume down button to activate your mic and speak your username");

                }
            }

        });

    }

    protected void onPause(){
        super.onPause();
        tts.shutdown();}

    public void speak(String message){
        if(Build.VERSION.SDK_INT >= 21) {
            tts.speak(message, TextToSpeech.QUEUE_FLUSH,null,null);
        }
        else {
            tts.speak(message,TextToSpeech.QUEUE_FLUSH,null);
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case 10:
                if (resultCode == RESULT_OK && data != null) {
                    ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                    txv.setText(result.get(0));
                }
                break;
        }
    }

    @Override
    public void onClick(View v) {
      switch (v.getId()){

          case R.id.signup:

              startActivity(new Intent(this, Signup.class));
              break;


      }
    }
}

Stacktrace

 --------- beginning of crash
2019-03-18 15:53:00.801 9755-9755/com.example.naji.finalyear E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.naji.finalyear, PID: 9755
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=10, result=-1, data=Intent { (has extras) }} to activity {com.example.naji.finalyear/com.example.naji.finalyear.login}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4053)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4096)
        at android.app.ActivityThread.-wrap20(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1516)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference
        at com.example.naji.finalyear.login.onActivityResult(login.java:111)
        at android.app.Activity.dispatchActivityResult(Activity.java:6915)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4049)
 

1 个答案:

答案 0 :(得分:3)

onCreate中,您需要先呼叫setContentView,然后再使用任何findViewById呼叫。

setContentView(R.layout.activity_login);
txv = (EditText) findViewById(R.id.tx2);

这两行只需要翻转即可。