Println需要一条消息

时间:2011-09-16 10:15:46

标签: android

我正在尝试以下应用程序,但是当我运行它时,我得到一个例外“println需要一个megssage”。可以任何人帮助。以下是课程。

接收者类

package com.sms;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.PhoneLookup;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SMSReceiver extends BroadcastReceiver
{
     String name=null;
    private Context mContext;


    @Override
    public void onReceive(Context context, Intent intent)
    {   
        // TODO Auto-generated method stub

        int n;
        Bundle bundle = intent.getExtras();
        Object pdus[] = (Object[]) bundle.get("pdus");
        SmsMessage smsMessage[] = new SmsMessage[pdus.length];
        for (n = 0; n < pdus.length; n++)
        {
            smsMessage[n] = SmsMessage.createFromPdu((byte[]) pdus[n]);
        }
        // show first message
        String sms1 = smsMessage[0].getMessageBody();
        String from = smsMessage[0].getOriginatingAddress();
        //String name = getDisplayNameFromPhoneNo( from);
        Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(from));
        Cursor c = context.getContentResolver().query(lookupUri, new String[]{PhoneLookup.DISPLAY_NAME}, null, null, null);
         while(c.moveToNext()){ 
                /* If we find a match we put it in a String.*/ 
               name = c.getString(c.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));  
                 }

        //Toast toast = Toast.makeText(context, "SMS Received from: " + from, Toast.LENGTH_LONG);
        //toast.show();

        System.out.println("!!!!"+name);
        //Toast.makeText(context, "name: " + name, Toast.LENGTH_LONG).show();
         Intent sIntent=new Intent(context,SMSTalk.class);
            sIntent.putExtra("stringdata",name);
            context.startService(sIntent);

        /*SMSTalk smsTalk = new SMSTalk();
        smsTalk.readName(name);*/
        //context.startService(new Intent(context,SMSTalk.class));
    }


}

服务类

package com.sms;

import java.util.Locale;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.speech.tts.TextToSpeech.OnUtteranceCompletedListener;
import android.widget.Toast;

public class SMSTalk extends Service implements OnInitListener, OnUtteranceCompletedListener {
        public static TextToSpeech mTts;
        private String spokenText;
        public String msg=null;
        int flag=0;
        String temp;
        Context context;

        @Override
        public void onCreate() {

            // This is a good place to set spokenText
            System.out.println("SERVICE CREATED");
            //Toast.makeText(this,"...>>>"+msg,Toast.LENGTH_LONG ).show();

        }

        @Override
        public int onStartCommand(final Intent intent, int flags, int startId) 
        {
                     mTts=new TextToSpeech(this, this);                
                    String msg=intent.getStringExtra("stringdata");
                    //Toast.makeText(this, "service starting "+ msg, Toast.LENGTH_SHORT).show();
                    System.out.println("$$$***"+msg);      

                      Toast.makeText(this, "service starting message from: "+ msg, Toast.LENGTH_SHORT).show();

                       //mTts.speak(msg,0, null);

            return  START_STICKY;       

        }      

        @Override
        public void onInit(final int status) {

                // TODO Auto-generated method stub
                if (status == TextToSpeech.SUCCESS) {

                           mTts.setLanguage(Locale.UK);
                          Toast.makeText(getApplicationContext(), "SUCCESSFULLY INITIALIZED",Toast.LENGTH_LONG).show();
                          mTts.speak(msg,0, null);
                        }
                    }


        @Override
        public void onUtteranceCompleted(String uttId) {
            stopSelf();
            System.out.println("onUtteranceCompleted"+msg);
        }

        @Override
        public void onDestroy() {
            if (mTts != null) {
                mTts.stop();
                mTts.shutdown();
            }
            super.onDestroy();
        }

        @Override
        public IBinder onBind(Intent arg0) {
            return null;
        }

       /* public void readName(String temp)
        {
             msg=temp;
             System.out.println("HHHHHHHHHHHHHHHHHHH"+msg);
             mTts.speak(msg, TextToSpeech.QUEUE_ADD, null);

        }*/

       }

清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.sms"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">

         <receiver android:name=".SMSReceiver" android:enabled="true">              
          <intent-filter>                   
           <action android:name="android.provider.Telephony.SMS_RECEIVED"/>               
           </intent-filter>         
            </receiver>
            <service android:name=".SMSTalk"


            ></service>

    </application>
    <uses-sdk android:minSdkVersion="5" />    
    <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
    <uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>


</manifest> 

logcat trace

09-16 15:35:19.015: ERROR/AndroidRuntime(1148): FATAL EXCEPTION: main
09-16 15:35:19.015: ERROR/AndroidRuntime(1148): java.lang.NullPointerException: println needs a message
09-16 15:35:19.015: ERROR/AndroidRuntime(1148):     at android.util.Log.println_native(Native Method)
09-16 15:35:19.015: ERROR/AndroidRuntime(1148):     at android.util.Log.i(Log.java:158)
09-16 15:35:19.015: ERROR/AndroidRuntime(1148):     at android.speech.tts.TextToSpeech.speak(TextToSpeech.java:720)
09-16 15:35:19.015: ERROR/AndroidRuntime(1148):     at com.sms.SMSTalk.onInit(SMSTalk.java:56)
09-16 15:35:19.015: ERROR/AndroidRuntime(1148):     at android.speech.tts.TextToSpeech$1.onServiceConnected(TextToSpeech.java:451)
09-16 15:35:19.015: ERROR/AndroidRuntime(1148):     at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1064)
09-16 15:35:19.015: ERROR/AndroidRuntime(1148):     at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1081)
09-16 15:35:19.015: ERROR/AndroidRuntime(1148):     at android.os.Handler.handleCallback(Handler.java:587)
09-16 15:35:19.015: ERROR/AndroidRuntime(1148):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-16 15:35:19.015: ERROR/AndroidRuntime(1148):     at android.os.Looper.loop(Looper.java:123)
09-16 15:35:19.015: ERROR/AndroidRuntime(1148):     at android.app.ActivityThread.main(ActivityThread.java:3647)
09-16 15:35:19.015: ERROR/AndroidRuntime(1148):     at java.lang.reflect.Method.invokeNative(Native Method)
09-16 15:35:19.015: ERROR/AndroidRuntime(1148):     at java.lang.reflect.Method.invoke(Method.java:507)
09-16 15:35:19.015: ERROR/AndroidRuntime(1148):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-16 15:35:19.015: ERROR/AndroidRuntime(1148):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-16 15:35:19.015: ERROR/AndroidRuntime(1148):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:4)

显然,当它执行时,以下行msg尚未初始化:

mTts.speak(msg,0, null);

所以将此更改为

public String msg=null;

到此:

public String msg="";

或者根据您的目的,使用适当的内容对其进行初始化。