2016-12-26 60 views
0

我想在平板視圖中顯示列表+說明。以下代碼在縱向模式下正常工作,但在橫向模式下時停止。有什麼問題嗎?二進制XML文件行#20:錯誤擴展類片段Android

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

佈局/ 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 /佈局-脊/ 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> 

錯誤

E/AndroidRuntime:致命異常:主 工藝: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.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 $ hhandleMessage(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) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 引起:android.view.InflateException:二進制XML文件行#20:錯誤的擴展類片段 在android.view.LayoutInflater.createViewFromTag( LayoutInflater.java:763) 在android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 在android.view.LayoutInflater.rInflate(LayoutInflater.java:809) 在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 andro id.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) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 在android.app.ActivityThread.access $ 800(ActivityThread。Java的:151) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1303) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(活套。 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) 由java導致:java .lang.NullPointerException at java.lang.VMClassLoader.findLoadedClass(Native Method) 在java.lang.ClassLoader.findLoadedClass(ClassLoader.java:362) 在java.lang.ClassLoader.loadClass(ClassLoader.java:499) 在java.lang.ClassLoader.loadClass(ClassLoader.java:469) 在android.app.Fragment.instantiate(Fragment.java:604) 在android.app.Fragment.instantiate(Fragment.java:582) 在android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2108) 在機器人。 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) 在android.view.LayoutInflater.inflate(LayoutInflater.java:504) 在android.view.LayoutInflater.inflate(LayoutInflater.java:414) 在android.view.LayoutInflater.inflate(LayoutInflater.java:365) 在com.example.fyp_awais.tab.MainActivity.onCreate()中的com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378) (android.app.Activity.setContentView(Activity.java:2145) 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 )Android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) 在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) 應用程序已終止。

+0

發佈您的錯誤日誌。 – Noorul

+0

@ user6750923,根據你的代碼片段,我找不到任何xml文件中的phone_container,你可以更新主要活動的xml – TejaDroid

+0

@TejaDroid更新 – user6750923

回答

1

你確定你收到錯誤風景模式嗎?因爲在風景其運行正常並且在肖像模式下你沒有使用動態片段,所以你需要擴展片段活動而不是Activity來運行這個片段。如果您使用動態片段而不是硬編碼,那將會很好。

更改您的佈局端口/ 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> 

//你的完整的解決方案

主要活動

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

     } 
    } 
} 

端口:

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

土地:

<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

你能否提供一個工作示例動態片段? – user6750923

+0

我正在使用平板電腦虛擬設備(Nexus-9)。它可以在縱向模式下找到,但在橫向上停止。 – user6750923

+0

對於風景,您正在使用這種動態。 – Spartan

0

我認爲錯誤是在XML文件

<fragment>如果你想使用XML的fragment則需要定義類片段作爲

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

else定義片段內部代碼並將xml中的FrameLayout設置爲

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

是的。它正在工作。但是我想(列表+ desc)並排在平板電腦中。 – user6750923

+0

@ user6750923,請檢查此鏈接,「http://stackoverflow.com/questions/12253965/complete-working-sample-of-the-gmail-three-fragment-animation-scenario」,我想你得到了你的答案題。還有一個「https://commonsware.com/Android/previews/large-screen-strategies-and-tactics」解釋策略... – TejaDroid

相關問題