Thread Android error that is crashing the app at the time of opening

Asked

Viewed 54 times

0

I’m doing an initial project to test the functionalities of the Speechrecognizer library from google for android, but at the time if climbing the App I’m having problems, in this case it just closes and without even giving a crash message, the error apparently is related to Thread main(this is a part of the error):

02-11 20:59:00.930 16313-16313/? E/propClient: PropClient failed to load
02-11 20:59:01.077 25677-25677/? E/adbd: recv: OPEN 0000000f 00000000 0015:73 68 65 6C 6C 3A 6C 6F 67 63 61 74 20 2D 76 20 6C 6F 6E 67 00 
service_to_fd: shell:logcat -v long
02-11 20:59:01.090 16315-16315/? E/propClient: PropClient failed to load
02-11 20:59:08.294 19965-19965/? E/rs$DefaultThreadFactory: ===== Detect pool-thread leak =====
02-11 20:59:08.295 19965-19965/? E/rs$DefaultThreadFactory: dalvik.system.VMStack.getThreadStackTrace(Native Method)
java.lang.Thread.getStackTrace(Thread.java:1536) 

Mainactivity.kt

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.provider.Settings
import android.speech.RecognitionListener
import android.speech.RecognizerIntent
import android.speech.SpeechRecognizer
import android.support.v4.content.ContextCompat
import android.util.Log
import java.util.*


class MainActivity : AppCompatActivity() {

lateinit var sr: SpeechRecognizer

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    checkPermission()
    configureSpeechRecognizer()

}

fun configureSpeechRecognizer(){
    sr = SpeechRecognizer.createSpeechRecognizer(this)
    var mSpeechRecognizerIntent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
    mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
    mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault())

    sr.setRecognitionListener(Ouvinte())
}

fun checkPermission(){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED){
            var intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package: $packageName"))
            startActivity(intent)
            finish()
        }
    }
}

class Ouvinte: RecognitionListener{
    var TAG = "speechOuvinte"

    override fun onReadyForSpeech(p0: Bundle?) {
        Log.d(TAG, "onReadyForSpeech")
    }

    override fun onRmsChanged(p0: Float) {
        Log.d(TAG, "onRmsChanged")
    }

    override fun onBufferReceived(p0: ByteArray?) {
        Log.d(TAG, "onBufferReceived")
    }

    override fun onPartialResults(p0: Bundle?) {
        Log.d(TAG, "onPartialResults")
    }

    override fun onEvent(eventType: Int, p1: Bundle?) {
        Log.d(TAG, "onEvent: $eventType")
    }

    override fun onBeginningOfSpeech() {
        Log.d(TAG, "onBeginningOfSpeech")
    }

    override fun onEndOfSpeech() {
        Log.d(TAG, "onEndOfSpeech")
    }

    override fun onError(error: Int) {
        Log.d(TAG, "onError: $error")
    }

    override fun onResults(result: Bundle?) {
        Log.d(TAG, "onReadyForSpeech")

        var data = result!!.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION)

        for(i: Int in data.indices){
            Log.d(TAG, "dados: $data[i]")
        }
    }

}

}

The layout of main is very simple because at the moment I do not use any screen component. I am open to suggestions, even if you want to show me a tutorial for this library that you used and believe will be great learning, please take this question to share! Thank you !

1 answer

0

Heavy operations cannot be done on MainThread, try opening another thread and putting your SpeechRecognizer in it.

    ExecutorService pool = Executors.newSingleThreadExecutor();

    Runnable task = new Runnable() {
        public void run() {
            //seu código pesado aqui
        }
    };

    pool.execute(task);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.