- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试以下应用程序,但是当我运行它时,我得到一个异常“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/
我都见过,事实上,在观察后我并没有意识到有什么区别。但两者之间真的有什么区别 println(); // without quotation marks 和 println(""); // wi
当运行这个 java 程序时,我希望输出只是第一个 println,因为其他方法,quaffle 和 snitch 只返回整数。但是,该程序的输出还包括 quaffle 和 snitch 方法的 pr
我开始学习 jsp 并且我看到,如果我们想在 jsp 中打印一些东西,我们必须编写 out.println() 而不是 System.out.println() ,但是如果我们编写 System.ou
我刚刚写了这段代码: public class T { public String toString() { System.out.println("new line");
我一直在研究 Swift,刚刚遇到了一个问题。我有以下词典: var locations:Dictionary = ["current":CLLocationCoordinate2D(latitude
我有这个代码: System.err.print("number of terms = "); System.out.println(allTerms.size()); System.err
我一直在研究 Swift,刚刚遇到了一个问题。我有以下词典: var locations:Dictionary = ["current":CLLocationCoordinate2D(latitude
我无法解释竞争检测器中 println 和 fmt.Println 的不同输出。我希望两者都是种族,或者至少两者都不是种族。 package main var a int func f() {
我一直以为Predef.println只是 System.out.println 的快捷方式,但显然我错了,因为它似乎没有使用 System.out根本不。为什么会这样?我该如何“重定向” Syste
我有一个字符串数组: val str:Array[String] = Array("aa","bb") scala> str.foreach(println) // works aa bb scala
这个问题已经有答案了: Move console cursor to specified position (4 个回答) 已关闭 7 年前。 我正在使用简单的 println 行在 java 上工作
这是我现在正在做的一个简单测试用例的代码: private static final ByteArrayOutputStream OUTCONTENT = new ByteArrayOutputStr
public static void algorithmOne(int n){ long startTime = System.currentTimeMillis(); sea
我有以下代码: @Test public void testMultipleUpdatesSameTime() { final CyclicBarrier gate = new Cyc
我正在尝试创建一个可打印的命令提示板,以便在 CMD 中创建一个 TicTacToe 游戏。虽然,当我为我的董事会和我的单元格创建类(class)时,Java 在我的 print 和 println
我有 char c1 = 'S'; // S as a character char c2 = '\u0068'; // h in Unicode char c3 = 0
这是我的代码(golang) func main() { names := []string{"1", "2", "3"} for index, name := range names
来自 log.go (日志包的实现): 167 // Println calls l.Output to print to the logger. 168 // Arguments are handl
为什么我需要使用 System.out.println而不是 println当我使用 GroovyInterceptable ? 例如,如果我在 Groovy 文件中编码,我可以通过键入以下内容打印到
当我编写计算器应用程序时,我只是想不出最好的方法是什么: private void calculate(String command) { System.out.print("value1:
我是一名优秀的程序员,十分优秀!