我收到错误,一旦我在模拟器上执行以下程序,应用程序就意外停止了:
package com.example.TextSpeaker2;
import java.util.Locale;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.widget.Button;
import android.widget.Toast;
public class TextSpeaker2 extends Activity implements OnInitListener{
public static final Uri SMS_CONTENT_URI = Uri.parse("content://sms");
public static final Uri SMS_INBOX_CONTENT_URI = Uri.withAppendedPath(SMS_CONTENT_URI, "inbox");
int MY_DATA_CHECK_CODE = 0;
public static TextToSpeech mtts;
public static String body="";
Button speak;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent myintent = new Intent();
myintent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(myintent, MY_DATA_CHECK_CODE);
getSmsDetails(this, 0, false);
}
public static void getSmsDetails(Context context, long ignoreThreadId, boolean unreadOnly) {
String SMS_READ_COLUMN = "read";
String WHERE_CONDITION = unreadOnly ? SMS_READ_COLUMN + " = 0" : null;
String SORT_ORDER = "date DESC";
int count = 0;
if (ignoreThreadId > 0) {
WHERE_CONDITION += " AND thread_id != " + ignoreThreadId;
}
Cursor cursor = context.getContentResolver().query(SMS_INBOX_CONTENT_URI,
new String[] { "_id", "thread_id", "address", "person", "date", "body" }, WHERE_CONDITION, null, SORT_ORDER);
if (cursor != null) {
count = cursor.getCount();
cursor.moveToFirst();
} else {
return;
}
do {
try {
long messageId = cursor.getLong(0);
long threadId = cursor.getLong(1);
String address = cursor.getString(2);
long contactId = cursor.getLong(3);
String contactId_string = String.valueOf(contactId);
long timestamp = cursor.getLong(4);
body = cursor.getString(5);
StringBuffer smsMessage = new StringBuffer();
smsMessage.append("MessageID: " + messageId + "/" + count + "\n");
smsMessage.append("ThreadID: " + threadId + "\n");
smsMessage.append("address: " + address + "\n");
smsMessage.append("contactID: " + contactId + "\n");
smsMessage.append("contactID_string: " + contactId_string + "\n");
smsMessage.append("timestamp: " + timestamp + "\n");
smsMessage.append("body: " + body + "\n");
Toast.makeText(context, smsMessage.toString(), Toast.LENGTH_LONG).show();
} finally {
cursor.close();
}
} while (cursor.moveToNext());
}
public void ButtonClickListener(){
mtts.speak(body, TextToSpeech.QUEUE_FLUSH, null);
}
protected void onActivityResult(int requestcode,int resultcode,Intent data)
{
if(requestcode == MY_DATA_CHECK_CODE)
{
if(resultcode==TextToSpeech.Engine.CHECK_VOICE_DATA_PASS)
{
// success so create the TTS engine
mtts = new TextToSpeech(this,this);
mtts.setLanguage(Locale.ENGLISH);
}
else
{
//install the Engine
Intent install = new Intent();
install.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(install);
}
}
}
public void onDestroy(Bundle savedInstanceStatBundle)
{
mtts.shutdown();
}
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
if(status==TextToSpeech.SUCCESS)
speak.setEnabled(true);
}
}
答案 0 :(得分:1)
我的建议:添加一些调试进入/退出日志记录,这样您至少可以提示您崩溃的位置。一种可能性:我注意到您在可能尚未初始化的几个地方使用mtts
成员。