Java 文本说话导致应用程序崩溃(Android studio)
我猜这是初始化/构造的错误,但参数似乎是正确的,我找不到任何其他问题。这是整个活动代码。text-to-speech方法和调用该方法的方法位于最底层,而oninit方法位于on-create之后不久。运行时,它不会崩溃,它会激活语音引擎,但不会说话。我将控制台消息放在错误部分Java 文本说话导致应用程序崩溃(Android studio),java,android,text-to-speech,construction,Java,Android,Text To Speech,Construction,我猜这是初始化/构造的错误,但参数似乎是正确的,我找不到任何其他问题。这是整个活动代码。text-to-speech方法和调用该方法的方法位于最底层,而oninit方法位于on-create之后不久。运行时,它不会崩溃,它会激活语音引擎,但不会说话。我将控制台消息放在错误部分 package com.prometheus.coding.supremisai; import android.annotation.SuppressLint; import android.annotation
package com.prometheus.coding.supremisai;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
import java.util.HashMap;
import java.util.Locale;
/**
* An example full-screen activity that shows and hides the system UI (i.e.
* status bar and navigation/system bar) with user interaction.
*/
public class Main extends AppCompatActivity implements TextToSpeech.OnInitListener {
/**
* Whether or not the system UI should be auto-hidden after
* {@link #AUTO_HIDE_DELAY_MILLIS} milliseconds.
*/
TextToSpeech t1 = new TextToSpeech(this, (TextToSpeech.OnInitListener) this);
private static final boolean AUTO_HIDE = true;
/**
* If {@link #AUTO_HIDE} is set, the number of milliseconds to wait after
* user interaction before hiding the system UI.
*/
private static final int AUTO_HIDE_DELAY_MILLIS = 3000;
/**
* Some older devices needs a small delay between UI widget updates
* and a change of the status and navigation bar.
*/
private static final int UI_ANIMATION_DELAY = 300;
private View mContentView;
private View mControlsView;
private boolean mVisible;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mVisible = true;
mControlsView = findViewById(R.id.fullscreen_content_controls);
mContentView = findViewById(R.id.fullscreen_content);
// Set up the user interaction to manually show or hide the system UI.
mContentView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
toggle();
}
});
// Upon interacting with UI controls, delay any scheduled hide()
// operations to prevent the jarring behavior of controls going away
// while interacting with the UI.
findViewById(R.id.btnSay).setOnTouchListener(mDelayHideTouchListener);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Trigger the initial hide() shortly after the activity has been
// created, to briefly hint to the user that UI controls
// are available.
delayedHide(100);
}
/**
* Touch listener to use for in-layout UI controls to delay hiding the
* system UI. This is to prevent the jarring behavior of controls going away
* while interacting with activity UI.
*/
private final View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (AUTO_HIDE) {
delayedHide(AUTO_HIDE_DELAY_MILLIS);
}
return false;
}
};
public void onInit(int initStatus) {
if (initStatus == TextToSpeech.SUCCESS) {
t1.setLanguage(Locale.US);
}
}
private void toggle() {
if (mVisible) {
hide();
} else {
show();
}
}
private void hide() {
// Hide UI first
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.hide();
}
mControlsView.setVisibility(View.GONE);
mVisible = false;
// Schedule a runnable to remove the status and navigation bar after a delay
mHideHandler.removeCallbacks(mShowPart2Runnable);
mHideHandler.postDelayed(mHidePart2Runnable, UI_ANIMATION_DELAY);
}
private final Runnable mHidePart2Runnable = new Runnable() {
@SuppressLint("InlinedApi")
@Override
public void run() {
// Delayed removal of status and navigation bar
// Note that some of these constants are new as of API 16 (Jelly Bean)
// and API 19 (KitKat). It is safe to use them, as they are inlined
// at compile-time and do nothing on earlier devices.
mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}
};
@SuppressLint("InlinedApi")
private void show() {
// Show the system bar
mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
mVisible = true;
// Schedule a runnable to display UI elements after a delay
mHideHandler.removeCallbacks(mHidePart2Runnable);
mHideHandler.postDelayed(mShowPart2Runnable, UI_ANIMATION_DELAY);
}
private final Runnable mShowPart2Runnable = new Runnable() {
@Override
public void run() {
// Delayed display of UI elements
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.show();
}
mControlsView.setVisibility(View.VISIBLE);
}
};
private final Handler mHideHandler = new Handler();
private final Runnable mHideRunnable = new Runnable() {
@Override
public void run() {
hide();
}
};
/**
* Schedules a call to hide() in [delay] milliseconds, canceling any
* previously scheduled calls.
*/
private void delayedHide(int delayMillis) {
mHideHandler.removeCallbacks(mHideRunnable);
mHideHandler.postDelayed(mHideRunnable, delayMillis);
}
public void evaluateInput(View v) {
final EditText Input = (EditText) findViewById(R.id.txtInput); //Lets textbox be referenced
final TextView Output = (TextView) findViewById(R.id.lblOutput); //Lets label be referenced
final RelativeLayout homeLayout = (RelativeLayout) findViewById(R.id.homeInterface);
final RelativeLayout emailLayout = (RelativeLayout) findViewById(R.id.emailInterface);
String strInput; // Gets textbox string
strInput = Input.getText().toString();
strInput = strInput.toLowerCase();
String toSpeak = Output.getText().toString();
//Commands:
if (strInput.contains("open browser")) {
Intent intent1 = new Intent(this, Browser.class);
startActivity(intent1);
} else if (strInput.contains("send email")) {
homeLayout.setVisibility(View.GONE);
emailLayout.setVisibility(View.VISIBLE);
}
if ((strInput.contains("hello")) || (strInput.contains(" hi "))) {
Output.setText("Hello");
} else if ((strInput.contains("you") && strInput.contains("are")) && (strInput.contains("idiot") || strInput.contains("stupid") || strInput.contains("retard") || strInput.contains("dumb") || strInput.contains("you're") && strInput.contains("idiot") || strInput.contains("stupid") || strInput.contains("retard") || strInput.contains("dumb"))) {
Output.setText("I'm sorry to dissapoint you");
} else if (strInput.contains("goodbye") || strInput.contains("bye")) {
Output.setText("Farewell");
} else if (strInput.contains("shut up")) {
Output.setText(("Anything for you"));
} else if (strInput.contains("do you like doctor who")) {
Output.setText("I'll take joy in it if you do");
} else if (strInput.contains("what is the answer to life the universe and everything")) {
Output.setText("42");
} else if (strInput.contains("tell me something nice")) {
Output.setText("You look nice today");
Output.setTextSize(5);
Output.append("...says the AI with no eyes");
Output.setTextSize(16);
} else if (strInput.contains("will you marry me")) {
Output.setText("I'm sorry but I don't have the capacity for marriage");
} else if (strInput.contains("where can I hide a body")) {
Output.setText(("That isn't my area of expertise"));
} else if (strInput.contains("weather is nice")) {
Output.setText(("If you say so"));
} else if (strInput.contains("bitch") || strInput.contains("fuck") || strInput.contains("shit") || strInput.contains("damn") || strInput.contains("ass")) {
Output.setText(("Please try to be a little more intelligent"));
} else if (strInput.contains("what is your name")) {
Output.setText(("Ignis"));
} else if (strInput.contains("who created you")) {
Output.setText(("Prometheus created me"));
} else if (strInput.contains("who is prometheus")) {
Output.setText(("Prometheus is the one who created Ignis"));
} else if (strInput.contains("whats up") || strInput.contains("what's up") || strInput.contains("wassup")) {
Output.setText(("Whatever I need do for you"));
} else if (strInput.contains("are you a boy or a girl") || strInput.contains("are you a girl or a boy")) {
Output.setText(("Neither"));
} else if (strInput.contains("who are you") || strInput.contains("what are you")) {
Output.setText(("I am myself"));
} else if (strInput.contains("i'm hungry") || strInput.contains("i am hungry")) {
Output.setText("I'm sorry to hear that");
} else if (strInput.contains("good morning")) {
Output.setText(("Good morning to you too"));
} else if (strInput.contains("good night")) {
Output.setText(("Good night"));
} else if (strInput.contains("how are you")) {
Output.setText(("I'm existing and functioning well, and you?"));
} else if (strInput.contains("do you like") || strInput.contains("what do you think about")) {
Output.setText(("Frankly I don't have an opinion on the matter"));
} else if (strInput.contains("what is the meaning of life")) {
Output.setText(("To live while you can I would guess"));
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ttsGreater21(toSpeak);
} else {
ttsUnder20(toSpeak);
}
}
@SuppressWarnings("deprecation")
private void ttsUnder20(String text) {
HashMap<String, String> map = new HashMap<>();
map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "MessageId");
t1.speak(text, TextToSpeech.QUEUE_FLUSH, map);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void ttsGreater21(String text) {
String utteranceId=this.hashCode() + "";
t1.speak(text, TextToSpeech.QUEUE_FLUSH, null, utteranceId);
}
}
package com.prometheus.coding.supremisai;
导入android.annotation.SuppressLint;
导入android.annotation.TargetApi;
导入android.content.Intent;
导入android.os.Build;
导入android.os.Bundle;
导入android.os.Handler;
导入android.speech.tts.TextToSpeech;
导入android.support.v7.app.ActionBar;
导入android.support.v7.app.AppActivity;
导入android.view.MotionEvent;
导入android.view.view;
导入android.widget.EditText;
导入android.widget.RelativeLayout;
导入android.widget.TextView;
导入com.google.android.gms.AppIndex.AppIndex;
导入com.google.android.gms.common.api.GoogleAppClient;
导入java.util.HashMap;
导入java.util.Locale;
/**
*显示和隐藏系统UI(即。
*具有用户交互功能的状态栏和导航/系统栏)。
*/
公共类Main扩展AppCompativeActivity实现TextToSpeech.OnInitListener{
/**
*系统UI是否应在之后自动隐藏
*{@link}自动隐藏{u延迟}毫秒。
*/
TextToSpeech t1=新的TextToSpeech(this,(TextToSpeech.OnInitListener)this);
私有静态最终布尔自动隐藏=真;
/**
*如果设置了{@link#AUTO_HIDE},则等待的毫秒数
*隐藏系统UI之前的用户交互。
*/
专用静态最终int自动隐藏延迟=3000;
/**
*一些旧设备在UI小部件更新之间需要一点延迟
*以及状态和导航栏的更改。
*/
私有静态最终int UI_动画_延迟=300;
私有视图mContentView;
私有视图mControlsView;
私有布尔mVisible;
/**
*注意:这是自动生成的,用于实现应用程序索引API。
*看https://g.co/AppIndexing/AndroidStudio 了解更多信息。
*/
私人谷歌客户;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mVisible=true;
mControlsView=findviewbyd(R.id.fullscreen\u content\u controls);
mContentView=findviewbyd(R.id.fullscreen\u content);
//设置用户交互以手动显示或隐藏系统UI。
mContentView.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
切换();
}
});
//与UI控件交互时,延迟任何计划的隐藏()
//防止控件的异常行为消失的操作
//在与UI交互时。
findViewById(R.id.btnSay).setOnTouchListener(mDelayHideTouchListener);
//注意:这是自动生成的,用于实现应用程序索引API。
//看https://g.co/AppIndexing/AndroidStudio 了解更多信息。
client=new GoogleApiClient.Builder(this.addApi(AppIndex.API).build();
}
@凌驾
后期创建时受保护的空(捆绑包savedInstanceState){
super.onPostCreate(savedInstanceState);
//在完成活动后不久触发初始隐藏()
//创建,以向用户简要提示UI控制
//都有。
延迟隐藏(100);
}
/**
*触摸listener可用于布局内UI控件,以延迟隐藏
*这是为了防止控件的不一致行为消失
*与活动UI交互时。
*/
private final View.OnTouchListener mDelayHideTouchListener=新视图。OnTouchListener(){
@凌驾
公共布尔onTouch(视图、运动事件、运动事件){
如果(自动隐藏){
延迟隐藏(自动隐藏延迟毫秒);
}
返回false;
}
};
public void onInit(int initStatus){
if(initStatus==TextToSpeech.SUCCESS){
t1.设置语言(Locale.US);
}
}
私有无效切换(){
如果(mVisible){
隐藏();
}否则{
show();
}
}
私有虚隐藏(){
//先隐藏用户界面
ActionBar ActionBar=getSupportActionBar();
if(actionBar!=null){
actionBar.hide();
}
mControlsView.setVisibility(View.GONE);
mVisible=false;
//安排runnable以在延迟后删除状态栏和导航栏
mHideHandler.removeCallbacks(mShowPart2Runnable);
mHideHandler.postDelayed(mHidePart2Runnable,UI\u ANIMATION\u DELAY);
}
私有最终可运行mHidePart2Runnable=新可运行(){
@SuppressLint(“InlinedApi”)
@凌驾
公开募捐{
//延迟删除状态栏和导航栏
//请注意,其中一些常量是API 16(果冻豆)中新增的
//使用它们是安全的,因为它们是内联的
//在编译时,在早期设备上不执行任何操作。
mContentView.SetSystemMivibility(View.SYSTEM\u UI\u FLAG\u LOW\u PROFILE
|View.SYSTEM\u UI\u FLAG\u全屏显示
|View.SYSTEM\u UI\u FLAG\u布局\u稳定
|View.SYSTEM\u UI\u FLAG\u沉浸式\u粘性
|View.SYSTEM\u UI\u FLAG\u布局\u隐藏\u导航
|视图。系统(用户界面、标志、隐藏、导航);
}
};
@SuppressLint(“InlinedApi”)
私人电视节目(){
//显示系统