http://blog.sina.com.cn/s/blog_474928c90100x871.html
 
 
public class Ex04_1Activity extends Activity {
   
EditText editText;
TextView textView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        editText = (EditText) findViewById(R.id.myEditText);
        textView = (TextView) findViewById(R.id.myTextView);
        editText.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
textView.setText(editText.getText());
Toast toast = new Toast(Ex04_1Activity.this);
toast.setView(textView);//错误1
toast.show();
return false;
}
});
    }
}
错误1的地方会报view not attached to windows manager的错误。根据错误提示,将代码改为如下就可行了:
@Override
public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
textView.setText(editText.getText());
Toast toast = new Toast(Ex04_1Activity.this);
TextView textView1 = new TextView(Ex04_1Activity.this);
textView1.setText(textView.getText());
toast.setView(textView1);
toast.show();
return false;
}
由此分析,以findViewById形式生成的View,new Toast()这种方式是拿不到的,因为初始程序还会报告一个FindViewLocked的错误。
为了进一步证明我的猜想,将代码改成如下形式:
public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
textView.setText(editText.getText());
Toast.makeText(Ex04_1Activity.this, editText.getText(), 1).show();
return false;
}
程序又一次通过了。
再次改一下代码:
public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
textView.setText(editText.getText());
Toast toast = new Toast(Ex04_1Activity.this);
    toast.setText(editText.getText().toString());
toast.show();
return false;
}
这时又报了一个:This Toast was not created with Toast.makeText()的错误。
 

由此可见,Toast.makeText()生成的Toast可以访问findViewById方式生成的View,而自己New Toast()的方式生成的Toast只能访问同样new 出来的View对象。原因大概是在.xml中生成的View对象可以被多个Activity引用,Android为了安全起见,就将其上了锁,并且提供唯一的Toast方式,Toast.makeText()来实现吐丝,这一点和单例模式颇有共同之处

view not attached to windows manager与This Toast was not created with Toast.makeText()的更多相关文章

  1. bug_ _java.lang.IllegalArgumentException: View not attached to window manager 2

    今天遇到一个很奇特的问题,当用户设置了PIN码,在锁屏界面正常解锁PIN码后,进入Launcher时显示com.android.phone 已停止运行.一开始猜想会不会是解锁PIN码的时候处理导致了P ...

  2. bug_ _java.lang.IllegalArgumentException: View not attached to window manager

    ============= 1   view not attached to window manager 转自:http://hi.baidu.com/spare_h/blog/item/7fa3e ...

  3. Android中 View not attached to window manager错误的解决办法

    前几日出现这样一个Bug是一个RuntimeException,详细信息是这样子的:java.lang.IllegalArgumentException: View not attached to w ...

  4. 关于java.lang.IllegalArgumentException: View not attached to window manager 错误的分析

    今天遇到一个很奇特的问题,当用户设置了PIN码,在锁屏界面正常解锁PIN码后,进入Launcher时显示com.android.phone 已停止运行.一开始猜想会不会是解锁PIN码的时候处理导致了P ...

  5. View not attached to window manager

    java.lang.IllegalArgumentException: View not attached to window manager 在用ProgressDialog的时候,任务结束后Dis ...

  6. View not attached to window manager crash 的解决办法

    View not attached to window manager crash 的解决办法 转自:http://stackoverflow.com/questions/22924825/view- ...

  7. java.lang.IllegalArgumentException: View not attached to window manager

    公司项目线上bug: java.lang.IllegalArgumentException: View not attached to window manager at android.view.W ...

  8. decorview that was originally added here or java.lang.IllegalArgumentException: View not attached to window manager

    使用Dialog的时候,没少出现下面这两个报错 12-11 17:47:49.776: E/WindowManager(11461): android.view.WindowLeaked: Activ ...

  9. 关于dialog引起的 java.lang.IllegalArgumentException: View=com.android.internal.policy.impl.PhoneWindow$DecorView not attached to window manager 错误的分析

    在跑Monkey测试的时候出现了一个比较特别的问题,先来看看Log: // CRASH: com.meizu.media.painter (pid 12491) // Short Msg: java. ...

随机推荐

  1. 关于ActionContext.getContext()的用法心得

    转: 为了避免与Servlet API耦合在一起,方便Action类做单元测试,Struts 2对HttpServletRequest.HttpSession和ServletContext进行了封装, ...

  2. AJAX 三级联动

    新的封装类 <?php class DBDA { public $host="localhost";//服务器地址 public $uid="root"; ...

  3. Asp.Net - 8.多线程

    8.1 概念 进程(Process):是Windows系统中的一个基本概念,它包含着一个运行程序所需要的资源.进程之间是相对独立的,一个进程无法直接访问另一个进程的数据(除非利用分布式计算方式),一个 ...

  4. ASP.NET Web API 控制请求频率

    参考地址:http://www.cnblogs.com/shanyou/p/3194802.html 安装nuget包:WebApiContrib 很多的api,例如GitHub’s API 都有流量 ...

  5. jq获取鼠标位置

    jq获取鼠标位置 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  6. android 入门-引用库项目

    http://blog.csdn.net/arui319/article/details/6831164

  7. android 入门-安装环境

    1.安装jdk 相关链接 2.安装adt 里面包含eclipse 3.下载androidsdk 4.打开eclipse 找到windows -> 属性 -> android 主目录 复制 ...

  8. ThinkPHP3.2判断手机端访问并设置默认访问模块的方法

    ThinkPHP3.2判断是否为手机端访问并跳转到另一个模块的方法 目录结构 公共模块Common,Home模块,Mobile模块 配置Application/Common/Conf/config.p ...

  9. PING命令入门详解

    转自:http://www.linkwan.com/gb/tech/htm/928.htm 1.Ping的基础知识 ping命令相信大家已经再熟悉不过了,但是能把ping的功能发挥到最大的人却并不是很 ...

  10. 初识RPC协议

    什么是rpc框架 先回答第一个问题:什么是RPC框架? 如果用一句话概括RPC就是:远程调用框架(Remote Procedure Call) 那什么是远程调用? 通常我们调用一个php中的方法,比如 ...