Java 另一个线程中的某些方法无法在Android中执行 问题
单击注销按钮后,某些操作有时不会执行。在下面的代码中,有时Java 另一个线程中的某些方法无法在Android中执行 问题,java,android,multithreading,Java,Android,Multithreading,单击注销按钮后,某些操作有时不会执行。在下面的代码中,有时hideTabBar()和showAuth()似乎没有执行,但有时会执行。为什么? 源代码 MainActivity.java public void selectLogout(View view) { AlertDialog.Builder mAlertDialogBuilder = new AlertDialog.Builder(this); mAlertDialogBuilder.setTitle("Log
hideTabBar()
和showAuth()
似乎没有执行,但有时会执行。为什么?
源代码
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();
}
活动\u main.xml
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();
}
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>
记住,任何与UI相关的更改都必须发生在UI线程上。因此,如果您试图在后台线程中更改UI,它可能会抛出错误。要解决这个问题,您可以使用
处理程序
,该处理程序可以将作业发布到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();
}
从未执行过?你确定吗?你等了多少?在那里放一个断点,看看它是否停止。也许你需要调用
refresh/repaint
或隐式调用什么!您的应用程序是否在注销后退出?在WebView处理程序有机会执行延迟的runnable之前,可能会终止执行。半秒钟的短暂延迟可以解释为什么这似乎是随机发生的。