gpt4 book ai didi

java - 每次执行辅助类时代码都会崩溃

转载 作者:行者123 更新时间:2023-12-02 01:09:08 25 4
gpt4 key购买 nike

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

这是主要 Activity :

    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 方法后我仍然无法解决问题。

最佳答案

TextToSpeech myTTS从未初始化,因此它提示:

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.voice.martin.satyam.TTSHelper.speak(java.lang.String)' on a null object reference.

您需要类似的东西public class MainActivity extends AppCompatActivity Implements TextToSpeech.OnInitListener

关于java - 每次执行辅助类时代码都会崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59618041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com