android 开启线程关闭对话框,android – 从非UI线程弹出一个Dialog

我正在开发一个面向群组的网络应用程序.问题是,当我即将加入一个组时,它首先检查该组是否安全,如果是,它会询问用户和密码.获得组安全性可能需要几秒钟,因此我为整个过程生成了一个新线程.如果该组需要安全性,我想弹出一个Dialog.我认为它可能与后台线程有关,它们可能无法弹出Dialogs ……但问题是我需要在后台线程中检查组安全性,因为它需要一些时间.

希望任何人都可以提出解决方案或任何方式只在组安全时询问用户/通过.这是后台主题:

public void run() {

secInf = mGroupId.getSecurityInformation();

if (secInf.getAdmissionLevel() == CreateGroupDialog.PRIVATE_KEY_ACCESS) {

showUserPasswordDialog();

} else {

mService.joinGroup(mGroupId);

// Notifies handler to dismiss ProgresDialog and start activity

mHandler.sendMessage(Message.obtain(mHandler,

GroupsActivity.JOIN_SUCCESSFUL));

}

showUserPasswordDialog的用途(mActivity是产生此线程的活动):

private void showUserPasswordDialog() {

AlertDialog dialog;

// add this to your code

// This example shows how to add a custom layout to an AlertDialog

LayoutInflater factory = LayoutInflater.from(mActivity);

final View textEntryView = factory.inflate(

R.layout.alert_dialog_text_entry, null);

AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);

builder.setIcon(R.drawable.alert_dialog_icon);

builder.setTitle(R.string.ask_user_password);

builder.setView(textEntryView);

builder.setPositiveButton(R.string.ok_text,

new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int whichButton) {

String userName = ((EditText) mActivity

.findViewById(R.id.username_edit_alert_dialog))

.getText().toString();

String password = ((EditText) mActivity

.findViewById(R.id.password_edit_alert_dialog))

.getText().toString();

Credentials cred = new CredentialsL1(userName, password);

mSmeppService.joinGroup(mGroupId, cred);

mHandler.sendMessage(Message.obtain(mHandler,

GroupsActivity.JOIN_SUCCESSFUL));

});

builder.setNegativeButton(R.string.cancel_text,

new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int whichButton) {

dialog.cancel();

mHandler.sendMessage(Message.obtain(mHandler,

GroupsActivity.DISMISS_PROGRESS_DIALOG));

}

});

dialog = builder.create();

/* I found this somewhere, but didn't work either */

// Window window = dialog.getWindow();

// WindowManager.LayoutParams lp = window.getAttributes();

// lp.token = textEntryView.getWindowToken();

// lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;

// window.setAttributes(lp);

// window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

dialog.show();

}

我得到了这个例外:

12-13 19:18:31.823: ERROR/AndroidRuntime(1702): FATAL EXCEPTION: Thread-38

12-13 19:18:31.823: ERROR/AndroidRuntime(1702): android.view.InflateException: Binary XML file line #17: Error inflating class android.widget.EditText

12-13 19:18:31.823: ERROR/AndroidRuntime(1702): at android.view.LayoutInflater.createView(LayoutInflater.java:513)

12-13 19:18:31.823: ERROR/AndroidRuntime(1702): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)

12-13 19:18:31.823: ERROR/AndroidRuntime(1702): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)

12-13 19:18:31.823: ERROR/AndroidRuntime(1702): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)

12-13 19:18:31.823: ERROR/AndroidRuntime(1702): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)

12-13 19:18:31.823: ERROR/AndroidRuntime(1702): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)

12-13 19:18:31.823: ERROR/AndroidRuntime(1702): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)

12-13 19:18:31.823: ERROR/AndroidRuntime(1702): at org.pfc.threads.GroupJoinerThread.showUserPasswordDialog(GroupJoinerThread.java:76)

12-13 19:18:31.823: ERROR/AndroidRuntime(1702): at org.pfc.threads.GroupJoinerThread.run(GroupJoinerThread.java:52)

12-13 19:18:31.823: ERROR/AndroidRuntime(1702): Caused by: java.lang.reflect.InvocationTargetException

12-13 19:18:31.823: ERROR/AndroidRuntime(1702): at android.widget.EditText.(EditText.java:51)

12-13 19:18:31.823: ERROR/AndroidRuntime(1702): at java.lang.reflect.Constructor.constructNative(Native Method)

12-13 19:18:31.823: ERROR/AndroidRuntime(1702): at java.lang.reflect.Constructor.newInstance(Constructor.java:446)

12-13 19:18:31.823: ERROR/AndroidRuntime(1702): at android.view.LayoutInflater.createView(LayoutInflater.java:500)

12-13 19:18:31.823: ERROR/AndroidRuntime(1702): ... 8 more

12-13 19:18:31.823: ERROR/AndroidRuntime(1702): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

12-13 19:18:31.823: ERROR/AndroidRuntime(1702): at android.os.Handler.(Handler.java:121)

12-13 19:18:31.823: ERROR/AndroidRuntime(1702): at android.text.method.MetaKeyKeyListener$MetaKeyDropbackHandler.(MetaKeyKeyListener.java:605)

12-13 19:18:31.823: ERROR/AndroidRuntime(1702): at android.text.method.MetaKeyKeyListener.(MetaKeyKeyListener.java:96)

12-13 19:18:31.823: ERROR/AndroidRuntime(1702): at android.text.method.BaseKeyListener.(BaseKeyListener.java:25)

12-13 19:18:31.823: ERROR/AndroidRuntime(1702): at android.text.method.TextKeyListener.(TextKeyListener.java:66)

12-13 19:18:31.823: ERROR/AndroidRuntime(1702): at android.text.method.TextKeyListener.getInstance(TextKeyListener.java:83)

12-13 19:18:31.823: ERROR/AndroidRuntime(1702): at android.widget.TextView.(TextView.java:806)

12-13 19:18:31.823: ERROR/AndroidRuntime(1702): at android.widget.EditText.(EditText.java:55)

12-13 19:18:31.823: ERROR/AndroidRuntime(1702): ... 12 more

XML布局文件是:

android:id="@+id/username_edit_alert_dialog"

android:enabled="false"

android:layout_height="wrap_content"

android:layout_width="fill_parent"

android:layout_marginLeft="20dip"

android:layout_marginRight="20dip"

android:scrollHorizontally="true"

android:autoText="false"

android:capitalize="none"

android:gravity="fill_horizontal"

android:textAppearance="?android:attr/textAppearanceMedium" />

android:id="@+id/password_view"

android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:layout_marginLeft="20dip"

android:layout_marginRight="20dip"

android:text="@string/password_view_text"

android:gravity="left"

android:textAppearance="?android:attr/textAppearanceMedium" />

android:id="@+id/password_edit_alert_dialog"

android:enabled="false"

android:layout_height="wrap_content"

android:layout_width="fill_parent"

android:layout_marginLeft="20dip"

android:layout_marginRight="20dip"

android:scrollHorizontally="true"

android:autoText="false"

android:capitalize="none"

android:gravity="fill_horizontal"

android:password="true"

android:textAppearance="?android:attr/textAppearanceMedium" />

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值