gpt4 book ai didi

android - Println 需要一条消息

转载 作者:搜寻专家 更新时间:2023-11-01 08:13:18 26 4
gpt4 key购买 nike

我正在尝试以下应用程序,但是当我运行它时,我得到一个异常“println needs a 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);

}*/

}

list 文件

<?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 跟踪

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)

最佳答案

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

mTts.speak(msg,0, null);

所以把这个改成

public String msg=null;

为此:

public String msg="";

或者根据你想做什么,用适当的内容初始化它。

关于android - Println 需要一条消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7443205/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com