Android 报错 android.view.InflateException: Binary XML file line 7: Binary XML file line 7: Error in

Android 报错 android.view.InflateException: Binary XML file line 7: Binary XML file line 7: Error in安卓开发中使用碎片 Fragment 遇到的报错 特此记录下来

Android 报错 android.view.InflateException: Binary XML file line #7: Binary XML file line #7: Error inflating class com.coolweather.android.fragment

我在学习安卓开发中的碎片(Fragment)时遇到的报错,特此记录下来,其实问题很简单,下面是会产生报错的java代码和xml代码:

package com.example.ui_test; import android.annotation.SuppressLint; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; @SuppressLint("ValidFragment") class NewsContentFragment extends Fragment { private View view; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = inflater.inflate(R.layout.news_content_frag,container,false); return view; } public void refresh(String newsTitle,String newsContent){ View visibilityLayout = view.findViewById(R.id.visibility_layout); visibilityLayout.setVisibility(View.VISIBLE); TextView newsTitleText = (TextView) view.findViewById(R.id.news_title); TextView newsContentText = (TextView) view.findViewById(R.id.news_content); newsTitleText.setText(newsTitle); newsContentText.setText(newsContent); } } 
 
   
   
    
     
     
     
    
    
  

下面是修改后的java代码:

package com.example.ui_test; import android.annotation.SuppressLint; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; @SuppressLint("ValidFragment") public class NewsContentFragment extends Fragment { private View view; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = inflater.inflate(R.layout.news_content_frag,container,false); return view; } public void refresh(String newsTitle,String newsContent){ View visibilityLayout = view.findViewById(R.id.visibility_layout); visibilityLayout.setVisibility(View.VISIBLE); TextView newsTitleText = (TextView) view.findViewById(R.id.news_title); TextView newsContentText = (TextView) view.findViewById(R.id.news_content); newsTitleText.setText(newsTitle); newsContentText.setText(newsContent); } } 

仅仅是因为定义java类时没有写public,xml文件无需修改

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/211866.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 下午9:24
下一篇 2026年3月18日 下午9:24


相关推荐

  • 传感器尺寸与像素密度对相片分辨率的影响「建议收藏」

    传感器尺寸与像素密度对相片分辨率的影响「建议收藏」在人们日常生活摄影中,相机的传感器尺寸以及像素素往往决定了一幅图像的清晰度,当然,不同的镜头,不同的CMOS质量等等都会对相片的质量产生影响,今天就简单讨论讨论传感器尺寸和像素密度对图像分辨率的影响。当传感器尺寸一定时,像素越多,也就是像素密度越大,所能记录到的信息也就越多,当然,也不是没有上限的,当像素密度过大的时候,单个感光像素获取到的光线量无疑会变少,所以要提高感光度才能获取到和

    2022年6月7日
    69
  • java手机号正则表达式验证,面试必会「建议收藏」

    java手机号正则表达式验证,面试必会「建议收藏」什么是Arthas?Arthas是一款开源在线诊断工具,采用命令行交互模式,支持web端在线诊断,同时提供丰富的Tab自动补全功能,进一步方便进行问题的定位和诊断。这是一款开源一年多GitHubstar2万,99%的阿里研发小哥都在用的Java终极诊断利器!相对比直接下载使用,我推荐开发者可以试一下通过IDE插件CloudToolkit中使用Arthas来实现一键远程诊断功能。得益于Arthas强大且丰富的功能,让Arthas能做的事情超乎想象。下面仅仅列举几

    2025年12月12日
    6
  • JavaScript:如何将消息打印到错误控制台?

    JavaScript:如何将消息打印到错误控制台?HowcanIprint preferablyin 如何将消息打印到错误控制台 最好包括

    2026年3月26日
    3
  • Trie树

    Trie的核心思想是空间换时间。利用字符串的公共前缀来降低查询时间的开销以达到提高效率的目的。Trie的简单实现(插入、查询)

    2021年12月18日
    57
  • Python的使用方法「建议收藏」

    Python的使用方法「建议收藏」1安装turtlePython2安装命令:Python3安装命令:因为turtle库主要是在Python2中使用的,所以安装的时候可能会提示错误:Command"pythons

    2022年7月6日
    28
  • seekg()与tellg()用法详解

    seekg()与tellg()用法详解对输入流操作:seekg()与tellg()对输出流操作:seekp()与tellp()下面以输入流函数为例介绍用法:seekg()是对输入文件定位,它有两个参数:第一个参数是偏移量,第二个参数是基地址。对于第一个参数,可以是正负数值,正的表示向后偏移,负的表示向前偏移。而第二个参数可以是:ios::beg:表示输入流的开始位置ios::cur:表示输入流的当前位置ios::end:表示输入流的结束位置tellg()函数不需要带参数,它返回当前定位指针的位置,也代表着输入流的大小。假设

    2022年6月5日
    56

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号