我猜这是启动/构造的错误,但参数似乎是正确的,我找不到任何其他问题。下面是整个活动代码。文本到语音转换方法和调用它的方法位于最底部,oninit 方法在创建后不久。运行时,它不会崩溃,它会激活语音引擎,但从不说话。我将控制台消息放在错误部分
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);
}
}
com.prometheus.coding.supremisai.Main cannot be cast to android.speech.tts.TextToSpeech$OnInitListener
Main 需要实现 onInitListener,或者需要传入 OnInitListener。