0

I am very new to Android programming and have a little problem.

The Error is:

Variable 'Demo_Button' is accessed from within inner class. Needs to declared final.

What i tried:

changed Demo_button.setImageResource(R.drawable.pressed); to final Demo_button.setImageResource(R.drawable.pressed);

package com.iklikla.eightgame;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.ImageButton;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ImageButton Demo_button = (ImageButton)findViewById(R.id.imageButton);

    Demo_button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Demo_button.setImageResource(R.drawable.pressed);
        }
    });
}

}
Nikita Silverstruk
  • 1,097
  • 1
  • 20
  • 42
Andreas Heimann
  • 63
  • 1
  • 2
  • 10
  • not really part of the question, but in java variable names normally start with a lowercase letter and have no underscores. e.g. `demoButton` – X.X_Mass_Developer Oct 16 '13 at 21:03

1 Answers1

1

A couple options here

First, I would declare it as a member variable then it will work

public class MainActivity extends Activity {

    ImageButton Demo_button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Demo_button = (ImageButton)findViewById(R.id.imageButton);

Second, since you are changing the View being clicked you can access it that way

emo_button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {

    ImageButton btn = (ImageButton)v; // cast the View to an ImageButton
        btn.setImageResource(R.drawable.pressed);
    }
});

Not related but will give you an error at runtime with the current code, you need to inflate a layout before trying to initialize that Button (most likely with setContentView()). So using my first example it would look something like

public class MainActivity extends Activity {

    ImageButton Demo_button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.my_layout);  // where my_layout is the name of your layout
                                             // file containing the Button without the xml extension
       Demo_button = (ImageButton)findViewById(R.id.imageButton);
codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • Thank you very much.. no errors now.. but the App keeps Crashing.. Code: http://pastebin.com/ETd1qtMJ – Andreas Heimann Oct 16 '13 at 22:50
  • Yes, that's because you didn't pay attention to the last part of my answer. I said that would happen if you didn't add `setContentView()` before trying to initialize the `View` – codeMagic Oct 17 '13 at 00:17
  • "... how can i deactivate it after the touch" what do you mean? What do you want to "deactivate" and what do you mean by "deactivate"? – codeMagic Oct 17 '13 at 16:28
  • I want the ImageButton to display another image while holding the finger on it. after releasing, it should change back to normal.. – Andreas Heimann Oct 17 '13 at 22:55
  • The best way would be to create a selector file and use [StateListDrawables](http://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html). Use `android:state_pressed` for the image you want when the button is held down – codeMagic Oct 18 '13 at 12:58