2 Replies - 1700 Views - Last Post: 13 January 2011 - 11:19 AM

#1 theheirarchy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 11-January 11

non-final variable error, android

Posted 13 January 2011 - 11:14 AM

I have researched this pretty heavily, and the simplest and most elegant solution to make a sound play on click appears to be:
public class panic_v1 extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        MediaPlayer mp = MediaPlayer.create(this, R.raw.tone_1);
        
        Button b = (Button)this.findViewById(R.id.endButton);
        b.setonclickListener(new View.onclickListener() {
			
			@Override
			public void onclick(View v) {
				// TODO Auto-generated method stub
		    	mp.start();
			}
		});

    }
   
    }


The issue is this: I keep getting an error saying:

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

I'm new to Android Development, and I'm not sure how to resolve that error....

Any help would be awesome!

Ryan

Is This A Good Question/Topic? 0
  • +

Replies To: non-final variable error, android

#2 H3R3T1C  Icon User is offline

  • Android Expert
  • member icon

Reputation: 277
  • View blog
  • Posts: 757
  • Joined: 30-March 07

Re: non-final variable error, android

Posted 13 January 2011 - 11:17 AM

make MediaPlayer mp = MediaPlayer.create(this, R.raw.tone_1); final like so:
final MediaPlayer mp = MediaPlayer.create(this, R.raw.tone_1);


Was This Post Helpful? 1
  • +
  • -

#3 theheirarchy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 11-January 11

Re: non-final variable error, android

Posted 13 January 2011 - 11:19 AM

LMAO!

I apologize for my inexperience with this. Your solution was perfect, thanks so much!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1