zoukankan      html  css  js  c++  java
  • 关于在findViewById()方法时遇到的一些问题

    最近需要做一个关于色卡的App,需要用到LayoutInflater来实现标签动态切换View界面,但是发现在使用过程中App突然会闪退,
    闪退目前已解决。

    1. 先看下关于findViewById()的详细介绍:findViewById()是找具体某一个xml下具体 widget控件(如:Button,TextView等)
    2. 我的闪退原因:我将findViewById()用在了还没有动态加载我要查找组件的xml,导致闪退
    3. 解决方法:用在之后。。。

    搜集了一些大佬的代码注释等,比我说的更加清楚。

    大佬的代码原地址:https://www.cnblogs.com/niray/p/3815059.html

    Inflater与findViewById()区别

    /**
                 * Inflater英文意思是膨胀,在Android中应该是扩展的意思吧。 LayoutInflater的作用类似于
                 * findViewById(),不同点是LayoutInflater是用来找layout文件夹下的xml布局文件,并且实例化! 而
                 * findViewById()是找具体某一个xml下的具体 widget控件(如:Button,TextView等)。
                 */
    
                // LayoutInflater的作用是,把一个View的对象与XML布局文件关联并实例化
                LayoutInflater inflater = (LayoutInflater) listviewActivity.this
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
                // LayoutInflater的作用是,把一个View的对象与XML布局文件关联并实例化
                View itemView = inflater.inflate(R.layout.listview_item, null);
    
                // View的对象实例化之后,可以通过findViewById()查找布局文件中的指定Id的组件
                TextView title = (TextView) itemView.findViewById(R.id.txttitle);
                TextView text = (TextView) itemView.findViewById(R.id.txtContent);
    
    /**
                 * Inflater英文意思是膨胀,在Android中应该是扩展的意思吧。 LayoutInflater的作用类似于
                 * findViewById(),不同点是LayoutInflater是用来找layout文件夹下的xml布局文件,并且实例化! 而
                 * findViewById()是找具体某一个xml下的具体 widget控件(如:Button,TextView等)。
                 */
    
                // LayoutInflater的作用是,把一个View的对象与XML布局文件关联并实例化
                LayoutInflater inflater = (LayoutInflater) listviewActivity.this
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
                // LayoutInflater的作用是,把一个View的对象与XML布局文件关联并实例化
                View itemView = inflater.inflate(R.layout.listview_item, null);
    
                // View的对象实例化之后,可以通过findViewById()查找布局文件中的指定Id的组件
                TextView title = (TextView) itemView.findViewById(R.id.txttitle);
                TextView text = (TextView) itemView.findViewById(R.id.txtContent);
    
  • 相关阅读:
    学习篇之函数就是对象
    @Controller和@RestController的区别
    maven 阿里云 国内镜像 中央仓库
    IntelliJ IDEA使用教程
    解决“无法从套接字读取更多数据”
    at java.util.Arrays.copyOfRange(Arrays.java:3209)导致的java.lang.OutOfMemoryError: Java heap space 错误的解决办法
    maven中引入ojdbc包
    Oralce增加表空间
    Web服务框架发展与REST服务开发
    Oralce解锁表
  • 原文地址:https://www.cnblogs.com/tfxz/p/12621710.html
Copyright © 2011-2022 走看看