jjzjj

java - 安卓.view.InflateException : Binary XML file line #8: Error inflating class fragment

coder 2023-12-19 原文

我正在尝试将布局膨胀为 fragment 我有#8:错误膨胀类 fragment 我的 View 甚至没有创建它落在膨胀 如何正确充气布局?

我的 XML 大:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:baselineAligned="false">

        <fragment 
                  android:id="@+id/first_page"
                  android:layout_weight="1"
                  android:layout_width="0dp"
                  android:layout_height="match_parent"
                  class = "com.project.places.First_Page"   
                    />

        <fragment 
                  android:id="@+id/map"
                  android:layout_weight="2"
                  android:layout_width="0dp"
                  android:layout_height="match_parent"
                  android:name="com.project.places.Map_fragment"
                   />


    </LinearLayout>

我的容器:

    </FrameLayout>

我的 fragment 布局:

android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10sp" >

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >



        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="10dp" >

            <ImageButton
                android:id="@+id/search_By_Location"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/location" />

            <TextView
                android:id="@+id/search_By_Location_TextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:text="Search By Your Current Location"
                android:textAppearance="?android:attr/textAppearanceLarge" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="10dp" >

            <ImageButton
                android:id="@+id/search_By_City"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/city_icon" />

            <TextView
                android:id="@+id/search_By_City_TextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:text="Search By City"
                android:textAppearance="?android:attr/textAppearanceLarge" />
        </LinearLayout><LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="10dp">

            <ImageButton 
                android:id="@+id/favorits" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:layout_gravity="center_horizontal" 
                android:src="@drawable/location_star"
                 />

            <TextView android:id="@+id/favorits_TextView"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_gravity="center_horizontal"
                  android:text="Browser Favorites"
                  android:textAppearance="?android:attr/textAppearanceLarge" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="10dp" >

            <ImageButton
                android:id="@+id/history"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/location_clock" />

            <TextView
                android:id="@+id/history_TextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:text="Browser History"
                android:textAppearance="?android:attr/textAppearanceLarge" />
          </LinearLayout>

       </LinearLayout>
   </ScrollView>

</LinearLayout>

Java 主程序:

    my class java first :


    package com.project.places;

    import android.app.Activity;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageButton;
    import android.widget.Toast;

    public class First_Page extends Fragment{


        onSearchTypeListener searchType_Callback;

        public interface onSearchTypeListener{
            public void searchTypeSelected(String type);
        }


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
            return inflater.inflate(R.layout.first_page, container, false);
        }

        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {


            ImageButton by_city =(ImageButton)getActivity().findViewById(R.id.search_By_City);
            by_city.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    //Toast.makeText(getActivity(), "by_city", Toast.LENGTH_SHORT).show();
                    searchType_Callback.searchTypeSelected("city");
                }
            });

            ImageButton by_location =(ImageButton)getActivity().findViewById(R.id.search_By_Location);
            by_location.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    //Toast.makeText(getActivity(), "by_location", Toast.LENGTH_SHORT).show();
                    searchType_Callback.searchTypeSelected("location");

                }
            });

            ImageButton favorits =(ImageButton)getActivity().findViewById(R.id.favorits);
            favorits.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Toast.makeText(getActivity(), "favorits", Toast.LENGTH_SHORT).show();
                    searchType_Callback.searchTypeSelected("favorits");

                }
            });

            ImageButton history =(ImageButton)getActivity().findViewById(R.id.history);
            history.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Toast.makeText(getActivity(), "history", Toast.LENGTH_SHORT).show();
                    searchType_Callback.searchTypeSelected("history");

                }
            });
        }

        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);

            try {
                searchType_Callback = (onSearchTypeListener)activity;
            } catch (ClassCastException e) {
                throw new ClassCastException(activity.toString() + " must implement onSearchTypeListener");
            }
        }

    }

日志文件

                01start activity ComponentInfoandroid.view.InflateException: Binary XML file line #8: Error inflating class fragment
-   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
-   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
-   at android.app.ActivityThread.access$600(ActivityThread.java:156)
-   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
-   at android.os.Handler.dispatchMessage(Handler.java:99)
-   at android.os.Looper.loop(Looper.java:153)
-   at android.app.ActivityThread.main(ActivityThread.java:5299)
-   at java.lang.reflect.Method.invokeNative(Native Method)
-   at java.lang.reflect.Method.invoke(Method.java:511)
-   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
-   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
-   at dalvik.system.NativeStart.main(Native Method)
- Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
-   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
-   at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
-   at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
-   at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
-   at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
-   at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:278)
-   at android.app.Activity.setContentView(Activity.java:1881)
-   at com.project.places.MainActivity.onCreate(MainActivity.java:18)
-   at android.app.Activity.performCreate(Activity.java:5122)
-   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
-   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
-   ... 11 more
- Caused by: java.lang.NullPointerException
-   at com.project.places.First_Page.onViewCreated(First_Page.java:36)
-   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:906)
-   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
-   at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1184)
-   at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:291)
-   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
-   ... 21 more

最佳答案

Caused by: java.lang.NullPointerException - at com.project.places.First_Page.onViewCreated(First_Page.java:36

在您的 onViewCreated() 中,您应该使用 view.findViewById() 而不是 getActivity().findViewById() 访问 fragment UI View >。该 fragment 尚未附加到 Activity,因此也未在 Activity 的 View 层次结构中找到它。

关于java - 安卓.view.InflateException : Binary XML file line #8: Error inflating class fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21433510/

有关java - 安卓.view.InflateException : Binary XML file line #8: Error inflating class fragment的更多相关文章

  1. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  2. ruby-on-rails - 渲染另一个 Controller 的 View - 2

    我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>

  3. java - 等价于 Java 中的 Ruby Hash - 2

    我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/

  4. ruby-on-rails - 如何在我的 Rails 应用程序 View 中打印 ruby​​ 变量的内容? - 2

    我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby​​中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R

  5. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  6. java - 从 JRuby 调用 Java 类的问题 - 2

    我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www

  7. java - 我的模型类或其他类中应该有逻辑吗 - 2

    我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我

  8. java - 什么相当于 ruby​​ 的 rack 或 python 的 Java wsgi? - 2

    什么是ruby​​的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht

  9. ruby-on-rails - 复数 for fields_for has_many 关联未显示在 View 中 - 2

    目前,Itembelongs_toCompany和has_manyItemVariants。我正在尝试使用嵌套的fields_for通过Item表单添加ItemVariant字段,但是使用:item_variants不显示该表单。只有当我使用单数时才会显示。我检查了我的关联,它们似乎是正确的,这可能与嵌套在公司下的项目有关,还是我遗漏了其他东西?提前致谢。注意:下面的代码片段中省略了不相关的代码。编辑:不知道这是否相关,但我正在使用CanCan进行身份验证。routes.rbresources:companiesdoresources:itemsenditem.rbclassItemi

  10. Observability:从零开始创建 Java 微服务并监控它 (二) - 2

    这篇文章是继上一篇文章“Observability:从零开始创建Java微服务并监控它(一)”的续篇。在上一篇文章中,我们讲述了如何创建一个Javaweb应用,并使用Filebeat来收集应用所生成的日志。在今天的文章中,我来详述如何收集应用的指标,使用APM来监控应用并监督web服务的在线情况。源码可以在地址 https://github.com/liu-xiao-guo/java_observability 进行下载。摄入指标指标被视为可以随时更改的时间点值。当前请求的数量可以改变任何毫秒。你可能有1000个请求的峰值,然后一切都回到一个请求。这也意味着这些指标可能不准确,你还想提取最小/

随机推荐