我正在尝试更改图像视图中的图像,但是当我在 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));
}
}
});
}