无法启动活动ComponentInfo {Activity}:android.view.InflateException:二进制XML文件行错误膨胀类片段

时间:2015-07-21 17:25:34

标签: android android-fragments

当我尝试在活动中启动片段时,我收到以下异常。看起来这是一个常见问题,我拥有答案中建议的所有权限。跟踪不是很有帮助。

Process: com.eazysystems.transdoc, PID: 11129
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxxx.xxxx/com.xxxx.xxxx.view.ViewRecordActivity}: android.view.InflateException: Binary XML file line #5: Error inflating class fragment
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5257)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
 Caused by: android.view.InflateException: Binary XML file line #5: Error inflating class fragment
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
        at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
        at com.eazysystems.transdoc.view.ViewRecordActivity.onCreate(ViewRecordActivity.java:21)
        at android.app.Activity.performCreate(Activity.java:5990)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
 Caused by: java.lang.NullPointerException
        at java.lang.VMClassLoader.findLoadedClass(Native Method)
        at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:362)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:499)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
        at android.support.v4.app.Fragment.isSupportFragmentClass(Fragment.java:455)
        at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2135)
        at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:300)
        at android.support.v7.app.AppCompatDelegateImplV7.callActivityOnCreateView(AppCompatDelegateImplV7.java:838)
        at android.support.v7.app.AppCompatDelegateImplV11.callActivityOnCreateView(AppCompatDelegateImplV11.java:34)
        at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:826)
        at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:725)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
            at com.eazysystems.transdoc.view.ViewRecordActivity.onCreate(ViewRecordActivity.java:21)
            at android.app.Activity.performCreate(Activity.java:5990)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

此外,异常发生在此Activity类的setContentView(R.layout.activity_view_record);。我试图使应用程序尽可能简单但它得到相同的异常

    public class ViewRecordActivity extends AppCompatActivity {

    private static String RECORD_ID = "RECORD_ID";

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

        /*Bundle extras = getIntent().getExtras();
        Integer recordId = extras.getInt(RECORD_ID);*/

        ViewRecordFragment viewRecordFragment = ViewRecordFragment.newInstance(1);
        //viewRecordFragment.setArguments(getIntent().getExtras());

        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager
                .beginTransaction()
                .replace(R.id.view_record_fragment, viewRecordFragment
                ).commit();
    }


    public static class ViewRecordFragment extends Fragment {

        private static String RECORD_ID = "RECORD_ID";

        public ViewRecordFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            /*gridview = (GridView) getActivity().findViewById(R.id.image_grid_view);*/

            //Integer recordId = getArguments().getInt(RECORD_ID);
            //loadImageResourcesAsync(recordId);

            return inflater.inflate(R.layout.fragment_view_record, container, false);
        }

        public static ViewRecordFragment newInstance(Integer recordId) {
            ViewRecordFragment fragment = new ViewRecordFragment();
            //fragment.loadImageResourcesAsync(recordId);
            /*Bundle bundle = new Bundle();
            bundle.putInt(RECORD_ID, recordId);
            fragment.setArguments(bundle);*/
            return fragment;
        }

        private void loadImageResourcesAsync(Integer recordId) {
            new LoadImageResourcesAsync(getActivity()).execute(recordId);
        }

    }
}

活动的布局如下。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <fragment
            android:id="@+id/view_record_fragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />


        <!--<fragment xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/view_record_fragment"
android:name="com.xxx.xxx.view.ViewRecordActivity$ViewRecordFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:layout="@layout/fragment_view_record" />-->


    </LinearLayout>

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

在布局中添加片段的基本方法如下所示。你必须提到片段的类名。

 <fragment android:name="com.example.android.fragments.HeadlinesFragment"
          android:id="@+id/headlines_fragment"
          android:layout_weight="1"
          android:layout_width="0dp"
          android:layout_height="match_parent" />