Using java Syntax Highlighting
- final ImageButton button = (ImageButton) findViewById(R.id.android_button);
- button.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- // Perform action on clicks
- Toast.makeText(Test.this, myString, Toast.LENGTH_SHORT).show();
- }
- });
Parsed in 0.031 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
Using java Syntax Highlighting
- package org.panel;
- import java.io.BufferedInputStream;
- import java.io.InputStream;
- import java.net.URL;
- import java.net.URLConnection;
- import org.apache.http.util.ByteArrayBuffer;
- import org.panel.Panel.OnPanelListener;
- import android.app.Activity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.view.Window;
- import android.view.WindowManager;
- import android.view.View.OnClickListener;
- import android.widget.ImageButton;
- import android.widget.TextView;
- import android.widget.Toast;
- import easing.interpolator.BounceInterpolator;
- import easing.interpolator.EasingType.Type;
- public class Test extends Activity implements OnPanelListener {
- private Panel bottomPanel;
- private Panel topPanel;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- final Window win = getWindow();
- final int screenHeight = win.getWindowManager().getDefaultDisplay().getHeight();
- final int screenWidth = win.getWindowManager().getDefaultDisplay().getWidth();
- if(screenHeight > 1 && screenWidth > 1){
- // No Statusbar
- win.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
- // No Titlebar
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- requestWindowFeature(Window.FEATURE_PROGRESS);
- }
- setContentView(R.layout.main);
- /* Will be filled and displayed later. */
- String myString = null;
- try {
- /* Define the URL we want to load data from. */
- URL myURL = new URL(
- "http://www.polarict.nl/test.txt");
- /* Open a connection to that URL. */
- URLConnection ucon = myURL.openConnection();
- /* Define InputStreams to read
- * from the URLConnection. */
- InputStream is = ucon.getInputStream();
- BufferedInputStream bis = new BufferedInputStream(is);
- /* Read bytes to the Buffer until
- * there is nothing more to read(-1). */
- ByteArrayBuffer baf = new ByteArrayBuffer(50);
- int current = 0;
- while((current = bis.read()) != -1){
- baf.append((byte)current);
- }
- /* Convert the Bytes read to a String. */
- myString = new String(baf.toByteArray());
- } catch (Exception e) {
- /* On any Error we want to display it. */
- myString = e.getMessage();
- }
- final ImageButton button = (ImageButton) findViewById(R.id.android_button);
- button.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- // Perform action on clicks
- Toast.makeText(Test.this, "myString", Toast.LENGTH_SHORT).show();
- }
- });
- Panel panel;
- topPanel = panel = (Panel) findViewById(R.id.topPanel);
- panel.setOnPanelListener(this);
- panel.setInterpolator(new BounceInterpolator(Type.OUT));
- bottomPanel = panel = (Panel) findViewById(R.id.bottomPanel);
- panel.setOnPanelListener(this);
- panel.setInterpolator(new BounceInterpolator(Type.OUT));
- }
- public void onPanelClosed(Panel panel) {
- String panelName = getResources().getResourceEntryName(panel.getId());
- Log.d("Test", "Panel [" + panelName + "] closed");
- }
- public void onPanelOpened(Panel panel) {
- String panelName = getResources().getResourceEntryName(panel.getId());
- Log.d("Test", "Panel [" + panelName + "] opened");
- }
- }
Parsed in 0.046 seconds, using GeSHi 1.0.8.4


