首页 > 解决方案 > 每次执行助手类时代码崩溃

问题描述

每次我打开我的应用程序它都会崩溃。我想从另一个类调用 TTS 方法,所以我创建了 TTSHelper 命名类并添加了 speak 方法,但它不起作用

这是主要活动:

    public class MainActivity extends Activity 
{

    TTSHelper TTSHelp;

    private TextToSpeech myTTS;

    // ON CREATE STAGE 
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);




        // TO CHECK IF I HAVE TTS INSTALLED OR NOT
        initializeTextToSpeech();


    }// END ON CREAE ??





    // CHECKING TTS IS INSTALLED OR NOT
    private void initializeTextToSpeech()
    {

        myTTS = new TextToSpeech(this, new TextToSpeech.OnInitListener(){
                @Override
                public void onInit(int i){
                    if(myTTS.getEngines().size() == 0) {
                        Toast.makeText(MainActivity.this, "No tts engine found"
                                       , Toast.LENGTH_LONG).show();
                        finish();
                    } else {
                        //Toast.makeText(MainActivity.this,"Hi..! i'm i.Vert, here to assist you."
                        //   , Toast.LENGTH_SHORT).show();
                        myTTS.setLanguage(Locale.US);
                        TTSHelp.speak("Hello Sir");

                    }
                }
            });
    }

这是我制作的辅助类:(更新)添加了 oninit 方法

    public class TTSHelper
{
    TextToSpeech myTTS;

  @Override
public void onInit(int status) {        
    if (status == TextToSpeech.SUCCESS) {
        Toast.makeText(TTSHelper.this, 
                       "Text-To-Speech engine is initialized", Toast.LENGTH_LONG).show();
    }
    else if (status == TextToSpeech.ERROR) {
        Toast.makeText(TTSHelper.this, 
                       "Error occurred while initializing Text-To-Speech engine", Toast.LENGTH_LONG).show();
    }
}

// ANSWER OR SPEAK FUNCTION
    public void speak( String message)
    {
        if(Build.VERSION.SDK_INT >= 21) {
            myTTS.speak(message, TextToSpeech.QUEUE_FLUSH, null, null);
        } else {
            myTTS.speak(message, TextToSpeech.QUEUE_FLUSH, null);
        }
    }
}

日志是:

    Late-enabling -Xcheck:jni
01-07 00:44:34.901 31803 31803 E   libc                                         Access denied finding property "persist.vendor.sys.activitylog"
01-07 00:44:34.885 31803 31803 W   re-initialized>                              type=1400 audit(0.0:60604): avc: denied { read } for name="u:object_r:mtk_amslog_prop:s0" dev="tmpfs" ino=7624 scontext=u:r:untrusted_app_25:s0:c512,c768 tcontext=u:object_r:mtk_amslog_prop:s0 tclass=file permissive=0
01-07 00:44:35.222 31803 31803 I   e.martin.satya                               The ClassLoaderContext is a special shared library.
01-07 00:44:35.520 31803 31803 E   BoostFramework                               BoostFramework() : Exception_1 = java.lang.ClassNotFoundException: com.qualcomm.qti.Performance
01-07 00:44:35.521 31803 31803 E   BoostFramework                               BoostFramework() Ux Perf: Exception = java.lang.ClassNotFoundException: com.qualcomm.qti.UxPerformance
01-07 00:44:35.522 31803 31803 E   BoostFramework                               BoostFramework() : Exception_1 = java.lang.ClassNotFoundException: com.qualcomm.qti.Performance
01-07 00:44:35.523 31803 31803 E   BoostFramework                               BoostFramework() Ux Perf: Exception = java.lang.ClassNotFoundException: com.qualcomm.qti.UxPerformance
01-07 00:44:35.803 31803 31803 E   BoostFramework                               BoostFramework() : Exception_1 = java.lang.ClassNotFoundException: com.qualcomm.qti.Performance
01-07 00:44:35.804 31803 31803 E   BoostFramework                               BoostFramework() Ux Perf: Exception = java.lang.ClassNotFoundException: com.qualcomm.qti.UxPerformance
01-07 00:44:35.805 31803 31803 E   BoostFramework                               BoostFramework() : Exception_1 = java.lang.ClassNotFoundException: com.qualcomm.qti.Performance
01-07 00:44:35.806 31803 31803 E   BoostFramework                               BoostFramework() Ux Perf: Exception = java.lang.ClassNotFoundException: com.qualcomm.qti.UxPerformance
01-07 00:44:35.964 31803 31803 I   TextToSpeech                                 Sucessfully bound to com.google.android.tts
01-07 00:44:36.444 31803 31803 W   Activity                                     Slow Operation: Activity com.voice.martin.satyam/.MainActivity onCreate took 991ms
01-07 00:44:36.549 31803 31803 I   SurfaceFactory                               [static] sSurfaceFactory = com.mediatek.view.impl.SurfaceFactoryImpl@b63ff83
01-07 00:44:36.587 31803 31803 D   ViewRootImpl[MainActivity]                   hardware acceleration = true , fakeHwAccelerated = false, sRendererDisabled = false, forceHwAccelerated = false, sSystemRendererDisabled = false
01-07 00:44:36.598 31803 31803 V   PhoneWindow                                  DecorView setVisiblity: visibility = 0, Parent = android.view.ViewRootImpl@753037e, this = DecorView@add10df[MainActivity]
01-07 00:44:36.603 31803 31803 I   TextToSpeech                                 Connected to ComponentInfo{com.google.android.tts/com.google.android.tts.service.GoogleTTSService}
01-07 00:44:36.630 31803 31834 I   TextToSpeech                                 Set up connection to ComponentInfo{com.google.android.tts/com.google.android.tts.service.GoogleTTSService}
01-07 00:44:36.733 31803 31803 D   Surface                                      Surface::allocateBuffers(this=0xa2ffe800)
01-07 00:44:36.815 31803 31803 W   RenderThread                                 type=1400 audit(0.0:60605): avc: denied { search } for name="clients" dev="debugfs" ino=7474 scontext=u:r:untrusted_app_25:s0:c512,c768 tcontext=u:object_r:debugfs_ion:s0 tclass=dir permissive=0
01-07 00:44:36.859 31803 31833 I   ConfigStore                                  android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
01-07 00:44:36.859 31803 31833 I   ConfigStore                                  android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
01-07 00:44:36.859 31803 31833 I   OpenGLRenderer                               Initialized EGL, version 1.4
01-07 00:44:36.859 31803 31833 D   OpenGLRenderer                               Swap behavior 2
01-07 00:44:36.879 31803 31833 D   Surface                                      Surface::connect(this=0xa2ffe800,api=1)
01-07 00:44:36.881 31803 31833 I   libEGL                                       [MTK Game SDK] low_latency_mode(0) pid(-1) property(-1)
01-07 00:44:36.933 31803 31833 E   ion                                          ioctl c0044901 failed with code -1: Invalid argument
01-07 00:44:37.962 31803 31803 D   AndroidRuntime                               Shutting down VM
01-07 00:44:37.965 31803 31803 E   AndroidRuntime                               FATAL EXCEPTION: main
01-07 00:44:37.965 31803 31803 E   AndroidRuntime                               Process: com.voice.martin.satyam, PID: 31803
01-07 00:44:37.965 31803 31803 E   AndroidRuntime                               java.lang.NullPointerException: Attempt to invoke virtual method 'void com.voice.martin.satyam.TTSHelper.speak(java.lang.String)' on a null object reference
01-07 00:44:37.965 31803 31803 E   AndroidRuntime                               at com.voice.martin.satyam.MainActivity$100000000.onInit(MainActivity.java:170)
01-07 00:44:37.965 31803 31803 E   AndroidRuntime                               at android.speech.tts.TextToSpeech.dispatchOnInit(TextToSpeech.java:831)

我怎样才能解决这个问题?添加 oninit 方法后,我仍然无法解决问题。

标签: javaandroid

解决方案


TextToSpeech myTTS从未初始化,因此它抱怨:

java.lang.NullPointerException: 尝试void com.voice.martin.satyam.TTSHelper.speak(java.lang.String)在空对象引用上调用虚拟方法“”。

你需要类似的东西public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener


推荐阅读