如何将tts添加到mresult

时间:2013-10-23 02:17:54

标签: android text-to-speech

你好我已经在我的应用程序中包含了一个计算器,计算器工作正常但是当我尝试添加一个tts引擎来读取数字或答案时,应用程序强制关闭代码是

package com.martinsapp.socialstories;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.content.Intent;
import java.util.Locale;
import android.widget.Toast;
import android.view.View.OnClickListener;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class CalcActivity extends Activity implements OnClickListener, OnInitListener {
private EditText Scr;
private float NumberBf;
private String Operation;
private ButtonClickListener btnClick;
private TextToSpeech myTTS;
private int MY_DATA_CHECK_CODE = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_calc);
    Scr = (EditText) findViewById(R.id.editText);
    Scr.setEnabled(false);
    btnClick = new ButtonClickListener();
    Button speakButton = (Button)findViewById(R.id.speak);
    speakButton.setOnClickListener(this);
    Intent checkTTSIntent = new Intent();
    checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
    startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);


    int idList[] =        {   
R.id.button0,R.id.button1,R.id.button2,R.id.button3,R.id.button4,R.id.button5,
R.id.button6            ,R.id.button7,

 R.id.button8,R.id.button9,R.id.buttonDot,R.id.buttonAdd,R.id.buttonSub,      
R.d.buttonMul,R.id.buttonDiv,R.id.buttonEq,R.id.buttonC
};

    for(int id:idList){
        View v =(View) findViewById(id);
        v.setOnClickListener(btnClick);
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.calc, menu);
    return true;
}

public void mMath(String str){
    NumberBf = Float.parseFloat(Scr.getText().toString());
    Operation = str;
    Scr.setText("0");
}
public void getKeyboard(String str){
    String ScrCurrent = Scr.getText().toString();
    ScrCurrent += str;
    Scr.setText(ScrCurrent);

}
public void mResult() {
    float NumAf = Float.parseFloat(Scr.getText().toString());
    float result = 0;
    if(Operation.equals("+")){
        result = NumAf + NumberBf;
    }
    if(Operation.equals("-")){
        result = NumberBf - NumAf;
    }
    if(Operation.equals("*")){
        result = NumAf * NumberBf;
    }
    if(Operation.equals("/")){
        result = NumberBf / NumAf;
    }
Scr.setText(String.valueOf(result));
}

public void onClick(View v) {
    //get the text entered
    EditText enteredText = (EditText)findViewById(R.id.editText);
    String words = enteredText.getText().toString();
    speakWords(words);
}
private void speakWords(String speech) {
    //speak straight away
    myTTS.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == MY_DATA_CHECK_CODE) {
        if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
            myTTS = new TextToSpeech(this, this);
        }
        else {Intent installTTSIntent = new Intent();
            installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
            startActivity(installTTSIntent);
         }
    }
}
public void onInit(int initStatus) {
    //check for successful instantiation
    if (initStatus == TextToSpeech.SUCCESS) {
        if(myTTS.isLanguageAvailable(Locale.US)==TextToSpeech.LANG_AVAILABLE)
            myTTS.setLanguage(Locale.US);
    }
    else if (initStatus == TextToSpeech.ERROR) {
        Toast.makeText(this, "Sorry! Text To Speech failed...",            
Toast.LENGTH_LONG).show();
    }
}



private class ButtonClickListener implements View.OnClickListener{
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.buttonC:
                Scr.setText("0");
                NumberBf = 0;
                Operation = "";
                break;
            case R.id.buttonAdd:
                mMath("+");
                break;
            case R.id.buttonSub:
                mMath("-");
                break;
            case R.id.buttonMul:
                mMath("*");
                break;
            case R.id.buttonDiv:
                mMath("/");
                break;
            case R.id.buttonEq:
                mResult();
                break;
            default:
                String numb = ((Button) v).getText().toString();
                getKeyboard(numb);
                break;

        }
    }
}
}

logcat用speakWord方法说明了我从这里复制了教程http://mobile.tutsplus.com/tutorials/android/android-sdk-using-the-text-to-speech-engine/

这是我的logcat

10-23 02:54:50.145  12818-12818/com.martinsapp.socialstories W/dalvikvm﹕ threadid=1:
          thread exiting with uncaught exception (group=0x415632d0)
10-23 02:54:50.150  12818-12818/com.martinsapp.socialstories E/AndroidRuntime﹕ FATAL
 EXCEPTION: main
java.lang.NullPointerException
        at com.martinsapp.socialstories.CalcActivity.speakWords(CalcActivity.java:93)
        at com.martinsapp.socialstories.CalcActivity.onClick(CalcActivity.java:89)
        at android.view.View.performClick(View.java:4101)
        at android.view.View$PerformClick.run(View.java:17078)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:155)
        at android.app.ActivityThread.main(ActivityThread.java:5485)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)

1 个答案:

答案 0 :(得分:1)

我认为你需要添加

myTTS = new TextToSpeech(this, this);

在你活动的onCreate()中。希望这可以帮助。这是你得到null的地方。您已在onActivityResult初始化以供日后使用。但它也应该在使用之前进行初始化。