我想创建一个应用程序,在ListView中显示来自本地数据库的数据,允许您向数据库添加字符串,然后读取所选的任何单词。对于OO目的,我想要一个可重复使用的TTS-Object。
目前一切都已实现,如果我在ListView中选择一个单词,则该条目显示为toast,这意味着我有一个单词的字符串。
问题:当我创建TTS对象时,我收到NullPointerException。
以下代码显示了当ListView中的条目被选中时会发生什么:
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor cursor = (SQLiteCursor) list.getItemAtPosition(position);
String title = cursor.getString(1);
Toast.makeText(getApplicationContext(), "selected: " + title , Toast.LENGTH_SHORT).show();
//--> App chrashes here
AndroidTextToSpeechActivity attsa = new AndroidTextToSpeechActivity();
attsa.speakOut(title);
}
}); }
这是logcat:
07-22 20:25:58.718:W / dalvikvm(18373):threadid = 1:线程退出,未捕获异常(group = 0x40a6b1f8) 07-22 20:25:58.738:E / AndroidRuntime(18373):致命异常:主要 07-22 20:25:58.738:E / AndroidRuntime(18373):java.lang.NullPointerException 07-22 20:25:58.738:E / AndroidRuntime(18373):在android.content.ContextWrapper.getPackageName(ContextWrapper.java:127) 07-22 20:25:58.738:E / AndroidRuntime(18373):在android.speech.tts.TextToSpeech。(TextToSpeech.java:545) 07-22 20:25:58.738:E / AndroidRuntime(18373):在android.speech.tts.TextToSpeech。(TextToSpeech.java:522) 07-22 20:25:58.738:E / AndroidRuntime(18373):在android.speech.tts.TextToSpeech。(TextToSpeech.java:507) 07-22 20:25:58.738:E / AndroidRuntime(18373):at eu.tuts.dbexample.obj.AndroidTextToSpeechActivity。(AndroidTextToSpeechActivity.java:20) 07-22 20:25:58.738:E / AndroidRuntime(18373):at eu.tuts.dbexample.activities.Datenbanken $ 1.onItemClick(Datenbanken.java:79) 07-22 20:25:58.738:E / AndroidRuntime(18373):在android.widget.AdapterView.performItemClick(AdapterView.java:292) 07-22 20:25:58.738:E / AndroidRuntime(18373):在android.widget.AbsListView.performItemClick(AbsListView.java:1060) 07-22 20:25:58.738:E / AndroidRuntime(18373):在android.widget.AbsListView $ PerformClick.run(AbsListView.java:2516) 07-22 20:25:58.738:E / AndroidRuntime(18373):在android.widget.AbsListView $ 1.run(AbsListView.java:3170) 07-22 20:25:58.738:E / AndroidRuntime(18373):在android.os.Handler.handleCallback(Handler.java:605) 07-22 20:25:58.738:E / AndroidRuntime(18373):在android.os.Handler.dispatchMessage(Handler.java:92) 07-22 20:25:58.738:E / AndroidRuntime(18373):在android.os.Looper.loop(Looper.java:137) 07-22 20:25:58.738:E / AndroidRuntime(18373):在android.app.ActivityThread.main(ActivityThread.java:4575) 07-22 20:25:58.738:E / AndroidRuntime(18373):at java.lang.reflect.Method.invokeNative(Native Method) 07-22 20:25:58.738:E / AndroidRuntime(18373):at java.lang.reflect.Method.invoke(Method.java:511) 07-22 20:25:58.738:E / AndroidRuntime(18373):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:789) 07-22 20:25:58.738:E / AndroidRuntime(18373):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556) 07-22 20:25:58.738:E / AndroidRuntime(18373):at dalvik.system.NativeStart.main(Native Method)
这里是TTS对象的完整源代码。很抱歉发布完整的Sorcecode - 我不知道错误发生在哪里。
package eu.tuts.dbexample.obj;
import java.util.Locale;
import eu.tuts.dbexample.R;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class AndroidTextToSpeechActivity extends Activity implements TextToSpeech.OnInitListener {
/** Called when the activity is first created. */
private TextToSpeech tts = new TextToSpeech(this, this);
@Override
public void onDestroy() {
// Don't forget to shutdown tts!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
//speakOut();
Log.d("TTS", "TTS init okay");
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
public void speakOut(String text) {
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}
答案 0 :(得分:0)
您无法在Activity的构造函数中创建TextToSpeech
实例,因为TextToSpeech
的构造函数中的第一个参数是Context
,并且在onCreate()
之前未设置上下文在活动中调用{1}}。
改变这个:
private TextToSpeech tts = new TextToSpeech(this, this);
到此:
private TextToSpeech tts;
并将此代码放入onCreate()
您的活动中:
tts = new TextToSpeech(this, this);