Android 安卓文本功能问题
在上次修复之后,我现在有一个新问题。有人建议你问一个新问题,就是这样! 它仍然强制关闭,但现在在一条新的线上Android 安卓文本功能问题,android,random,arraylist,forceclose,Android,Random,Arraylist,Forceclose,在上次修复之后,我现在有一个新问题。有人建议你问一个新问题,就是这样! 它仍然强制关闭,但现在在一条新的线上 public void AI(String text) { // initiate new lists to be on the safe side List<String> indexer = new ArrayList<String>(); List<String> sentence = new ArrayLis
public void AI(String text) {
// initiate new lists to be on the safe side
List<String> indexer = new ArrayList<String>();
List<String> sentence = new ArrayList<String>();
List<String> explode = new ArrayList<String>();
List<String> ary = new ArrayList<String>();
explode = Arrays.asList(text.split(" "));
// initiate randint and trigger variable
Random rand = new Random();
int randint = rand.nextInt(explode.size());
// initiate the trigger variable
String trigger = explode.get(randint);
//check if word exists in database and add if it not.
int i = 0;
for(String word : explode) {
if(common.get(word) == null) {
words.add(word);
common.put(word, 1);
context.put(word, explode);
pos.put(word, i);
length.put(word, explode.size());
if(word.equals(trigger)) {
ary = new ArrayList<String>(explode);
}
}else{
common.put(word, common.get(word)+1 );
}
i++;
}
// fill the arraylist sentence with words to be used, some in context, some random from the database.
for(int i2 = 0; i2 < length.get(trigger); i2++ ) {
randint = rand.nextInt(length.get(trigger));
if(randint < length.get(trigger)/2) {
//
if(ary.get(i2)!=null) {
sentence.add(ary.get(i2));
}
}else{
sentence.add(words.get(rand.nextInt(words.size())));
}
}
// use to the pos-hashmap to check at which index the word was detected at, if not place it at the deliberate index.
for(int i3 = 0; i3 < sentence.size(); i3++) {
if(sentence.get(i3)!=null) {
indexer.add(pos.get(sentence.get(i3)), sentence.get(i3));
}
}
// compose the final string that is to be passed to the speak function
for(int i4 = 0; i4 < indexer.size(); i4++) {
say = say + indexer.get(i4)+" ";
}
// pass the string to the speak function
mTts.speak(say, TextToSpeech.QUEUE_FLUSH, null);
// removing final string to be ready for next iteration
say = "";
// end of AI stuff
}
第二个日志:
05-26 19:24:42.926: W/dalvikvm(543): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
05-26 19:24:42.926: E/AndroidRuntime(543): Uncaught handler: thread main exiting due to uncaught exception
05-26 19:24:42.956: E/AndroidRuntime(543): java.lang.IndexOutOfBoundsException: Invalid location 0, size is 0
05-26 19:24:42.956: E/AndroidRuntime(543): at java.util.ArrayList.get(ArrayList.java:341)
05-26 19:24:42.956: E/AndroidRuntime(543): at sarah.namespace.SarahActivity.AI(SarahActivity.java:186)
05-26 21:38:44.555: W/dalvikvm(571): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
05-26 21:38:44.555: E/AndroidRuntime(571): Uncaught handler: thread main exiting due to uncaught exception
05-26 21:38:44.575: E/AndroidRuntime(571): java.lang.IndexOutOfBoundsException
05-26 21:38:44.575: E/AndroidRuntime(571): at java.util.ArrayList.add(ArrayList.java:145)
05-26 21:38:44.575: E/AndroidRuntime(571): at sarah.namespace.SarahActivity.AI(SarahActivity.java:188)
logcats在尖叫什么:
第一:
sentence.add(words.get(rand.nextInt(words.size())));
第二:
for(int i3 = 0; i3 < sentence.size(); i3++) {
if(sentence.get(i3)!=null) {
indexer.add(pos.get(sentence.get(i3)), sentence.get(i3));
}
}
for(inti3=0;i3<句子大小();i3++){
如果(语句get(i3)!=null){
索引器.add(pos.get(句子.get(i3)),句子.get(i3));
}
}
如果有人能帮我再次解开这个谜,我会非常高兴的 嗯,我不确定哪一行是186和188(这两行抛出边界外异常)。但看看这个循环,我有一些建议:
for(int i2 = 0; i2 < length.get(trigger); i2++ ) {
看起来您希望有50/50的机会:将原始单词添加到句子中
或
if(ary.get(i2)!=null) {
sentence.add(ary.get(i2));
}
}else{
sentence.add(words.get(rand.nextInt(words.size())));
}
}
。。。一个随机单词到句子
我还认为这可能引发异常:
if(ary.get(i2)!=null) {
sentence.add(ary.get(i2));
因为您正在对照length.get(trigger)
notari.size()
检查i2
的有效性。现在请记住,ary
只是explode
的一个副本
让我们把这个循环压缩成:
for(String word : explode) {
if(rand.nextInt(1)) // 50/50 chance of being false or true (0 or 1)
sentence.add(word);
else
sentence.add(words.get(rand.nextInt(words.size())));
}
看看剩下的两个循环,如果你为每个循环应用一个相似的循环,你将能够压缩和加速这些循环。我不知道该应用程序的其余代码,但您可以完全删除一些列表和地图。干杯 第186行真的是
语句.add(words.get(rand.nextInt(words.size()))代码>?因为text
不能为空,这意味着words
应该至少包含一个值,这行应该可以工作…是的,我明白你的意思,我认为问题在第二个疑点之内。通过预先缩放arraylist解决了这个问题,但现在我遇到了一个新问题,为什么这里返回indexoutofbounds?如果(ary.get(i2)!=null){session.add(ary.get(i2));}Edit:nvm,现在它又坏了。如果ary.size()是,比如说,10,而i2是10或更多,这将导致异常。看看我的答案。谢谢!成功了!现在只有i3循环返回indexoutofboundexception.ok,通过在i3之前添加此循环来修复它。对于(int i2=0;i2 if(ary.get(i2)!=null) {
sentence.add(ary.get(i2));
for(String word : explode) {
if(rand.nextInt(1)) // 50/50 chance of being false or true (0 or 1)
sentence.add(word);
else
sentence.add(words.get(rand.nextInt(words.size())));
}