簡體   English   中英

不能在不同方法中定義的內部類中引用非final變量i

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

我有“不能在一個不同方法中定義的內部類中引用非最終變量”錯誤......我哪里出錯了?...我剛開始學習android和java編程..

public class Tictac extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

        Button button[] = new Button[9];
        button[0]= (Button) findViewById(R.id.button1);
        button[1] = (Button) findViewById(R.id.button2);
        button[2] = (Button) findViewById(R.id.button3);
        button[3] = (Button) findViewById(R.id.button4);
        button[4] = (Button) findViewById(R.id.button5);
        button[5] = (Button) findViewById(R.id.button6);
        button[6] = (Button) findViewById(R.id.button7);
        button[7] = (Button) findViewById(R.id.button8);
        button[8] = (Button) findViewById(R.id.button9);


        final TextView text = (TextView) findViewById(R.id.textView1);

        final ImageView img[] = new ImageView[9];
        img[0] = (ImageView) findViewById(R.id.img1);
        img[1] = (ImageView) findViewById(R.id.img2);
        img[2] = (ImageView) findViewById(R.id.img3);
        img[3] = (ImageView) findViewById(R.id.img4);
        img[4] = (ImageView) findViewById(R.id.img5);
        img[5] = (ImageView) findViewById(R.id.img6);
        img[6] = (ImageView) findViewById(R.id.img7);
        img[7] = (ImageView) findViewById(R.id.img8);
        img[8] = (ImageView) findViewById(R.id.img9);
        final ImageView imSq[] = new ImageView[9];
        imSq[0] = (ImageView) findViewById(R.id.imSq1);
        imSq[1] = (ImageView) findViewById(R.id.imSq2);
        imSq[2] = (ImageView) findViewById(R.id.imSq3);
        imSq[3] = (ImageView) findViewById(R.id.imSq4);
        imSq[4] = (ImageView) findViewById(R.id.imSq5);
        imSq[5] = (ImageView) findViewById(R.id.imSq6);
        imSq[6] = (ImageView) findViewById(R.id.imSq7);
        imSq[7] = (ImageView) findViewById(R.id.imSq8);
        imSq[8] = (ImageView) findViewById(R.id.imSq9);


        for(int i =0;i <=8;i++){
        if(i%2==0){
             button[i].setOnClickListener(new View.OnClickListener() {
                     public void onClick(View v) {
        **HERE-->**       img[i].setVisibility(2);
                         text.setText("COOL");

                    }
                    });
        }
        else{   
             button[i].setOnClickListener(new View.OnClickListener() {
                     public void onClick(View v) {
         **HERE-->**        imSq[i].setVisibility(2);
                         text.setText("COOL");

                    }
                    });
    }



        }

}      

}

錯誤消息確切地說明了什么是錯誤的: i變量不是final,但是你試圖在匿名內部類中引用它。

你可以這樣做:

for (int i = 0; i <= 8;i++) {
  if (i % 2 == 0) {
     final int j = i;
     button[i].setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
           img[j].setVisibility(2);
           text.setText("COOL");
         }
     });
  }
}

這里我們獲取變量i副本 ,並將其分配給最終變量j ,然后我們可以在匿名內部類中使用它。 或者,如果您不關心陣列更改的可能性,您可以:

for (int i = 0; i <= 8;i++) {
  if (i % 2 == 0) {
     final ImageView imageView = img[i];
     button[i].setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
           imageView.setVisibility(2);
           text.setText("COOL");
         }
     });
  }
}

Java語言規范的8.1.3節

使用但未在內部類中聲明的任何局部變量,形式方法參數或異常處理程序參數必須聲明為final。 任何在內部類中使用但未聲明的局部變量必須在內部類的主體之前明確賦值(第16節)。

你可以創建一個方法,如下所示:

private void method(final Button btn, final ImageView img) {
    btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            img.setVisibility(2);
            text.setText("COOL");
        }
    });
}

並在你的for i循環中使用它。 這應該工作(未測試)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM