0

我正在尝试更改图像视图中的图像,但是当我在 oncreate 中设置变量时,它给了我一条黄线,因此我无法更改图像。

在这个应用程序中,我正在检查 Internet 连接。基本上,如果互联网连接可用,我想显示某个图像;如果不是,我想为此展示一张图片。

这是我的代码:

  @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));
            }
        }

    });

}
4

3 回答 3

1

您需要将 ImageView 声明为 final 才能在内部类中使用它:

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

顺便说一句 Eclipse 应该显示错误消息

无法在以不同方法定义的内部类中引用非最终变量 imagev

于 2012-10-31T08:45:28.413 回答
1

将图像视图声明为类变量,即 imageV 应在 onCreate(作为类变量)之前声明,而不是在其内部。它会解决你的问题..

于 2012-10-31T08:51:36.597 回答
0

公共类 {activity_name} 扩展 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));
        }
    }

});

}

于 2012-10-31T09:11:09.647 回答