Home Articles

二进制XML文件行#20:错误膨胀类片段Android

Asked
Viewed 1586 times
0

我想在平板电脑视图中显示列表说明 . 以下代码在纵向模式下工作正常,但在横向模式下停止 . 这有什么不对吗?

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进程:com.example.fyp_awais.tab,PID:3974 java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.fyp_awais.tab/com.example.fyp_awais.tab . MainActivity}:android.view.InflateException:二进制XML文件行#20:在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)的android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)中错误地膨胀类片段在android.app.ActivityThread.access $ 800(ActivityThread.java:151)android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1303)在android.os.Handler.dispatchMessage(Handler.java:102)在android位于android.app.ActivityThread.main(ActivityThread.java:5254)的.os.Looper.loop(Looper.java:135)位于java.lang.reflect.Method的java.lang.reflect.Method.invoke(Native Method) .invoke(Method.java:372)com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903)at com.android.internal.os.ZygoteInit.main(ZygoteIni) t.java:698)由以下原因引起:android.view.InflateException:二进制XML文件行#20:在android.view.LayoutInflater.rInflate(在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)中填充类片段时出错( LayoutInflater.java:806)android.view.LayoutInflater.inflate(LayoutInflater.java:804)android.view.LayoutInflater.inflate(LayoutInflater.java:804)android.view.LayoutInflater.inflate(LayoutInflater.java:414) )在Android.app.Activity.setContentView(Activity.java:)的com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378)的android.view.LayoutInflater.inflate(LayoutInflater.java:365)中 . 2145)在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)的android.app.Activity.performCreate(Activity.java:5990)的com.example.fyp_awais.tab.MainActivity.onCreate(MainActivity.java:56) )在android.app.ActivityThread.handleLaunchActivity(ActivityTh)的android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) read.java:2387)在Android.app.Handler.dispatchMessage(处理程序 . )的android.app.ActivityThread.access $ 800(ActivityThread.java:151)android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1303) . java:102)在android.app.Looper.loop(Looper.java:135)的android.app.ActivityThread.main(ActivityThread.java:5254)java. java.lang.reflect.Method.invoke(Native Method)位于com.android.internal.os.ZygoteInit.main的com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903)中的.lang.reflect.Method.invoke(Method.java:372)(ZygoteInit) .java:698)引起:java.lang.ClassLoader.findLoadedClass(ClassLoader.java:362)java.lang.ClassLoader.loadClass(ClassLoader)java.lang.VMClassLoader.findLoadedClass(Native Method)的java.lang.NullPointerException .java:499)在android.app.Fragment.instantiate(Fragment.java:582)的android.app.Fragment.instantiate(Fragment.java:604)的java.lang.ClassLoader.loadClass(ClassLoader.java:469)在android.app.FragmentManagerImpl.onCreateView(Fr agmentManager.java:2108)在android.view.LayoutInflater.rInflate(LayoutInflater.java:806)的android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)的android.app.Activity.onCreateView(Activity.java:5328) )android.view.LayoutInflater.inflate(LayoutInflater.java:504)的android.view.LayoutInflater.inflate(LayoutInflater.java:504)android.view.LayoutInflater.inflate(LayoutInflater.java:414)android.view .LayoutInflater.inflate(LayoutInflater.java:365)位于com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378),位于com的android.app.Activity.setContentView(Activity.java:2145) . 在Android.app的android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)的android.app.Activity.performCreate(Activity.java:5990)的example.fyp_awais.tab.MainActivity.onCreate(MainActivity.java:56) .ActivityThread.performLaunchActivity(ActivityThread.java:2278)在android.ap.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)的android.ap上p.ActivityThread.access $ 800(ActivityThread.java:151)在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1303)at atandroid.os.Handler.dispatchMessage(Handler.java:102)位于android.app.Looper.loop(Looper.java:135)的android.app.ActivityThread.main(ActivityThread.java:5254),位于java.lang.reflect com.android上com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903)的java.lang.reflect.Method.invoke(Method.java:372)中的.Method.invoke(Native Method) .internal.os.ZygoteInit.main(ZygoteInit.java:698)申请已终止 .

2 Answers

  • 1

    您确定在 landscape 模式下收到错误吗?因为在_1836369中它运行良好并且在 portrait 模式下你没有使用动态片段,所以你需要扩展 Fragment Activity 而不是Activity来运行片段 . 如果你使用动态片段代替硬编码会很好 .

    将layout-port / main_activity更改为此

    <?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>
    

    //你的完整解决方案

    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>
    
  • 0

    我认为错误发生在 <fragment> 的xml文件中

    如果要在xml中使用fragment,则需要将该片段的类定义为

    <fragment
            class="com.example.android.app.fragmentName"
            android:id="@+id/titles"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    

    否则定义片段内部代码并将xml中的FrameLayout设置为

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

Related