-2

In my android application here is some piece of code.

final String line;
while ((line = rd.readLine()) != null) {
 // Print the response output...
 offer.this.runOnUiThread(new Runnable() {
 public void run() {
    Toast.makeText(getBaseContext(), ""+line.toString(), Toast.LENGTH_LONG).show();
    //Toast.makeText(getBaseContext(),"data inserted",Toast.LENGTH_SHORT).show();
    }
  });
}

In the above code I declared line as "final", it shows error as

The final local variable line may already have been assigned

at the line

while ((line = rd.readLine()) != null)

So, I removed the final modifier. Now it shows error as

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

at the line

Toast.makeText(getBaseContext(), ""+line.toString(), Toast.LENGTH_LONG).show();

I've declared a local String variable inside the while loop and assigned line value to that. Code is

while ((line = rd.readLine()) != null) {
    final String e=line.toString();
    offer.this.runOnUiThread(new Runnable() {
    public void run() {
        Toast.makeText(getBaseContext(), ""+e.toString(), Toast.LENGTH_LONG).show();
        }
    });
 }

It also show error. I couldn't understand this. Can someone say how to avoid this error in my code.

Thanks in advance.

Linga
  • 10,379
  • 10
  • 52
  • 104

1 Answers1

4
String line;
while ((line = rd.readLine ()) != null) {
    final String l = line;
    offer.this.runOnUiThread (
        new Runnable () {
            public void run () {
                Toast.makeText (getBaseContext(), l, Toast.LENGTH_LONG).show ();
            }
        }
    );
}
Mikhail Vladimirov
  • 13,572
  • 1
  • 38
  • 40