tags:

views:

94

answers:

2

hi, i have written a simple app which would speak out to the user any incoming message. Both programmes seem to work perfectly when i lauched them as two separate pgms , but on keeping them in the same project/package only the speaker programme screen is seen and the receiver pgm doesn't seem to work . Can someone please help me out on it?

the speaker pgm is:

package com.example.TextSpeaker;
import java.util.Locale;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech; 
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

// the following programme converts the msg user to speech

public class TextSpeaker extends Activity  implements OnInitListener {
/** Called when the activity is first created. */
int MY_DATA_CHECK_CODE = 0;
public TextToSpeech mtts;
public Button button;

@Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    button = (Button)findViewById(R.id.button);
    button.setOnClickListener(new OnClickListener(){ 

 @Override
  public void onClick(View v) {
  Toast.makeText(getApplicationContext(), "The service has been started\n Every new   message will now be read out", Toast.LENGTH_LONG).show();

   }

    });
    Intent myintent = new Intent();
    myintent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
    startActivityForResult(myintent, MY_DATA_CHECK_CODE);
}
    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();
    }

    public void onPause()
    { 
     super.onPause();
     // if our app has no focus
     if(mtts!=null)
      mtts.stop();
    }
 @Override
 public void onInit(int status) {
 if(status==TextToSpeech.SUCCESS)
  button.setEnabled(true);

}


}

and the Receiver programme is:

package com.example.TextSpeaker;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.telephony.SmsMessage; // supports both gsm and cdma
import android.widget.Toast;



public class Receiver extends BroadcastReceiver{


 @Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str="";
if(bundle!=null)
{
// retrive the sms received

Object[] pdus = (Object[])bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for(int i=0;i<msgs.length;i++)
{
msgs[i]=SmsMessage.createFromPdu((byte[]) pdus[i]);
str+="Message From "+msgs[i].getOriginatingAddress()+".";
str+="Message "+msgs[i].getMessageBody().toString();
}
Toast.makeText(context,str,Toast.LENGTH_SHORT).show();
TextSpeaker tsp  = new TextSpeaker();
tsp.mtts.speak(str, TextToSpeech.QUEUE_ADD,null);

}

}

}

A: 

Now , when i send the message from another emulator, then on receiving the sms, the app gives runtime error , and i have to force close it: here is the only manifest file i am using :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.TextSpeaker"
  android:versionCode="1"
  android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".TextSpeaker"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name=".Receiver">
        <intent-filter>
        <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
        </intent-filter>
    </receiver>

</application>
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.READ_SMS"></uses-permission> <!-- allows app to read sms -->
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>

I checked the logcat , it is showing the following error lines: 06-15 07:02:23.753: WARN/dalvikvm(650): threadid=3: thread exiting with uncaught exception (group=0x4001b188)

06-15 07:02:23.773: ERROR/AndroidRuntime(650): Uncaught handler: thread main exiting due to uncaught exception

06-15 07:02:23.868: ERROR/AndroidRuntime(650): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2646)

06-15 07:02:23.868: ERROR/AndroidRuntime(650): at android.app.ActivityThread.access$3100(ActivityThread.java:119)

06-15 07:02:23.868: ERROR/AndroidRuntime(650): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1913)

06-15 07:02:23.868: ERROR/AndroidRuntime(650): at android.os.Handler.dispatchMessage(Handler.java:99)

06-15 07:02:23.868: ERROR/AndroidRuntime(650): at android.os.Looper.loop(Looper.java:123)

06-15 07:02:23.868: ERROR/AndroidRuntime(650): at android.app.ActivityThread.main(ActivityThread.java:4363)

06-15 07:02:23.868: ERROR/AndroidRuntime(650): at java.lang.reflect.Method.invokeNative(Native Method)

06-15 07:02:23.868: ERROR/AndroidRuntime(650): at java.lang.reflect.Method.invoke(Method.java:521) ... 06-15 07:02:23.868: ERROR/AndroidRuntime(650): Caused by: java.lang.NullPointerException 06-15 07:02:23.868: ERROR/AndroidRuntime(650): at com.example.TextSpeaker.Receiver.onReceive(Receiver.java:35)

06-15 07:02:23.868: ERROR/AndroidRuntime(650): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2637)

06-15 07:02:23.868: ERROR/AndroidRuntime(650): ... 10 more

pranay
i added a statement : public void onReceive(Context context, Intent intent) { if(intent.getAction().equals("SMS_RECEIVED")) { // remaining code }and now when i ran the app i get the LogCat error as:ERROR/MediaPlayerService(30): Couldn't open fd for content://settings/system/notification_soundERROR/MediaPlayer(52): Unable to to create media player.So the app now does not show runtime error
pranay
A: 

I guess that tsp.mtts is null. You're directly creating a new instance of TextSpeaker instead of the activity being created properly by the OS, so there's no reason its onCreate method would be called, so mtts will never be initialised.

Chris Smith
ok, so can you please suggest how do i correct it, actually i am quiet new to android
pranay
i tried to correct it by writing TextSpeaker tts; just after the onReceive method and then removed the instance creating statement. and simply had tsp.mtts.speak(..) , still it shows same runtime error? is there some other way of doing it?
pranay