当前位置: 首页 > 知识库问答 >
问题:

Android.view.fillateException:二进制XML文件第7行:inflating类片段时出错

饶谦
2023-03-14

我知道类似这样的问题已经得到了回答,但我似乎已经做了教程建议和阅读其他帖子,但我仍然得到了充气机错误。不知道我做错了什么。请检查代码。多谢.

        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));

            }

        }
            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);
            }

        }
        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);
            }
        }
        <?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>
        <?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>

我也试图支持蜂巢前的版本,我想我正在导入所有的支持库。

共有1个答案

童子明
2023-03-14

删除导入语句:

import android.support.v4.app.FragmentActivity;

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

import android.support.v4.app.FragmentActivity; 
 类似资料: