Problem with a Handler(ImageView)
I want to show 2 images (one when connected and the second when disconnected) and i'm using a handler to handle that, however, i'm not having any of the 2 images showen, don't know why :.
private Runnable handleUpdateStatus = new Runnable()
{
Boolean mRegistered;
public void run()
{
ImageView statusImageDisplay = (ImageView)findViewById(R.id.connected);
if (mRegistered)
{
statusImageDisplay.setImageDrawable(getResources().getDrawable(R.drawable.connected));
Log.i("CONNECTED","IMAGE SET");
}
else
{
statusImageDisplay.setImageDrawable(getResources().getDrawable(R.drawable.disconnected));
Log.i("DISCONNECTED","IMAGE SET");
}
}
};
Changing image code excerpt:
public void onRegistrationDone(String localProfileUri, long expiryTime) {
updateStatus("Enregistré au serveur.");
Log.d("SUCCEED","Registration DONE");
mRegistered = true;
mRegistrationUpdateHandler.removeCallbacks(handleUpdateStatus);
mRegistrationUpdateHandler.postDelayed(handleUpdateStatus, 4000);
}
What to do when the registration failed? this code:
public void onRegistrationFailed(String localProfileUri, int errorCode,String errorMessage) {
updateStatus("Enregistrement échoué. Veuillez vérifier vos paramètres.");
Log.d("ERROR REGISTRATION",errorMessage);
mRegistered = false;
mRegistrationUpdateHandler.removeCallbacks(handleUpdateStatus);
mRegistrationUpdateHandler.postDelayed(handleUpdateStatus, 2000);
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/sipLabel"
android:textSize="20sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ImageView android:id="@+id/connected" andr开发者_开发问答oid:src="@drawable/connected"
android:layout_below="@id/sipLabel"
android:layout_width="fill_parent" android:scaleType="center"
android:layout_height="fill_parent" android:layout_weight="0.35"
android:gravity="center" android:visibility="invisible" />
</LinearLayout>
Any idea please of how solving this problem ? Thank you very much.
You (must) have your mRegistered
variable declared twice, and while you're setting the value for the global one (the one declared inside your Activity
), you examine the value of the other one, declared inside your handleUpdateStatus Runnable
.
You should clean up your code a bit:
- remove the
mRegistration
declaration from yourRunnable
implementation,
// Boolean mRegistered;
- set the image drawable via resource id,
and it will work:
private boolean mRegistered;
private Runnable handleUpdateStatus = new Runnable()
{
public void run()
{
ImageView statusImageDisplay = (ImageView) findViewById(R.id.connected);
if (mRegistered)
{
statusImageDisplay.setImageResource(R.drawable.connected);
Log.i("CONNECTED", "IMAGE SET");
}
else
{
statusImageDisplay.setImageResource(R.drawable.disconnected);
Log.i("DISCONNECTED", "IMAGE SET");
}
}
};
This is all you need to change (assuming, that your layout really contains an image with id connected
).
Update
As about mRegistrationUpdateHandler
, you should declare it as a global variable of your Activity
(either final or initialized inside the onCreate
method):
private final Handler mRegistrationUpdateHandler = new Handler();
or
private Handler mRegistrationUpdateHandler;
精彩评论