因此,如果我使用下一个或上一个按钮来更改我已加载的内容并使用 tts 来读取内容,那么我有以下代码可以正常工作。如果我使用手势来更改 progressL1 int 的值,那么整个应用程序崩溃并且 tts 永远不会工作。奇怪的行为是,如果我按下下一个(或上一个按钮)然后在 tts 工作正常并且应用程序不会崩溃后使用手势。
我认为可能发生的是,如果我只使用手势来推进内容(progressL1++),则永远不会调用 Public void onInit(),但我不知道如何解决这个问题。我是 java 和 android 开发的新手,所以已经把它当作一堵墙,不知道如何进步。
代码是:
import java.io.IOException;
import java.io.InputStream;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGestureListener;
import android.graphics.drawable.Drawable;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.support.v4.app.NavUtils;
import android.text.method.ScrollingMovementMethod;
public class Lessonsinglegroup extends Activity implements OnClickListener, OnInitListener, android.view.GestureDetector.OnGestureListener {
//define the image and text view for use later
ImageView Image;
TextView Text;
//define the texttospeak stuff
private int MY_DATA_CHECK_CODE = 0;
public int progressL1 = 0;
private TextView inputText;
private TextToSpeech tts;
private ImageButton speakButton;
private ImageButton nextButton;
private ImageButton previousButton;
private Button babout;
private Button bhome;
//deal with the swipe to advance
private GestureDetector gDetector;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lessonsinglegroup);
getActionBar().setDisplayHomeAsUpEnabled(true);
//link the image and text boxes to the xml
Image = (ImageView)findViewById(R.id.image);
Text = (TextView)findViewById(R.id.text);
loadDataFromAsset(progressL1);
gDetector = new GestureDetector(this);
//finish with the asset load
//define tts stuff
inputText = (TextView) findViewById(R.id.text);
inputText.setMovementMethod(new ScrollingMovementMethod());
speakButton = (ImageButton) findViewById(R.id.ibtalk);
nextButton = (ImageButton) findViewById(R.id.ibnext);
previousButton = (ImageButton) findViewById(R.id.ibprevious);
babout = (Button)findViewById(R.id.babout); //about button
bhome = (Button)findViewById(R.id.bhome); //about button
//new onclick listener style
speakButton.setOnClickListener(this);
nextButton.setOnClickListener(this);
previousButton.setOnClickListener(this);
babout.setOnClickListener(this);
bhome.setOnClickListener(this);
}
public void onClick(View v) {
if (v==babout) {
Intent startabout = new Intent(Lessonsinglegroup.this, AboutScreen.class); //this is about screen
startActivity(startabout);
}
if (v==bhome) {
Intent startabout = new Intent(Lessonsinglegroup.this, Main.class); //this is main screen
startActivity(startabout);
}
if (v==nextButton && progressL1==10) {
Toast toast = Toast.makeText(Lessonsinglegroup.this,
"End of Lesson", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER,0,0);
LinearLayout toastview=(LinearLayout) toast.getView();
ImageView imageCodeProject = new ImageView(getApplicationContext());
imageCodeProject.setImageResource(R.drawable.lessoncomplete); //logo name
toastview.addView(imageCodeProject, 0);
toast.show();
}
if (v==nextButton && progressL1<10) {
progressL1++;
//toast not required now its working
// Toast.makeText(Lessonsinglegroup.this, "progress +1", Toast.LENGTH_LONG).show();
Lessonsinglegroup.this.loadDataFromAsset(progressL1);
}
if (v==previousButton) {
progressL1--;
//toast not required now its working
// Toast.makeText(Lessonsinglegroup.this, "progress +1", Toast.LENGTH_LONG).show();
Lessonsinglegroup.this.loadDataFromAsset(progressL1);
}
if (v==speakButton){
String text = inputText.getText().toString();
if (text!=null && text.length()>0) {
tts.speak(text, TextToSpeech.QUEUE_ADD, null);
}
};
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
}
//gesture handling down here
public boolean onDown(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
public boolean onFling(MotionEvent start, MotionEvent finish, float xVelocity, float yVelocity) {
if (start.getRawX() > finish.getRawX() && (progressL1<10)) {
//swipe left action here
progressL1++;
Lessonsinglegroup.this.loadDataFromAsset(progressL1);
}
if (start.getRawX() > finish.getRawX() && (progressL1==10)) {
Toast toast = Toast.makeText(Lessonsinglegroup.this,
"End of Single Group Lesson", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER,0,0);
LinearLayout toastview=(LinearLayout) toast.getView();
ImageView imageCodeProject = new ImageView(getApplicationContext());
imageCodeProject.setImageResource(R.drawable.lessoncomplete); //logo name
toastview.addView(imageCodeProject, 0);
toast.show();
}
if (start.getRawX() < finish.getRawX()) {
//swipe rightaction here
progressL1--;
Lessonsinglegroup.this.loadDataFromAsset(progressL1);
}
return true;
}
public void onLongPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2, float arg3) {
// TODO Auto-generated method stub
return false;
}
public void onShowPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
public boolean onSingleTapUp(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
public boolean onTouchEvent(MotionEvent me) {
return gDetector.onTouchEvent(me);
}
//end of gesture based advance
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
//sucess with TTS create it
tts = new TextToSpeech(this, this);
}
else {
//missing TTS so install it
Intent installIntent = new Intent();
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
}
}
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
Toast.makeText(Lessonsinglegroup.this, "Text to Speech initialised", Toast.LENGTH_SHORT).show();
}
else if (status == TextToSpeech.ERROR) {
Toast.makeText(Lessonsinglegroup.this, "Error starting Text to Speech", Toast.LENGTH_LONG).show();
}
}
//actually load the text file and image file
public void loadDataFromAsset(int progressL1) {
//load the asset files themselves
try {
InputStream is = getAssets().open("lessons/singlegp/" + progressL1 + ".txt");
//check file size
int size = is.available();
//create a buffer to handle it
byte[] buffer = new byte[size];
//send the data to the buffer
is.read(buffer);
//close the stream down
is.close();
//set the text we recovered to the TextView
Text.setText(new String(buffer));
}
catch (IOException ex) {
return;
}
//image file next
try {
InputStream ims = getAssets().open("lessons/singlegp/" + progressL1 + ".jpg");
//load the image as drawable
Drawable d = Drawable.createFromStream(ims, null);
//set the drawable image to the imageview
Image.setImageDrawable(d);
}
catch (IOException ex) {
return;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_lessonsinglegroup, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
我不确定这是否是编写我想要的代码的“最佳”方式,但是当仅使用手势来更改progressL1 值时,除了TTS 之外,它都可以工作。不知道为什么,请帮忙!
谢谢。