How to use findViewById variable in another method. Java Android

vanamp picture vanamp · Feb 10, 2012 · Viewed 14.3k times · Source

I am very new to programming and am teaching myself so sorry if my code is ugly.

I am trying to create a menu like screen that has left and right arrow buttons that change the picture displayed. I want to make it so when your on the first image the left arrow is gone and when on the last image is displayed the right arrow is gone.

I have tried to do this a lot of different ways but always end up needing a variable somewhere where it cannot be resolved. In the code posted the error is in the switch statement and I other ways I have tried to code it the unresolved variable error would be in the onClickListener.

You don't have to code it for me, just let me know what concept I am missing and I will study up on it.

Thanks

    package com.mystuff.mymenu;

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



public class PictureMenu extends Activity implements OnClickListener{

int setView = 1;    

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.picture_menu);  

    menuSet(setView);

ImageButton right = (ImageButton) findViewById(R.id.rightButton);
ImageButton left = (ImageButton) findViewById(R.id.leftButton);
right.setOnClickListener(this);
left.setOnClickListener(this);

}       
@Override
public void onClick(View v) {
if(v.getId() == R.id.rightButton){
    setView ++;
    menuSet(setView);
if(v.getId() == R.id.rightButton){
    setView ++;
        menuSet(setView);
}
    }   
    }


private void menuSet(int setView) {

    ImageView picture1 = (ImageView) findViewById(R.id.picture1);
    ImageView picture2 = (ImageView) findViewById(R.id.picture2);
    ImageView picture3 = (ImageView) findViewById(R.id.picture3);
    ImageView picture4 = (ImageView) findViewById(R.id.picture4);

    switch(setView){
    case 1: 
        left.setVisibility(View.GONE);  //ERROR left cannot be resolved
        right.setVisibility(View.VISIBLE); //ERROR right cannot be resolved
        picture1.setVisibility(View.VISIBLE);
        picture2.setVisibility(View.GONE);
        picture3.setVisibility(View.GONE);
        picture4.setVisibility(View.GONE);
        break;
    case 2:
        left.setVisibility(View.VISIBLE);  //ERROR left cannot be resolved
        right.setVisibility(View.VISIBLE);  //ERROR right cannot be resolved
        picture1.setVisibility(View.GONE);
        picture2.setVisibility(View.VISIBLE);
        picture3.setVisibility(View.GONE);
        picture4.setVisibility(View.GONE);
        break;
    case 3: 
        left.setVisibility(View.VISIBLE);  //ERROR left cannot be resolved
        right.setVisibility(View.VISIBLE);  //ERROR right cannot be resolved
        picture1.setVisibility(View.GONE);
        picture2.setVisibility(View.GONE);
        picture3.setVisibility(View.VISIBLE);
        picture4.setVisibility(View.GONE);
        break;
    case 4: 
        left.setVisibility(View.VISIBLE); //ERROR left cannot be resolved
        right.setVisibility(View.GONE); //ERROR right cannot be resolved
        picture1.setVisibility(View.GONE);
        picture2.setVisibility(View.GONE);
        picture3.setVisibility(View.GONE);
        picture4.setVisibility(View.VISIBLE);
        break;
    default:
        left.setVisibility(View.GONE);  //ERROR left cannot be resolved
        right.setVisibility(View.VISIBLE);  //ERROR right cannot be resolved
        picture1.setVisibility(View.VISIBLE);
        picture2.setVisibility(View.GONE);
        picture3.setVisibility(View.GONE);
        picture4.setVisibility(View.GONE);
        break;
}       


}



}

Answer

m0skit0 picture m0skit0 · Feb 10, 2012

Your problem lies in that right and left are local to onCreate() method, and cannot be seen from outside that method. If you want them class-scope, make them class attributes, e.g.:

public class PictureMenu extends Activity implements OnClickListener {

    int setView = 1; 
    ImageButton right, left;
    ...

    protected void onCreate(Bundle savedInstanceState) {
        ...
        right = (ImageButton) findViewById(R.id.rightButton);
        left = (ImageButton) findViewById(R.id.leftButton);
        ...
    }

Another solution would be passing them as arguments to menuSet()

IMHO, you should first learn proper programming, specially what variable scope is. Also properly formatting your code helps easily spotting scopes.