L
LHomme
Neues Mitglied
- 0
Hi Leute,
bin ein Neueinsteiger auf dem Gebiet der App-Enwicklung (habe gestern erst damit angefangen) und komme ursprünglich aus dem C#-Bereich. Meine IDE ist Android Studio 3.4.
Folgender Java-Code liefert mir eine NULL-Exception:
Bei dem Programm geht es lediglich darum, eine Sprachnachricht auszugeben. Wie aus meinem Kommentar ersichtlich, habe ich überhaupt keine Idee, warum dem tts-Objekt kein Wert zugewiesen wird.
Der Stacktrace sagt mir dazu folgendes:
Der Fehler tritt in der Zeile mit dem Aufruf tts.speak("Hallo!", TextToSpeech.QUEUE_FLUSH, null); auf.
Warum aber wird tts nicht initialisiert? Ich hoffe, ihr könnt helfen.
Liebe Grüße und einen schönen Restsonntag.
bin ein Neueinsteiger auf dem Gebiet der App-Enwicklung (habe gestern erst damit angefangen) und komme ursprünglich aus dem C#-Bereich. Meine IDE ist Android Studio 3.4.
Folgender Java-Code liefert mir eine NULL-Exception:
Code:
package de.androidnewcomer.saghallo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {
private TextToSpeech tts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tts = new TextToSpeech(this,this);
}
@Override
public void onInit(int status){
tts.speak("Hallo!", TextToSpeech.QUEUE_FLUSH, null); // ergibt eine - aus mir unbekannten Gründen - NULL-Exception!
}
}
Bei dem Programm geht es lediglich darum, eine Sprachnachricht auszugeben. Wie aus meinem Kommentar ersichtlich, habe ich überhaupt keine Idee, warum dem tts-Objekt kein Wert zugewiesen wird.
Der Stacktrace sagt mir dazu folgendes:
Code:
2019-06-30 15:13:48.397 4732-4732/de.androidnewcomer.saghallo E/AndroidRuntime: FATAL EXCEPTION: main
Process: de.androidnewcomer.saghallo, PID: 4732
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.androidnewcomer.saghallo/de.androidnewcomer.saghallo.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.speech.tts.TextToSpeech.speak(java.lang.String, int, java.util.HashMap)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2919)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3054)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1814)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:280)
at android.app.ActivityThread.main(ActivityThread.java:6706)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.speech.tts.TextToSpeech.speak(java.lang.String, int, java.util.HashMap)' on a null object reference
at de.androidnewcomer.saghallo.MainActivity.onInit(MainActivity.java:21)
at android.speech.tts.TextToSpeech.dispatchOnInit(TextToSpeech.java:831)
at android.speech.tts.TextToSpeech.initTts(TextToSpeech.java:809)
at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:740)
at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:719)
at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:703)
at de.androidnewcomer.saghallo.MainActivity.onCreate(MainActivity.java:16)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2899)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3054)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1814)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:280)
at android.app.ActivityThread.main(ActivityThread.java:6706)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2019-06-30 15:13:48.405 4732-4732/? I/Process: Sending signal. PID: 4732 SIG: 9
Der Fehler tritt in der Zeile mit dem Aufruf tts.speak("Hallo!", TextToSpeech.QUEUE_FLUSH, null); auf.
Warum aber wird tts nicht initialisiert? Ich hoffe, ihr könnt helfen.
Liebe Grüße und einen schönen Restsonntag.