0

こんにちは、チュートリアルからアプリに電卓を含めましたが、電卓は正常に動作しますが、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)
4

1 に答える 1

1

追加する必要があると思います

myTTS = new TextToSpeech(this, this);

onCreate()あなたの活動の中で。お役に立てれば。それはあなたがヌルになっているところです。onActivityResult後で使用するために初期化しました。ただし、使用する前に初期化する必要もあります。

于 2013-10-23T03:33:03.573 に答える