2010-06-21 110 views
0

我收到錯誤的應用程序已經只要我勞克下面的程序在模擬器上意外停止:獲得運行時錯誤

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

你收到什麼異常? 您可能會在SDK 1.5上運行它,因爲TextToSpeech可從Android 1.6獲得。 – 2010-06-21 13:11:01

+0

logcat中沒有錯誤,我使用2.1作爲最小sdk級別7的目標 – pranay 2010-06-21 13:14:32

回答

1

我的建議:添加一些調試入境/出境記錄所以你至少應該知道你在哪裏崩潰。一種可能性:我注意到你在幾個可能未被初始化的地方使用mtts成員。