final int变量增量不起作用

时间:2014-05-29 09:46:48

标签: android

我使用以下脚本将音频文​​件与文本链接

TextView link = (TextView) findViewById(R.id.link);
link.setText(buildStringToPlaySound(context, "Play <u>song</u> or <u>poem</u>",new String[] {"http://www.domain.com/song.mp3","http://www.domain.com/poem.mp3"}));
link.setMovementMethod(LinkMovementMethod.getInstance());

public SpannableStringBuilder buildStringToPlaySound(final Context context, String str, final String[] url)
{
   CharSequence sequence = Html.fromHtml(str);
    SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence);
    UnderlineSpan[] underlines = strBuilder.getSpans(0,strBuilder.length(),UnderlineSpan.class);
    //--------------------------------------
    int i=0; //line 1
    //--------------------------------------   
    for(UnderlineSpan span : underlines) {
       int start = strBuilder.getSpanStart(span);
       int end = strBuilder.getSpanEnd(span);
       int flags = strBuilder.getSpanFlags(span);
       ClickableSpan myActivityLauncher = new ClickableSpan() {

         public void onClick(View view) {
             try {
                //--------------------------------------
                playAudio(url[i]); //line 2
                //--------------------------------------
            } catch (Exception e) {
                e.printStackTrace();
            }
         }

       };

       strBuilder.setSpan(myActivityLauncher, start, end, flags);
       //--------------------------------------
       i++; //line 3
       //--------------------------------------
    }

    return strBuilder;
}

当我将line 1用作final int i = 0;

时,问题是

第3行显示错误

The final local variable i cannot be assigned. It must be blank and not using a compound assignment

但如果我使用line 1作为int i=0;

第2行显示错误

Cannot refer to a non-final variable i inside an inner class defined in a different method

3 个答案:

答案 0 :(得分:0)

将变量i作为您的类实例变量(范围将通过类)然后它不会提醒您将变量设置为final。 :)

答案 1 :(得分:0)

变量i是buildStringToPlaySound()方法中的局部变量。 当buildStringToPlaySound()方法返回时,局部变量i将从堆栈中清除,因此在buildStringToPlaySound()返回后它们将不再存在。

因此,您可以在匿名类中初始化变量,但在您的情况下,将变量设置为全局,以便不会从堆栈中清除它。

答案 2 :(得分:0)

从@Margarita Litkevych的评论中获取想法

在for循环中添加一行final int j=i;,并在ij替换变量playAudio(url[j]);并正常工作。

TextView link = (TextView) findViewById(R.id.link);
link.setText(buildStringToPlaySound(context, "Play <u>song</u> or <u>poem</u>",new String[] {"http://www.domain.com/song.mp3","http://www.domain.com/poem.mp3"}));
link.setMovementMethod(LinkMovementMethod.getInstance());

public SpannableStringBuilder buildStringToPlaySound(final Context context, String str, final String[] url)
{
   CharSequence sequence = Html.fromHtml(str);
    SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence);
    UnderlineSpan[] underlines = strBuilder.getSpans(0,strBuilder.length(),UnderlineSpan.class);
    //--------------------------------------
    int i=0; //line 1
    //--------------------------------------   
    for(UnderlineSpan span : underlines) {

    //=================================
    final int j=i;   //added this line
    //=================================

       int start = strBuilder.getSpanStart(span);
       int end = strBuilder.getSpanEnd(span);
       int flags = strBuilder.getSpanFlags(span);
       ClickableSpan myActivityLauncher = new ClickableSpan() {

         public void onClick(View view) {
             try {
                //------------------------- //==================
                playAudio(url[j]); //line 2 //Replaced i with j
                //--------------------------//==================
            } catch (Exception e) {
                e.printStackTrace();
            }
         }

       };

       strBuilder.setSpan(myActivityLauncher, start, end, flags);
       //--------------------------------------
       i++; //line 3
       //--------------------------------------
    }

    return strBuilder;
}