gpt4 book ai didi

java - 尝试使用文字转语音大声朗读短信时出现 NullPointerException

转载 作者:行者123 更新时间:2023-11-29 20:55:11 24 4
gpt4 key购买 nike

当我尝试将文本转为语音以在到达时说出短信文本时,我遇到了一个Exception,它给了我一个NullPointerException,我似乎可以弄清楚是它想让我做或错误到底在哪里。我的问题可能是什么?

错误日志

java.lang.RuntimeException: Unable to start receiver com.fegeley.handsfreetexting.TTS: java.lang.NullPointerException
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2677)
at android.app.ActivityThread.access$1800(ActivityThread.java:170)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5635)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.fegeley.handsfreetexting.TTS.speakText(TTS.java:52)
at com.fegeley.handsfreetexting.TTS.onReceive(TTS.java:74)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2669)
at android.app.ActivityThread.access$1800(ActivityThread.java:170)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5635)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)

MainActivity.java

package com.fegeley.handsfreetexting;

import android.media.AudioManager;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.view.View;
import android.widget.Toast;

import java.util.HashMap;
import java.util.Locale;


public class MainActivity extends ActionBarActivity {

TTS tts;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tts = new TTS();
tts.giveContext(this);
}

@Override
public void onPause(){
tts.onPause();
super.onPause();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

TTS.java

package com.fegeley.handsfreetexting;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.telephony.SmsMessage;

import java.util.HashMap;
import java.util.Locale;

/**
* Created by Kayne on 1/14/2015.
*/
public class TTS extends BroadcastReceiver {

private static Context context;
TextToSpeech tts;

public TTS(){
}

public static void giveContext(Context con){
context = con;
}

protected void onCreate(Bundle savedInstanceState){
tts = new TextToSpeech(context,
new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR){
tts.setLanguage(Locale.US);
}
}
});
}

public void onPause(){
if(tts !=null){
tts.stop();
tts.shutdown();
}
}

public void speakText(String toSpeak){
HashMap<String, String> hash = new HashMap<String,String>();
hash.put(TextToSpeech.Engine.KEY_PARAM_STREAM,
String.valueOf(AudioManager.STREAM_NOTIFICATION));
tts.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, hash);
}

@Override
public void onReceive(Context context, Intent intent) {
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
//if (bundle != null)
//{
//---retrieve the SMS message 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 += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
//}
//---display the new SMS message---
speakText(str);
}
}
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.fegeley.handsfreetexting" >

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<receiver android:name="com.fegeley.handsfreetexting.TTS">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>

<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>

</manifest>

最佳答案

第 52 行的“tts”对象为 NULL。

您似乎在 BroadcastReceiver 的“onCreate()”中对其进行了初始化。

protected void onCreate(Bundle savedInstanceState){
tts = new TextToSpeech(context,
new TextToSpeech.OnInitListener()

请注意 BroadcastReceiver 类没有任何 onCreate() Activity 风格的回调方法。

我猜你假设系统会调用 BroadcastReceiver 类的 onCreate()。对于 BroadcastReceiver 而言,情况并非如此。

您可以在 onReceive() 中初始化“tts”对象,如下所示:

public void onReceive(Context context1, Intent intent) {
//---get the SMS message passed in---

tts = new TextToSpeech(context,
new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR){
tts.setLanguage(Locale.US);
}
}
});

此外,更改 MainActivity.java

中的以下行

删除:tts.giveContext(this); 添加:TTS.giveContext(getApplicationContext());

此更改是为了防止ReceiverCallNotAllowedException

关于java - 尝试使用文字转语音大声朗读短信时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27968983/

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