【问题标题】:android.view.InflateException: Binary XML file line #7: Error inflating class fragmentandroid.view.InflateException: Binary XML file line #7: Error inflating class fragment
【发布时间】:2013-03-30 19:46:40
【问题描述】:

我知道类似的问题已经得到解答,但我似乎已经完成了教程建议并阅读了其他帖子的内容,但我仍然收到 Inflator 错误。不知道我做错了什么。请检查代码。非常感谢。

编辑:我在一个活动中有两个片段。我在一个片段中有一个按钮,在另一个片段中有一个文本视图。我试图了解片段是如何工作的,所以我在阅读文档后自己编写了这段代码。当按钮在一个片段中被触发时,它会向活动中的另一个片段发送一个值。我无法让片段膨胀

MainActivity.java

        import com.transport.mbtalocpro.PredictedTimeFragment.ButtonClickListener;
        import android.os.Bundle;
        import android.support.v4.app.FragmentActivity;


        public class MainActivity extends FragmentActivity implements ButtonClickListener {

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
            }


            @Override
            public void sendCounter(int count) {
                TestFrag testF = (TestFrag) getSupportFragmentManager().findFragmentById(R.id.bottomHalf);
                if(testF != null) testF.setCounter(String.valueOf(count));

            }

        }

PredictedTimeFragment.java

            import android.app.Activity;
            import android.os.Bundle;
            import android.support.v4.app.Fragment;
            import android.view.InflateException;
            import android.view.LayoutInflater;
            import android.view.View;
            import android.view.View.OnClickListener;
            import android.view.ViewGroup;
            import android.widget.Button;

            public class PredictedTimeFragment extends Fragment {


            ButtonClickListener bListener;

            public interface ButtonClickListener {
                public void sendCounter(int count);
            }

            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                super.onCreateView(inflater, container, savedInstanceState);
                try {
                    View view = inflater.inflate(R.layout.prediction_time, container, false);
                    Button button = (Button) view.findViewById(R.id.test);
                    button.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            test(v);

                        }
                    });
                } catch(InflateException e) {
                    e.printStackTrace();
                    System.out.println("Predicted Time Fragment");
                }
                return super.onCreateView(inflater, container, savedInstanceState);
            }

            public void onAttach(Activity activity) {
                  super.onAttach(activity);
                  try {
                    bListener = (ButtonClickListener) activity;
                  } catch (ClassCastException e) {
                     System.out.println("must implemenet Listener");
                  }
                }


            public void test(View view) {
                bListener.sendCounter(23);
            }

        }

TestFrag.java

        import android.os.Bundle;
        import android.support.v4.app.Fragment;
        import android.view.InflateException;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.ViewGroup;
        import android.widget.TextView;

        public class TestFrag extends Fragment {

            @Override
            public View onCreateView(LayoutInflater inflator, ViewGroup container, Bundle savedInstanceState) {
                super.onCreateView(inflator, container, savedInstanceState);
                try {
                    View view = inflator.inflate(R.layout.test_frag, container, false);
                } catch(InflateException e) {
                    e.printStackTrace();
                    System.out.println("Test Fragment");
                }
                return super.onCreateView(inflator, container, savedInstanceState);
            }

            public void setCounter(String textS) {
                TextView text = (TextView) getView().findViewById(R.id.test1);
                text.setText((String) textS);
            }
        }

activity_main.xml

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

            <fragment 
                android:id="@+id/topHalf"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="0.5"
                android:name="com.transport.mbtalocpro.PredictedTimeFragment"/>

            <fragment
                android:id="@+id/bottomHalf"        
                android:layout_width="fill_parent"        
                android:layout_height="0dp"
                android:layout_weight="0.5"
                android:name ="com.transport.mbtalocpro.TestFrag"/>

        </LinearLayout>

predicted_time.xml

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

            <Button 
                android:id="@+id/test"
                android:text="Test"
                android:layout_width="fill_parent"
                android:layout_height="30dp"
                />

        </LinearLayout>

test_frag.xml

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

            <TextView 
                android:id="@+id/test1"
                android:layout_width="fill_parent"
                android:layout_height="40dp"/>

        </LinearLayout>

我也在尝试支持 pre-honey comb 版本,我想我正在导入所有支持库。

【问题讨论】:

  • 乱扔代码可能不会鼓励别人帮助你。一些基本的细节会很棒。
  • @WarrenFaith - 我添加了一些关于代码的细节。对于那个很抱歉。我当时很急。谢谢。
  • 您不需要片段中的任何一个超级调用来覆盖 onCreateView;只是返回你的观点。

标签: android android-fragments


【解决方案1】:

删除您的导入语句:

import android.support.v4.app.FragmentActivity;

然后使用弹出的导入建议再次导入。我不知道为什么,但它总是对我有用。

import android.support.v4.app.FragmentActivity; 

【讨论】:

    【解决方案2】:

    修复错误:

    在清单中:

    <application
            android:largeHeap="true"
    

    在 .xml 文件中,您必须删除以下代码:

    android:src="@drawable..
    android:background="@drawable...
    

    因为报错是内存不足错误

    【讨论】:

      【解决方案3】:

      onCreateView 回调中,您必须返回您膨胀的view。这样系统就知道要绘制哪个View。否则,如果你只是这样返回super.onCreateView(...);,则不会出现任何UI。

      试试这个:

      public View onCreateView(LayoutInflater inflater,
                               ViewGroup container,
                               Bundle savedInstanceState) {
          super.onCreateView(inflater, container, savedInstanceState);
          try {
              View view = inflater.inflate(R.layout.prediction_time, container, false);
              Button button = (Button) view.findViewById(R.id.test);
              button.setOnClickListener(new OnClickListener() {
      
                  @Override
                  public void onClick(View v) {
                      test(v);
                  }
              });
          } catch(InflateException e) {
              e.printStackTrace();
              System.out.println("Predicted Time Fragment");
          }
          return view;
      }
      

      相应地更改另一个Fragment的返回值。

      【讨论】:

      • 我只是注意到这个很旧,并且有一个 wkhatch 的评论和正确的解决方案。
      【解决方案4】:

      尝试用 android.support.v4.app.Fragment 替换 xml 中的片段

      编辑:当您在 onCreateView 中完成视图时,也尝试调用 super ,即只调用一次。在开始时删除对 super 的调用,看看是否有帮助

      【讨论】:

      • 您是否尝试删除您对 super 的呼叫?因为您要调用它两次。能发一下logcat吗?
      • 实际上我重做了我的工作,它开始工作了,我相信它可能是xml中片段中的名称(android:name)我将其更改为class=“片段类的名称”但我以前做过一次,它没有奏效。不知道为什么上面的代码不起作用。无论如何感谢您的帮助。
      【解决方案5】:

      更改您的布局小部件 ID,它对我有用。

      【讨论】:

        猜你喜欢
        • 2016-07-11
        • 1970-01-01
        • 2012-03-04
        • 2017-09-13
        • 2013-10-07
        • 2015-05-01
        • 2014-06-18
        • 2015-01-13
        • 2013-09-10
        相关资源
        最近更新 更多