Android的文本功能forcecloses兰特问题 [英] Android Text function forcecloses rand issue

查看:210
本文介绍了Android的文本功能forcecloses兰特问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在previous修复后,我现在有一个新的问题。 GOT建议提出新问题,所以在这里它是!
它仍然强制关闭,但现在在新的一行。

after the previous fix i now have a new issue. Got advised to ask a new question, so here it is! It still force closes, but now on a new line.

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          
}   

变量:

    // arraylists
public List<String> explode = new ArrayList<String>();
public List<String> words = new ArrayList<String>();
public List<String> sentence = new ArrayList<String>();
public List<String> indexer = new ArrayList<String>();
// hashmaps
public Map<String, Integer> common = new HashMap<String, Integer>();
public Map<String, List<String>> context = new HashMap<String, List<String>>();
public Map<String, Integer> pos = new HashMap<String, Integer>();
public Map<String, Integer> length = new HashMap<String, Integer>();
 //strings
    public String say;

和logcat的:

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)

二logcat的:

Second logcat:

 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尖叫:

What the logcats are screaming at:

首先:

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));   
                                                                       }
                                            }

我将非常高兴,如果有人可以帮助我再次击败这个谜!

I would be very happy if someone could help me beat this riddle again!

推荐答案

嗯,我不确定这线是186和188(即抛出异常OutOfBounds两个)。但看这个循环中,我有一些建议:

Hmm, I am uncertain about which lines are 186 and 188 (the two that throw the OutOfBounds exceptions). But looking at this loop, I have some suggestions:

for(int i2 = 0; i2 < length.get(trigger); i2++ ) {

从上面我们注意到 length.get(触发)总是被设置为 explode.size()和我们可以看到, explode.size()永远不会改变。我们可以对证 explode.size()代替或再使用for-each循环。在进行:

From above we notice that length.get(trigger) is always set to explode.size() and we can see that explode.size() never changes. We could check against explode.size() instead or use a for-each loop again. Carrying on:

    randint = rand.nextInt(length.get(trigger));

    if(randint < length.get(trigger)/2) {

这看起来像你想有一个50/50的机会的:将原来的字

This looks like you want a 50/50 chance of: adding the original word to sentence or

        if(ary.get(i2)!=null) {
            sentence.add(ary.get(i2));
        }
    }else{
        sentence.add(words.get(rand.nextInt(words.size())));
    }
}    

...随机字

此外,我beleive这可能会抛出异常:

Also I beleive this could throw an exception:

        if(ary.get(i2)!=null) {
            sentence.add(ary.get(i2));

由于要检查对 length.get(触发)的有效性 I2 不是 ary.size()。现在还记得是一个简单的副本爆炸

because you are checking the validity of i2 against length.get(trigger) not ary.size(). Now remember that ary is simply a copy of explode.

所以,让我们凝聚这个循环为:

So let's condense this loop to:

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())));
}

看看剩下的两个循环,如果你申请一个类似for-each循环,你将能够凝聚和加快这些孩子的。我不知道该应用程序的code的休息,但你也许可以删除一些列表和地图完全。干杯!

Take a look at the remaining two loops, if you apply a similar for-each loop you will be able to condense and expedite these as well. I don't know the rest of the app's code, but you might be able to remove a few Lists and Maps entirely. Cheers!

这篇关于Android的文本功能forcecloses兰特问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆