处理程序问题(ImageView)

时间:2011-05-12 12:00:49

标签: android handler

我想要显示2个图像(一个连接时和第二个断开连接时)我正在使用处理程序来处理它,但是,我没有任何2个图像显示,不知道为什么:

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

    };  

更改图像代码摘录:

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

注册失败后该怎么办?这段代码:

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" android: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>

请问如何解决这个问题? 非常感谢你。

1 个答案:

答案 0 :(得分:2)

您(必须)将您的mRegistered变量声明两次,并且当您设置全局值(在Activity内声明的值)时,您会检查另一个的值一,在handleUpdateStatus Runnable内宣布。

您应该稍微清理一下代码:

  • 删除mRegistration声明 来自Runnable实施,
    // Boolean mRegistered;
  • 通过资源设置可绘制的图像 ID,

它会起作用:

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

这就是您需要更改的内容(假设您的布局确实包含ID为connected的图片)。

<强>更新
关于mRegistrationUpdateHandler,您应该将其声明为Activity的全局变量(在onCreate方法中最终或初始化):

private final Handler mRegistrationUpdateHandler = new Handler();

private Handler mRegistrationUpdateHandler;
相关问题