tags:

views:

139

answers:

2

i am getting error that the app has stopped unexpectedly as soon as i lauch the following programme on the emulator :

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);

}

}

+1  A: 

My advice: add some debug entry/exit logging so you at least have a hint on where you're crashing. One possibility: I note that you're using the mtts member in several places where it may not have been initialized.

Pontus Gagge
A: 

i modified the code as below, now i am able to hear the message but only the latest one and that too only when i press the speak button. so what should i do so that alongwith the Toast messages , i am also able to hear the sound of all the recevied messages? Also i intend to run the process as a Service so how should i modify it? thanks

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.telephony.SmsManager;
 import android.util.Log;
 import android.view.View;
 import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;


public class TextSpeaker2 extends Activity implements OnInitListener,OnClickListener{

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 message="";
Button speak;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d("TextSpeaker2", "created");

speak = (Button)findViewById(R.id.button);
speak.setOnClickListener(this);

Intent myintent = new Intent();
myintent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(myintent, MY_DATA_CHECK_CODE);

}

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[] { "address","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(0);
    //long contactId = cursor.getLong(3);
    //String contactId_string = String.valueOf(contactId);
    //long timestamp = cursor.getLong(4);

    String body = cursor.getString(1);
    Log.d("TextSpeaker2", "got body");

    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");
    message = smsMessage.toString();
    Toast.makeText(context, message, Toast.LENGTH_LONG).show();
    mtts.speak(message, TextToSpeech.QUEUE_FLUSH, null);
    mtts.playSilence(200, TextToSpeech.QUEUE_ADD, null);
  } finally {
    cursor.close();
  }
  } while (cursor.moveToNext());
 }



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);
        getSmsDetails(this, 0, false);
    }
    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);

}

 @Override
 public void onClick(View v) {
mtts.speak(message, TextToSpeech.QUEUE_ADD, null);

}

}

pranay