user1480139
user1480139

Reputation: 383

Android speech to text works in emulator but not on phone

I'm trying to write a speech to text app for android. Now the app doesn't crash on the emulator, but when I upload it to a samsung galaxy s 1900 I get following error messages in eclipse:

11-05 17:43:48.814: I/dalvikvm(3092): Could not find method com.example.speechtotext.MainActivity.getActionBar, referenced from method com.example.speechtotext.MainActivity.onCreate
11-05 17:43:48.818: W/dalvikvm(3092): VFY: unable to resolve virtual method 3036: Lcom/example/speechtotext/MainActivity;.getActionBar ()Landroid/app/ActionBar;
11-05 17:43:48.818: D/dalvikvm(3092): VFY: replacing opcode 0x6e at 0x0009
11-05 17:43:48.818: D/dalvikvm(3092): VFY: dead code 0x000c-0061 in Lcom/example/speechtotext/MainActivity;.onCreate (Landroid/os/Bundle;)V
11-05 17:43:48.896: D/AndroidRuntime(3092): Shutting down VM
11-05 17:43:48.900: W/dalvikvm(3092): threadid=1: thread exiting with uncaught exception (group=0x4001d7d0)
11-05 17:43:48.900: E/AndroidRuntime(3092): FATAL EXCEPTION: main
11-05 17:43:48.900: E/AndroidRuntime(3092): java.lang.NoSuchMethodError: com.example.speechtotext.MainActivity.getActionBar
11-05 17:43:48.900: E/AndroidRuntime(3092):     at com.example.speechtotext.MainActivity.onCreate(MainActivity.java:40)
11-05 17:43:48.900: E/AndroidRuntime(3092):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-05 17:43:48.900: E/AndroidRuntime(3092):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-05 17:43:48.900: E/AndroidRuntime(3092):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-05 17:43:48.900: E/AndroidRuntime(3092):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-05 17:43:48.900: E/AndroidRuntime(3092):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-05 17:43:48.900: E/AndroidRuntime(3092):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-05 17:43:48.900: E/AndroidRuntime(3092):     at android.os.Looper.loop(Looper.java:123)
11-05 17:43:48.900: E/AndroidRuntime(3092):     at android.app.ActivityThread.main(ActivityThread.java:4627)
11-05 17:43:48.900: E/AndroidRuntime(3092):     at java.lang.reflect.Method.invokeNative(Native Method)
11-05 17:43:48.900: E/AndroidRuntime(3092):     at java.lang.reflect.Method.invoke(Method.java:521)
11-05 17:43:48.900: E/AndroidRuntime(3092):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
11-05 17:43:48.900: E/AndroidRuntime(3092):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
11-05 17:43:48.900: E/AndroidRuntime(3092):     at dalvik.system.NativeStart.main(Native Method)
11-05 17:44:11.343: I/dalvikvm(3092): threadid=3: reacting to signal 3
11-05 17:44:12.724: E/dalvikvm(3092): Failed to write stack traces to /data/anr/traces.txt (805 of 2366): Unknown error: 0
11-05 17:44:22.165: I/Process(3092): Sending signal. PID: 3092 SIG: 9

firmware version 2.2 on the phone.

When running in emulator the program just sais that speech isn't supported in the emulator, no crash though.

This my code:

package com.example.speechtotext;

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

import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.speech.RecognizerIntent;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity implements OnClickListener,
        OnInitListener {

    private int MY_DATA_CHECK_CODE = 0;
    private TextToSpeech myTTS;
    private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;

    private ListView mList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        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);

        // Get display items for later interaction
        Button speakButton2 = (Button) findViewById(R.id.btn_speak);

        mList = (ListView) findViewById(R.id.list);

        // Check to see if a recognition activity is present
        PackageManager pm = getPackageManager();
        List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
        if (activities.size() != 0) {
            speakButton2.setOnClickListener(this);
        } else {
            speakButton2.setEnabled(false);
            speakButton2.setText("Recognizer not present");
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    // Indien men op de speakbutton klikt
    public void onClick(View arg0) {
        EditText enteredText = (EditText) findViewById(R.id.enter);
        String words = enteredText.getText().toString();
        speakWords(words);

        if (arg0.getId() == R.id.btn_speak) {
            startVoiceRecognitionActivity();
        }
    }

    // Zorgt voor de spraak
    private void speakWords(String speech) {
        // myTTS.speak(speech, TextToSpeech.QUEUE_ADD, null); zorgt dat de app
        // het toevoegt aan de queue zodat het wacht tot de vorige speech
        // opdracht uitgesproken is
        myTTS.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
    }

    // Controleren of de data beschikbaar is
    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);
            }
        }

        if (requestCode == VOICE_RECOGNITION_REQUEST_CODE
                && resultCode == RESULT_OK) {
            // Fill the list view with the strings the recognizer thought it
            // could have heard
            ArrayList<String> matches = data
                    .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            mList.setAdapter(new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, matches));
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    private void startVoiceRecognitionActivity() {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
                "Speech recognition demo");
        startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
    }

    // Taal juist zetten en indien er een fout is geeft men een foutboodschap
    public void onInit(int initStatus) {
        if (initStatus == TextToSpeech.SUCCESS) {
            myTTS.setLanguage(Locale.US);
        } else if (initStatus == TextToSpeech.ERROR) {
            Toast.makeText(this, "Sorry! Text To Speech failed...",
                    Toast.LENGTH_LONG).show();
        }
    }

}

I've checked that the min API isn't to high, but eclipse has set that to 2.2 so I think thats fine.

Someone an idea what could be wrong or how to debug?

Kind regards,

Upvotes: 0

Views: 1169

Answers (1)

Tomik
Tomik

Reputation: 23977

The problem is that you are trying to use ActionBar on device running Android 2.2 (API level 8), but ActionBar was added in API level 11.

So you have basically two options here:

  1. Don't use ActionBar in your app.
  2. Use ActionBarSherlock library, it backports action bar back to Android 2.x (though you still need to make some minor changes in your code, read the docs of ActionBarSherlock for more details).

Upvotes: 2

Related Questions