以下のコードは左から右へのスワイプで機能しています。右から左へのスワイプもthe
同じように設定したいと思います。スワイプは機能していますが、スピーチアウトが位置でクラッシュしています。良いだけですが、Texttoスピーチは正しく機能していません
以下はコードです:
public Object instantiateItem(ViewGroup container, int position) {
ImageView image=new ImageView(context);
image.setImageResource(fruits[position]);
((ViewPager) container).addView(image);
if(position>=1){
image.setImageResource(fruits[position]);
speakOut(names[position-1]);
}
return image;
}
ここでは機能していません。誰かが私に提案をして、この問題から私を連れ出すことができますか
public class Learn extends Activity implements TextToSpeech.OnInitListener{
private TextToSpeech tts;
private ViewPager pic;
private Button next,back;
private TextView text;
private Context context;
private int i=0;
public Integer[] fruits={R.drawable.apple1,R.drawable.banana1,R.drawable.grapes1,R.drawable.cherry1,R.drawable.lemon1,
R.drawable.orange1,R.drawable.strwberry1,R.drawable.water1};
public String [] names={"Apple","Banana","Grapes","Cherry","Lemon","Orange","Strawberry","Water melon"};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.learn);
tts = new TextToSpeech(this, this);
pic=(ViewPager)findViewById(R.id.view_pager);
next=(Button)findViewById(R.id.button1);
back=(Button)findViewById(R.id.button2);
text=(TextView)findViewById(R.id.text);
ImagePageAdapter adapterpic=new ImagePageAdapter(this);
pic.setAdapter(adapterpic);
}
private class ImagePageAdapter extends PagerAdapter {
public ImagePageAdapter(Context learn) {
// TODO Auto-generated constructor stub
context=learn;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return names.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView image=new ImageView(context);
image.setImageResource(fruits[position]);
((ViewPager) container).addView(image);
if(position>=1){
image.setImageResource(fruits[position]);
speakOut(names[position-1]);
}
return image;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
private void speakOut(String text) {
// TODO Auto-generated method stub
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
@Override
public void onDestroy() {
// Don't forget to shutdown tts!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
pic.setEnabled(true);
//speakOut("apple");
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
}
Here is stack trace
02-27 16:39:00.994: I/TTS received:(2369): Apple
02-27 16:39:05.144: I/TTS received:(2369): Banana
02-27 16:39:06.534: I/TTS received:(2369): Grapes
02-27 16:39:24.273: I/TTS received:(2369): Banana
02-27 16:39:25.355: I/TTS received:(2369): Grapes
02-27 16:39:26.593: I/TTS received:(2369): Cherry
02-27 16:39:28.624: I/TTS received:(2369): Apple
この問題から抜け出すための提案をください