工作中有一个界面刚进入时要弹出一个popupwindow,里面有键盘,结果直接模拟点击事件不起作用,还报错,
12-26 17:01:08.636: D/CrashHandler(1795): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
12-26 17:01:08.636: D/CrashHandler(1795): at android.view.ViewRoot.setView(ViewRoot.java:505)
12-26 17:01:08.636: D/CrashHandler(1795): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
12-26 17:01:08.636: D/CrashHandler(1795): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
12-26 17:01:08.636: D/CrashHandler(1795): at android.view.Window$LocalWindowManager.addView(Window.java:424)
12-26 17:01:08.636: D/CrashHandler(1795): at android.widget.Popupwindows.invokePopup(PopupWindow.java:828)
12-26 17:01:08.636: D/CrashHandler(1795): at android.widget.PopupWindow.showAtLocation(PopupWindow.java:688)
12-26 17:01:08.636: D/CrashHandler(1795): at com.jiuqi.muchmore.clothing.view.keyboard.OnClickListenerForKeyboard.onClick(OnClickListenerForKeyboard.java:258)
12-26 17:01:08.636: D/CrashHandler(1795): at android.view.View.performClick(View.java:2408)
把它加到异步任务中,等待一会(500毫秒)就正常了,百思不得其解。
弹出方法最后考达内的一篇文章做了下试验,解决了。
http://www.cdtarena.com/gpx/201211/6042.html
布局文件:showime.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical" >
<Button
android:id="@+id/ButtonIME"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5px"
android:text="弹出软键盘输入法" />
<EditText
android:id="@+id/EditTextIME"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5px"
android:text="显示输入法键盘22" />
</LinearLayout>
java源码:ShowIME.java
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class ShowIME extends Activity{
private EditText et;
private Button bt;
private static Handler messageHandler;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.showime);
bt = (Button)findViewById(R.id.ButtonIME);
bt.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
et.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN,
et.getLeft() + 5, et.getTop() + 5, 0));
et.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP,
et.getLeft() + 5, et.getTop() + 5, 0));
}
});
et = (EditText)findViewById(R.id.EditTextIME);
PopupKeyboardAsync t = new PopupKeyboardAsync();
t.execute();
// et.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, et.getLeft()+5, et.getTop()+5, 0));
// et.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, et.getLeft()+5, et.getTop()+5, 0));
// Looper looper = Looper.myLooper();
// messageHandler = new MessageHandler(looper);
// //此处的作用是延迟1秒,然后激活点击事件
// new Thread(){
// @Override
// public void run(){
// synchronized(this){
// try{
// wait(1000); //1秒
// }
// catch(InterruptedException e){
// e.printStackTrace();
// }
// }
// Message message = Message.obtain();
// message.what = 1;
// messageHandler.sendMessage(message);
// }
// }.start();
}
class PopupKeyboardAsync extends AsyncTask {
@Override
protected Void doInBackground(Void... params){
synchronized(this){
try{
wait(500); //0.5秒
}
catch(InterruptedException e){
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(Void result){
et.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, et.getLeft()+5, et.getTop()+5, 0));
et.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, et.getLeft()+5, et.getTop()+5, 0));
}
}
class MessageHandler extends Handler{
public MessageHandler(Looper looper){
super(looper);
}
@Override
public void handleMessage(Message msg){
Bundle bundle = msg.getData();
switch(msg.what){
case 1:
//模拟点击按钮
// bt.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN,
// et.getLeft() + 5, et.getTop() + 5, 0));
// bt.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP,
// et.getLeft() + 5, et.getTop() + 5, 0));
//以下代码模拟点击文本编辑框
et.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, et.getLeft()+5, et.getTop()+5, 0));
et.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, et.getLeft()+5, et.getTop()+5, 0));
break;
default:
break;
}
}
}
}
其他参考:
http://www.baidu.com/s?wd=android 模拟 MotionEvent&pn=10&ie=utf-8&rsv_page=1
http://hhj35713.blog.163.com/blog/static/2107092442012722104950715/
http://www.docin.com/p-418125317.html
http://www.blogjava.net/cdz/archive/2011/11/25/364835.html
http://www.dewen.org/q/5875
http://my.oschina.net/banxi/blog/56421
http://lvlayster.iteye.com/blog/1333334