MoschDev
MoschDev

Reputation: 671

Change ImageView image , Variable Error?

i am trying to change the image in a imageview, but when i set the variable in oncreate it gives me a yellow line making it so i cannot change my image.

in this app i am checking the Internet connection. basically, if the Internet connection is available i want to show a certain image; if not i want to show a image for that.

heres my code:

  @Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    ImageView imagev = (ImageView) findViewById(R.id.imageView1);

    Button btnStatus = (Button) findViewById(R.id.btn_check);

    // creating connection detector class instance
    cd = new ConnectionDetector(getApplicationContext());

    /**
     * Check Internet status button click event
     * */
    btnStatus.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            // get Internet status
            isInternetPresent = cd.isConnectingToInternet();

            // check for Internet status
            if (isInternetPresent) {
                // Internet Connection is Present
                // make HTTP requests
                showAlertDialog(AndroidDetectInternetConnectionActivity.this, "Internet Connection",
                        "You have internet connection", true);
                imagev.setImageDrawable(getResources().getDrawable(R.drawable.withinternet));

            } else {
                // Internet connection is not present
                // Ask user to connect to Internet
                showAlertDialog(AndroidDetectInternetConnectionActivity.this, "No Internet Connection",
                        "You don't have internet connection.", false);
                imagev.setImageDrawable(getResources().getDrawable(R.drawable.nointernet));
            }
        }

    });

}

Upvotes: 0

Views: 369

Answers (3)

mipreamble
mipreamble

Reputation: 1415

public class {activity_name} extends Activity { static ImageView imagev;

@Override public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


//ImageView imagev = (ImageView) findViewById(R.id.imageView1);
 //This activity name. Mainactivity if it is so.
  {activity_name}.imagev = (ImageView) findViewById(R.id.imageView1);

Button btnStatus = (Button) findViewById(R.id.btn_check);

// creating connection detector class instance
cd = new ConnectionDetector(getApplicationContext());

/**
 * Check Internet status button click event
 * */
btnStatus.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {

        // get Internet status
        isInternetPresent = cd.isConnectingToInternet();

        // check for Internet status
        if (isInternetPresent) {
            // Internet Connection is Present
            // make HTTP requests
            showAlertDialog(AndroidDetectInternetConnectionActivity.this, "Internet Connection",
                    "You have internet connection", true);
            {activity_name}.imagev.setImageDrawable(getResources().getDrawable(R.drawable.withinternet));

        } else {
            // Internet connection is not present
            // Ask user to connect to Internet
            showAlertDialog(AndroidDetectInternetConnectionActivity.this, "No Internet Connection",
                    "You don't have internet connection.", false);
            {activity_name}.imagev.setImageDrawable(getResources().getDrawable(R.drawable.nointernet));
        }
    }

});

}

Upvotes: 0

anz
anz

Reputation: 1327

Declare the image view as a class variable i.e., imageV should be declared before onCreate(as a class variable) rather than inside it. It will solve your problem..

Upvotes: 1

sdabet
sdabet

Reputation: 18670

You need to declare the ImageView as final to use it in an inner class:

final ImageView imagev = (ImageView) findViewById(R.id.imageView1);

Btw Eclipse should display the error message

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

Upvotes: 1

Related Questions