Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
398 views
in Technique[技术] by (71.8m points)

Binary XML file line #20: Error inflating class fragment Android

I want to show the list+Description in tablet view. Code below works fine with Portrait mode but stopped when in Landscape mode. Is it something wrong?

MainActivity

    import android.os.Bundle;
    import android.app.Activity;
    import android.app.Fragment;
    import android.app.FragmentTransaction;
    import android.app.ListFragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.ListAdapter;

    public class MainActivity extends Activity {

        // if run on phone, isSinglePane = true
        // if run on tablet, isSinglePane = false
        boolean isSinglePane;

        static String[] month ={
                "January", "February", "March", "April",
                "May", "June", "July", "August",
                "September", "October", "November", "December"};

        public static class MyListFragment extends ListFragment {

            @Override
            public void onActivityCreated(Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                super.onActivityCreated(savedInstanceState);

                ListAdapter myArrayAdapter =
                        new ArrayAdapter<String>(
                                getActivity(), android.R.layout.simple_list_item_1, month);
                setListAdapter(myArrayAdapter);

            }


        }

        public static class MyDetailFragment extends Fragment {

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                     Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                View view = inflater.inflate(R.layout.layout_detailfragment, null);
                return view;
            }

        }

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

            View v = findViewById(R.id.phone_container);
            if(v == null){
                //it's run on tablet
                isSinglePane = false;
       /*
        * MyListFragment and MyDetailFragment have been loaded in XML,
        * no need load.
        */

            }else{
                //it's run on phone
                //Load MyListFragment programmatically
                isSinglePane = true;

                if(savedInstanceState == null){
                    //if's the first time created
                    MyListFragment myListFragment = new MyListFragment();
                    FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
                    fragmentTransaction.add(R.id.phone_container, myListFragment);
                    fragmentTransaction.commit();
                }
            }
        }
    }

Layout/activityMain.xml

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

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

    </FrameLayout>

res/Layout-land/activitymain.xml

    <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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Normal" />

    <FrameLayout
        android:id="@+id/phone_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>

</LinearLayout>

LayoutdetailFragment.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">

    <TextView
        android:id="@+id/title_detailfragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Detail Fragment"/>

</LinearLayout>

Error

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.fyp_awais.tab, PID: 3974 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fyp_awais.tab/com.example.fyp_awais.tab.MainActivity}: android.view.InflateException: Binary XML file line #20: Error inflating class fragment at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 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:5254) 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 #20: Error inflating class fragment at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763) at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) at android.view.LayoutInflater.rInflate(LayoutInflater.java:809) 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 com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378) at android.app.Activity.setContentView(Activity.java:2145) at com.example.fyp_awais.tab.MainActivity.onCreate(MainActivity.java:56) 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:2387)? 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:5254)? 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.app.Fragment.instantiate(Fragment.java:604) at android.app.Fragment.instantiate(Fragment.java:582) at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2108) at android.app.Activity.onCreateView(Activity.java:5328) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733) at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)? at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)? 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 com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378)? at android.app.Activity.setContentView(Activity.java:2145)? at com.example.fyp_awais.tab.MainActivity.onCreate(MainActivity.java:56)? 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:2387)? 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:5254)? 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)? Application terminated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Are you sure you are getting error in landscape mode? because in landscape its running fine and in portrait mode you are not using dynamic fragment so that you need to extend Fragment Activity instead of Activity to run the fragment. It would be good if you use dynamic fragment instead hard-coded.

Change your layout-port/main_activity to this

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout  xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:id="@+id/phone_container2"
    >

  // remove hard coded fragment

</FrameLayout>

// your full solution

Main Activity

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;

public class MainActivity extends Activity {

    // if run on phone, isSinglePane = true
    // if run on tablet, isSinglePane = false
    boolean isSinglePane;

    static String[] month ={
            "January", "February", "March", "April",
            "May", "June", "July", "August",
            "September", "October", "November", "December"};

    public static class MyListFragment extends ListFragment {

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onActivityCreated(savedInstanceState);

            ListAdapter myArrayAdapter =
                    new ArrayAdapter<String>(
                            getActivity(), android.R.layout.simple_list_item_1, month);
            setListAdapter(myArrayAdapter);

        }

    }

    public static class MyDetailFragment extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            View view = inflater.inflate(R.layout.layout_detailfragment, null);
            return view;
        }

    }

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

        View v = findViewById(R.id.phone_container);
        if(v == null){
            //it's run on tablet
            isSinglePane = false;

            //if's the first time created
            MyDetailFragment myListFragment = new MyDetailFragment();
            FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
            fragmentTransaction.add(R.id.phone_container2, myListFragment);
            fragmentTransaction.commit();



       /*
        * MyListFragment and MyDetailFragment have been loaded in XML,
        * no need load.
        */

        }else{
            //it's run on phone
            //Load MyListFragment programmatically
            isSinglePane = true;


                //if's the first time created
                MyListFragment myListFragment = new MyListFragment();
                FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
                fragmentTransaction.add(R.id.phone_container, myListFragment);
                fragmentTransaction.commit();

        }
    }
}

port:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:id="@+id/phone_container2"
    >

  <!--  <fragment
        android:id="@+id/titles"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />-->

</FrameLayout>

land:

<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:paddingBottom="@dimen/activity_vertical_margin"
              android:paddingLeft="@dimen/activity_horizontal_margin"
              android:paddingRight="@dimen/activity_horizontal_margin"
              android:paddingTop="@dimen/activity_vertical_margin"
              android:orientation="vertical"
              tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Normal" />

    <FrameLayout
        android:id="@+id/phone_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>

</LinearLayout>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...