我写了这个应用程序,实际上我想提供说话功能。但是在添加了这个之后,要说话的文字不起作用。我在speech.speak(text, TextToSpeech.QUEUE_FLUSH, null); 处收到空指针异常;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Random;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.text.Html;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
@SuppressLint("NewApi")
public class BestQuotes_MainActivity extends Activity implements OnClickListener,TextToSpeech.OnInitListener
{
int IMAGE_FIRST = 0;
int IMAGE_LAST = 40;
int CURRENT_IMAGE = 0;
private String m_sText = null;
private TextToSpeech speech;
List<String> quotes = new ArrayList();
Context mContext = null;
ImageButton bSpeakerButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_best_quotes_main);
mContext = getBaseContext();
quotes = (new LoadContents()).populateList();
addImages();
Button bShareButton = (Button) findViewById(R.id.shareButton);
bSpeakerButton = (ImageButton) findViewById(R.id.speaker);
bShareButton.setOnClickListener(this);
bSpeakerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
speak();
}
});
}
private void speak()
{
String text = getText();
speech.speak(text, TextToSpeech.QUEUE_FLUSH, null); //throwing null pointer exception here
}
private void addImages() {
Random rand = new Random();
int randomNum = rand.nextInt(quotes.size() - 1 - IMAGE_FIRST + 1)
+ IMAGE_FIRST;
CURRENT_IMAGE = randomNum;
Log.e("CNH", "Rand value=" + randomNum);
TextView tv = (TextView) findViewById(R.id.quote);
tv.setMovementMethod(new ScrollingMovementMethod());
tv.setTextSize(20);
tv.setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);
String text = quotes.get(randomNum);
setText(text);
text = text.replace("\n", "<br />");
tv.setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);
tv.setText(Html.fromHtml("<b>" + getText() + "</b>"));
ImageButton imgNext = (ImageButton) findViewById(R.id.imgnext);
ImageButton imgPrevious = (ImageButton) findViewById(R.id.imgprevious);
imgNext.setImageResource(R.drawable.next);
imgPrevious.setImageResource(R.drawable.previous);
findViewById(R.id.imgnext).setOnClickListener(new HandleClick());
findViewById(R.id.imgprevious).setOnClickListener(new HandleClick());
}
private class HandleClick implements OnClickListener {
public void onClick(View view) {
if (view.getId() == findViewById(R.id.imgnext).getId()) {
int id = getNextImageNumber(1);
TextView tv;
tv = (TextView) findViewById(R.id.quote);
tv.setTextSize(20);
String text = quotes.get(id);
setText(text);
text = text.replace("\n", "<br />");
tv.setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);
tv.setText(Html.fromHtml("<b>" + text + "</b>"));
} else if (view.getId() == findViewById(R.id.imgprevious).getId()) {
int id = getNextImageNumber(2);
TextView tv = (TextView) findViewById(R.id.quote);
tv.setMovementMethod(new ScrollingMovementMethod());
tv.setTextSize(20);
String text = quotes.get(id);
text = text.replace("\n", "<br />");
setText(text);
tv.setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);
tv.setText(Html.fromHtml("<b>" + text + "</b>"));
}
}
}
public int getNextImageNumber(int nextOrPrevious) {
IMAGE_LAST = quotes.size() - 1;
if (nextOrPrevious == 1) {
if (CURRENT_IMAGE == IMAGE_LAST) {
CURRENT_IMAGE = IMAGE_FIRST;
} else {
CURRENT_IMAGE = CURRENT_IMAGE + 1;
}
} else {
if (CURRENT_IMAGE == IMAGE_FIRST) {
CURRENT_IMAGE = IMAGE_LAST;
} else {
CURRENT_IMAGE = CURRENT_IMAGE - 1;
}
}
return CURRENT_IMAGE;
}
public void onBackPressed()
{
finish();
}
public void deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
try {
File[] children = dir.listFiles();
if (children.length > 0) {
for (int i = 0; i < children.length; i++) {
File[] temp = children[i].listFiles();
for (int x = 0; x < temp.length; x++) {
temp[x].delete();
}
}
}
} catch (Exception e) {
Log.e("Cache", "failed cache clean");
}
}
}
public void downloadUrl(View v) {
String url = "";
// int url = 1;
Log.e("Clicked1", "" + v.getTag());
}
@Override
public void onClick(View v)
{
Intent iShare = new Intent(android.content.Intent.ACTION_SEND);
iShare.putExtra(android.content.Intent.EXTRA_TEXT, getText());
iShare.setType("text/plain");
startActivity(Intent.createChooser(iShare, "select any from the list:"));
/*finish();*/
}
public void setText(String sText)
{
this.m_sText = sText;
}
public String getText()
{
return m_sText;
}
@Override
public void onDestroy()
{
if (speech != null) {
speech.stop();
speech.shutdown();
}
super.onDestroy();
}
@Override
public void onInit(int status)
{
if (status == TextToSpeech.SUCCESS)
{
int result = speech.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
}
else
{
bSpeakerButton.setEnabled(true);
speak();
}
}
else
{
Log.e("TTS", "Initilization Failed!");
}
}
}