我想为有两个arrayList的孩子开发app,第二个数组包含四个元素,并且从这个数组中将一个元素随机选择,并且该元素将通过智能手机传递给TextToSpeech方法语音第一次它工作正常但是再次,我在onclicklistner上为textView调用相同的方法,TexttoSpeech类不能在第二次下面工作,我的代码可以任何一个正确的.......我担心我的代码中的问题在哪里.... ........
public class MainActivity extends Activity implements OnClickListener {
static ArrayList<Integer> LOneTEN_DigitsArr, LoneFOUR_DigitsArr;
static int i, LoneArr_SpVal, pick_speekDgt;
static TextView leftTop_text, rightTop_text, leftBottom_text,
rightBottom_text;
public static Context baseContext;
static TextToSpeech tts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// set ids of four textviews
leftBottom_text = (TextView) findViewById(R.id.bottomLeft_txtV);
rightBottom_text = (TextView) findViewById(R.id.bottomRight_txtV);
leftTop_text = (TextView) findViewById(R.id.topLeft_txtV);
rightTop_text = (TextView) findViewById(R.id.topRight_txtV);
baseContext = getBaseContext();
// below implementation at OnInitListener
tts = new TextToSpeech(getApplicationContext(),
new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status != TextToSpeech.ERROR) {
tts.setLanguage(Locale.UK);
speakText(String.valueOf(pick_speekDgt));
}
}
});
// initialize array list members
LOneTEN_DigitsArr = new ArrayList<Integer>();
LoneFOUR_DigitsArr = new ArrayList<Integer>();
leftBottom_text.setOnClickListener(this);
rightBottom_text.setOnClickListener(this);
leftTop_text.setOnClickListener(this);
rightTop_text.setOnClickListener(this);
// call below method to create 10 digits ArrayList and also call
// rest of the methods inside tenValues_Lone()
tenValues_Lone();
}
// inside onclick the method tenValues_Lone() and rest of methods will be call again and again
//when user click on textview
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bottomLeft_txtV:
tenValues_Lone();
break;
case R.id.bottomRight_txtV:
tenValues_Lone();
break;
case R.id.topLeft_txtV:
tenValues_Lone();
break;
case R.id.topRight_txtV:
tenValues_Lone();
break;
default:
break;
}
}
/**** create array of 10 digits for first level first it will be create ****/
public static int tenValues_Lone() {
LOneTEN_DigitsArr.clear();
for (i = 1; i <= 10; ++i) {
LOneTEN_DigitsArr.add(i);
System.out.print(String.valueOf(LOneTEN_DigitsArr));
}
// insinde tenValues_Lone() method i call below method
fourValues_Lone();
return i;
}
public static void fourValues_Lone() {
LoneFOUR_DigitsArr.clear();
for (int j = 1; j <= 4; j++) {
Collections.shuffle(LOneTEN_DigitsArr);
int pickValues = LOneTEN_DigitsArr.get(j);
if (!LoneFOUR_DigitsArr.contains(pickValues)) {
LoneFOUR_DigitsArr.add(pickValues);
} else {
j--;
}
}
//insinde fourValues_Lone() method i call below method also
Lone_GetSet_Text();
}
/******************** get text from array and set to textView ********************/
public static void Lone_GetSet_Text() {
pick_speekDgt = getRandom(LoneFOUR_DigitsArr);
leftBottom_text.setText(String.valueOf(LoneFOUR_DigitsArr.get(0)));
rightBottom_text.setText(String.valueOf(LoneFOUR_DigitsArr.get(1)));
leftTop_text.setText(String.valueOf(LoneFOUR_DigitsArr.get(2)));
rightTop_text.setText(String.valueOf(LoneFOUR_DigitsArr.get(3)));
Toast.makeText(baseContext,
"Speak" + "\b" + String.valueOf(pick_speekDgt), 300).show();
}
/**** get digits randomly from Array ****/
public static int getRandom(ArrayList<Integer> array) {
int rnd = new Random().nextInt(4);
int Val = array.get(rnd);
return Val;
}
public static String speakText(String toSpeak) {
toSpeak.toString();
tts.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
return toSpeak;
}
}
下面是我的logcat,但我不认为它显示一些错误或异常
09-10 15:35:13.809: D/STATUSBAR-NotificationService(2265): enqueueToast()
09-10 15:35:13.834: I/SurfaceFlinger(1909): id=224(7) createSurf 0x40dc5214 (1x1),1 flag=0,
Uoast
09-10 15:35:13.834: I/SurfaceFlinger(1909): SurfaceFlinger : eMagnificationFactorChanged.
magnificationFactor=1.0
09-10 15:35:13.834: I/SurfaceFlinger(1909): SurfaceFlinger : eZoomPositionChanged. zoomX=0.0
zoomY=0.0
09-10 15:35:13.834: D/KeyguardViewMediator(2265): setHidden false
09-10 15:35:13.849: D/PowerManagerService(2265): acquireWakeLock flags=0x2000000a
tag=KEEP_SCREEN_ON_FLAG uid=1000 pid=2265 myUID=1000 myPID=2265 myTID=291 1
09-10 15:35:13.869: D/KeyguardViewMediator(2265): setHidden false
09-10 15:35:14.399: D/KeyguardViewMediator(2265): setHidden false
09-10 15:35:14.399: D/KeyguardViewMediator(2265): setHidden false
09-10 15:35:14.879: W/PowerManagerService(2265): Timer 0x3->0x3|0x3
09-10 15:35:15.824: D/KeyguardViewMediator(2265): setHidden false
09-10 15:35:16.349: D/KeyguardViewMediator(2265): setHidden false
09-10 15:35:16.349: I/SurfaceFlinger(1909): id=224 Removed Uoast idx=5 MapSz=3
09-10 15:35:16.349: I/SurfaceFlinger(1909): id=224 Removed Uoast idx=-2 MapSz=3
09-10 15:35:16.354: D/KeyguardViewMediator(2265): setHidden false
09-10 15:35:16.354: D/PowerManagerService(2265): releaseWakeLockLocked flags=0x0
tag=KEEP_SCREEN_ON_FLAG myUID=1000 myPID=2265 myTID=2509
09-10 15:35:16.359: D/KeyguardViewMediator(2265): setHidden false
09-10 15:35:17.864: W/PowerManagerService(2265): Timer 0x3->0x3|0x0
09-10 15:35:23.704: W/SignalStrength(2265): getGsmLevel=4
09-10 15:35:23.704: W/SignalStrength(2265): getLevel=4 (SignalStrength: 18 -1 -1 -1 -1 -1 -1 -1
-1 -1
-1 -1 gsm|lte 4)
09-10 15:35:23.704: W/SignalStrength(2602): getGsmLevel=4
09-10 15:35:23.709: W/SignalStrength(2265): getGsmDbm=-77
09-10 15:35:23.709: W/SignalStrength(2265): getDbm=-77
09-10 15:35:23.709: W/SignalStrength(2265): getGsmAsuLevel=18
09-10 15:35:23.709: W/SignalStrength(2265): getAsuLevel=18
09-10 15:35:23.714: W/SignalStrength(2602): getLevel=4 (SignalStrength: 18 -1 -1 -1 -1 -1 -1 -1
-1 -1
-1 -1 gsm|lte 4)
09-10 15:35:23.714: W/SignalStrength(2602): getGsmLevel=4
09-10 15:35:23.714: W/SignalStrength(2602): getLevel=4 (SignalStrength: 18 -1 -1 -1 -1 -1 -1 -1
-1 -1
-1 -1 gsm|lte 4)
09-10 15:35:23.714: D/STATUSBAR-NetworkController(2602): onSignalStrengthsChanged
signalStrength=SignalStrength: 18 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 gsm|lte 4 level=4
09-10 15:35:23.719: W/AlarmManager(2265): FACTORY_ON= 0
09-10 15:35:24.849: D/BatteryService(2265): update start
09-10 15:35:24.854: D/BatteryService(2265): level:13 scale:100 status:2 health:2 present:true
voltage: 3728 temperature: 401 technology: Li-ion AC powered:false USB powered:true
icon:17303231
invalid charger:0
09-10 15:35:24.859: D/BatteryService(2265): Dormant mode is always on
09-10 15:35:24.859: D/STATUSBAR-BatteryController(2602): onReceive() - ACTION_BATTERY_CHANGED
09-10 15:35:24.864: D/STATUSBAR-BatteryController(2602): onReceive() - level:13
09-10 15:35:24.864: D/STATUSBAR-BatteryController(2602): onReceive() - plugged:2
09-10 15:35:24.864: D/STATUSBAR-BatteryController(2602): onReceive() - BATTERY_STATUS_CHARGING:
答案 0 :(得分:0)
我刚刚更改了onCreate中的以下代码...
tts = new TextToSpeech(getApplicationContext(),
new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status != TextToSpeech.ERROR) {
tts.setLanguage(Locale.UK);
speakText(String.valueOf(pick_speekDgt));
}
}
});
到
tts = new TextToSpeech(this,this);
并实现oInitListner ....如下所示
public class MainActivity extends Activity implements OnClickListener, OnInitListener{}
并使用如下所述的onInit()
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
if (status != TextToSpeech.ERROR) {
tts.setLanguage(Locale.UK);
speakText(String.valueOf(pick_speekDgt));
}
}
}