String doesn't show, because it is a non-final variable

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

String doesn't show, because it is a non-final variable

Postby Timmeah » Tue Mar 03, 2009 1:40 pm

I got a problem with some code. I want to popup some text, but the following code gives me a problem.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         final ImageButton button = (ImageButton) findViewById(R.id.android_button);
  3.  
  4.         button.setOnClickListener(new OnClickListener() {
  5.  
  6.             public void onClick(View v) {
  7.  
  8.                 // Perform action on clicks
  9.  
  10.                 Toast.makeText(Test.this, myString, Toast.LENGTH_SHORT).show();
  11.  
  12.             }
  13.  
  14.         });
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Eclipse gives me the following error for myString:

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

What kind of code do I have to add to fix this?



Full code from this file


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package org.panel;
  2.  
  3.  
  4.  
  5. import java.io.BufferedInputStream;
  6.  
  7. import java.io.InputStream;
  8.  
  9. import java.net.URL;
  10.  
  11. import java.net.URLConnection;
  12.  
  13. import org.apache.http.util.ByteArrayBuffer;
  14.  
  15. import org.panel.Panel.OnPanelListener;
  16.  
  17. import android.app.Activity;
  18.  
  19. import android.os.Bundle;
  20.  
  21. import android.util.Log;
  22.  
  23. import android.view.View;
  24.  
  25. import android.view.Window;
  26.  
  27. import android.view.WindowManager;
  28.  
  29. import android.view.View.OnClickListener;
  30.  
  31. import android.widget.ImageButton;
  32.  
  33. import android.widget.TextView;
  34.  
  35. import android.widget.Toast;
  36.  
  37. import easing.interpolator.BounceInterpolator;
  38.  
  39. import easing.interpolator.EasingType.Type;
  40.  
  41.  
  42.  
  43. public class Test extends Activity implements OnPanelListener {
  44.  
  45.        
  46.  
  47.         private Panel bottomPanel;
  48.  
  49.         private Panel topPanel;
  50.  
  51.         /** Called when the activity is first created. */
  52.  
  53.     @Override
  54.  
  55.    
  56.  
  57.     public void onCreate(Bundle savedInstanceState) {
  58.  
  59.         super.onCreate(savedInstanceState);
  60.  
  61.    
  62.  
  63.         final Window win = getWindow();
  64.  
  65.         final int screenHeight = win.getWindowManager().getDefaultDisplay().getHeight();
  66.  
  67.         final int screenWidth = win.getWindowManager().getDefaultDisplay().getWidth();
  68.  
  69.        
  70.  
  71.         if(screenHeight > 1 && screenWidth > 1){
  72.  
  73.                // No Statusbar
  74.  
  75.             win.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);    
  76.  
  77.  
  78.  
  79.                // No Titlebar
  80.  
  81.                requestWindowFeature(Window.FEATURE_NO_TITLE);
  82.  
  83.                requestWindowFeature(Window.FEATURE_PROGRESS);  
  84.  
  85.         }
  86.  
  87.        
  88.  
  89.        
  90.  
  91.         setContentView(R.layout.main);  
  92.  
  93.    
  94.  
  95.         /* Will be filled and displayed later. */
  96.  
  97.         String myString = null;
  98.  
  99.         try {
  100.  
  101.              /* Define the URL we want to load data from. */
  102.  
  103.              URL myURL = new URL(
  104.  
  105.                        "http://www.polarict.nl/test.txt");
  106.  
  107.              /* Open a connection to that URL. */
  108.  
  109.              URLConnection ucon = myURL.openConnection();
  110.  
  111.  
  112.  
  113.              /* Define InputStreams to read
  114.  
  115.               * from the URLConnection. */
  116.  
  117.              InputStream is = ucon.getInputStream();
  118.  
  119.              BufferedInputStream bis = new BufferedInputStream(is);
  120.  
  121.              
  122.  
  123.              /* Read bytes to the Buffer until
  124.  
  125.               * there is nothing more to read(-1). */
  126.  
  127.              ByteArrayBuffer baf = new ByteArrayBuffer(50);
  128.  
  129.              int current = 0;
  130.  
  131.              while((current = bis.read()) != -1){
  132.  
  133.                   baf.append((byte)current);
  134.  
  135.              }
  136.  
  137.  
  138.  
  139.              /* Convert the Bytes read to a String. */
  140.  
  141.              myString = new String(baf.toByteArray());
  142.  
  143.         } catch (Exception e) {
  144.  
  145.              /* On any Error we want to display it. */
  146.  
  147.              myString = e.getMessage();
  148.  
  149.  
  150.  
  151.         }
  152.  
  153.  
  154.  
  155.         final ImageButton button = (ImageButton) findViewById(R.id.android_button);
  156.  
  157.         button.setOnClickListener(new OnClickListener() {
  158.  
  159.             public void onClick(View v) {
  160.  
  161.                 // Perform action on clicks
  162.  
  163.                 Toast.makeText(Test.this, "myString", Toast.LENGTH_SHORT).show();
  164.  
  165.             }
  166.  
  167.         });
  168.  
  169.      
  170.  
  171.         Panel panel;
  172.  
  173.        
  174.  
  175.         topPanel = panel = (Panel) findViewById(R.id.topPanel);
  176.  
  177.         panel.setOnPanelListener(this);
  178.  
  179.         panel.setInterpolator(new BounceInterpolator(Type.OUT));
  180.  
  181.        
  182.  
  183.  
  184.  
  185.         bottomPanel = panel = (Panel) findViewById(R.id.bottomPanel);
  186.  
  187.         panel.setOnPanelListener(this);
  188.  
  189.         panel.setInterpolator(new BounceInterpolator(Type.OUT));
  190.  
  191.        
  192.  
  193.  
  194.  
  195.     }
  196.  
  197.  
  198.  
  199.         public void onPanelClosed(Panel panel) {
  200.  
  201.                 String panelName = getResources().getResourceEntryName(panel.getId());
  202.  
  203.                 Log.d("Test", "Panel [" + panelName + "] closed");
  204.  
  205.         }
  206.  
  207.         public void onPanelOpened(Panel panel) {
  208.  
  209.                 String panelName = getResources().getResourceEntryName(panel.getId());
  210.  
  211.                 Log.d("Test", "Panel [" + panelName + "] opened");
  212.  
  213.        
  214.  
  215.         }
  216.  
  217.        
  218.  
  219.  
  220.  
  221.     }
Parsed in 0.048 seconds, using GeSHi 1.0.8.4
Timmeah
Junior Developer
Junior Developer
 
Posts: 14
Joined: Tue Feb 10, 2009 4:30 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 8 guests