处理程序(ImageView)的问题

发布于 2024-11-06 22:36:47 字数 2410 浏览 1 评论 0原文

我想显示 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>

请问您知道如何解决这个问题吗? 非常感谢。

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

Any idea please of how solving this problem ?
Thank you very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷途知返 2024-11-13 22:36:47

您(必须)将 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;

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 your Runnable 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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文