0

问题

单击注销按钮后,某些操作有时不会执行。在下面的代码中,有时hideTabBar()andshowAuth()似乎没有被执行,但有时它们被执行了。为什么?

源代码

MainActivity.java

public void selectLogout(View view) {
    AlertDialog.Builder mAlertDialogBuilder = new AlertDialog.Builder(this);
    mAlertDialogBuilder.setTitle("Logout").setMessage("Are you sure you wanna logout?");
    mAlertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    mAlertDialogBuilder.setPositiveButton("Logout", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            mWebview.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mWebview.loadUrl(WEB_BASE + LOGOUT_TAG);
                    btnSlide.performClick();
                    hideTabBar();
                    showAuth();
                }
            }, 500);
        }
    });
    mAlertDialogBuilder.show();
}

activity_main.xml

android:onClick="selectLogout"用于将回调函数绑定到onClick注销按钮。

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="100dp"
    android:background="@color/button_state_resource"
    android:clickable="true"
    android:onClick="selectLogout" >

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:src="@drawable/icon_logout" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="120dp"
        android:text="Logout" />

</RelativeLayout>
4

1 回答 1

0

请记住,任何与 UI 相关的更改都必须在 UI 线程上发生。因此,如果您尝试在后台线程中更改 UI,它可能会引发错误。为了解决这个问题,您可以使用一个Handler可以将作业发布到 UI 线程以按顺序运行。

例子:

private Handler handler;
@Override
public void onCreate(Bundle savedInstanceState) {
    //....some stuff
    //initialize Handler
    handler = new Handler();
}


public void selectLogout(View view) {
    AlertDialog.Builder mAlertDialogBuilder = new AlertDialog.Builder(this);
    mAlertDialogBuilder.setTitle("Logout").setMessage("Are you sure you wanna logout?");
    mAlertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    mAlertDialogBuilder.setPositiveButton("Logout", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            mWebview.postDelayed(new Runnable() {
                @Override
                public void run() {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            // update UI
                            mWebview.loadUrl(WEB_BASE + LOGOUT_TAG);
                            btnSlide.performClick();
                            hideTabBar();
                            showAuth();
                        }
                    });
                }
            }, 500);
        }
    });
    mAlertDialogBuilder.show();
}
于 2013-07-16T09:34:35.053 回答