したがって、次のコードを使用すると、次または前のボタンを使用してロードしたコンテンツを変更し、ttsを使用してコンテンツを読み上げることができます。ジェスチャーを使用してprogressL1intの値を変更すると、アプリ全体がクラッシュし、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以外のすべてで機能します。理由がわからない、助けてください!
ありがとう。