我试图创建一个布局与三个垂直面板-2段和1帧布局。第一片段将是一个名单上的选择,第二片段将有不同的内容。在从第2段中选择列表项时,第3窗格将显示详细信息。
这是我的布局文件:
activity_three_pane.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:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:divider="?android:attr/dividerHorizontal"
android:orientation="horizontal"
android:showDividers="middle"
tools:context=".ActivitySectionList" >
<fragment
android:id="@+id/frg_section_list"
android:name="eam.droid.pt.entholkaappiyam.FragmentSectionList"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@android:layout/list_content" />
<fragment
android:id="@+id/frg_chapter_list"
android:name="eam.droid.pt.entholkaappiyam.FragmentChapterList"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@android:layout/list_content" />
<FrameLayout
android:id="@+id/fl_chapter_detail"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2" />
</LinearLayout>activity_section_list.xml:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frg_section_list"
android:name="eam.droid.pt.entholkaappiyam.FragmentSectionList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
tools:context=".ActivitySectionList"
tools:layout="@android:layout/list_content" />activity_chapter_list.xml:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frg_chapter_list"
android:name="eam.droid.pt.entholkaappiyam.FragmentChapterList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
tools:context=".ActivityChapterList"
tools:layout="@android:layout/list_content" />activity_chapter_detail.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fl_chapter_detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ActivityChapterDetail"
tools:ignore="MergeRootFrame" />Java代码如下所示:
ActivitySectionList.java
public class ActivitySectionList extends FragmentActivity implements FragmentSectionList.CallbacksFragmentSectionList.java
public class FragmentSectionList extends ListFragmentActivityChapterList.java
public class ActivityChapterList extends FragmentActivity implements FragmentChapterList.CallbacksFragmentChapterList.java
public class FragmentChapterList extends ListFragmentActivityChapterDetail.java
public class ActivityChapterDetail extends FragmentActivityFragmentChapterDetail.java
public class FragmentChapterDetail extends Fragment但是应用程序崩溃时会出现充气异常和IllegalStateException。过去两天我一直在努力,但找不出问题出在哪里。如果有人对此有想法,请回复。
我注意到的一件事是,它在手机上运行得很好,整个屏幕都需要单独的片段。但在平板电脑上崩溃,平板电脑试图在屏幕上安装三个片段。当我调试时,我开始了解到,FragmentChaairList.java的OnAttach在ActivityChaairList.java的onCreate之前被调用。这就是为什么,它给出了IllegaltStateException: Activity必须实现片段的回调。因此,我试图在ActivitySectionList.java中实现这两个片段回调。
就像这样:
public class ActivitySectionList extends FragmentActivity implements FragmentSectionList.Callbacks, FragmentChapterList.Callbacks然后它在平板电脑上工作得很好。但是,无法在电话中检索章节列表。
这是我的日志:
02-12 14:14:55.858: E/AndroidRuntime(989): FATAL EXCEPTION: main
02-12 14:14:55.858: E/AndroidRuntime(989): java.lang.RuntimeException: Unable to start activity ComponentInfo{eam.droid.pt.entholkaappiyam/eam.droid.pt.entholkaappiyam.ActivitySectionList}: android.view.InflateException: Binary XML file line #20: Error inflating class fragment
02-12 14:14:55.858: E/AndroidRuntime(989): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
02-12 14:14:55.858: E/AndroidRuntime(989): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
02-12 14:14:55.858: E/AndroidRuntime(989): at android.app.ActivityThread.access$600(ActivityThread.java:130)
02-12 14:14:55.858: E/AndroidRuntime(989): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
02-12 14:14:55.858: E/AndroidRuntime(989): at android.os.Handler.dispatchMessage(Handler.java:99)
02-12 14:14:55.858: E/AndroidRuntime(989): at android.os.Looper.loop(Looper.java:137)
02-12 14:14:55.858: E/AndroidRuntime(989): at android.app.ActivityThread.main(ActivityThread.java:4745)
02-12 14:14:55.858: E/AndroidRuntime(989): at java.lang.reflect.Method.invokeNative(Native Method)
02-12 14:14:55.858: E/AndroidRuntime(989): at java.lang.reflect.Method.invoke(Method.java:511)
02-12 14:14:55.858: E/AndroidRuntime(989): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-12 14:14:55.858: E/AndroidRuntime(989): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-12 14:14:55.858: E/AndroidRuntime(989): at dalvik.system.NativeStart.main(Native Method)
02-12 14:14:55.858: E/AndroidRuntime(989): Caused by: android.view.InflateException: Binary XML file line #20: Error inflating class fragment
02-12 14:14:55.858: E/AndroidRuntime(989): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
02-12 14:14:55.858: E/AndroidRuntime(989): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
02-12 14:14:55.858: E/AndroidRuntime(989): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
02-12 14:14:55.858: E/AndroidRuntime(989): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
02-12 14:14:55.858: E/AndroidRuntime(989): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
02-12 14:14:55.858: E/AndroidRuntime(989): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
02-12 14:14:55.858: E/AndroidRuntime(989): at android.app.Activity.setContentView(Activity.java:1867)
02-12 14:14:55.858: E/AndroidRuntime(989): at eam.droid.pt.entholkaappiyam.ActivitySectionList.onCreate(ActivitySectionList.java:20)
02-12 14:14:55.858: E/AndroidRuntime(989): at android.app.Activity.performCreate(Activity.java:5008)
02-12 14:14:55.858: E/AndroidRuntime(989): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
02-12 14:14:55.858: E/AndroidRuntime(989): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
02-12 14:14:55.858: E/AndroidRuntime(989): ... 11 more
02-12 14:14:55.858: E/AndroidRuntime(989): Caused by: java.lang.IllegalStateException: Activity must implement fragment's callbacks.
02-12 14:14:55.858: E/AndroidRuntime(989): at eam.droid.pt.entholkaappiyam.FragmentChapterList.onAttach(FragmentChapterList.java:84)
02-12 14:14:55.858: E/AndroidRuntime(989): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:867)
02-12 14:14:55.858: E/AndroidRuntime(989): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1066)
02-12 14:14:55.858: E/AndroidRuntime(989): at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1168)
02-12 14:14:55.858: E/AndroidRuntime(989): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:280)
02-12 14:14:55.858: E/AndroidRuntime(989): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)有人能告诉我哪里出了问题吗?如果有人遇到教程或3个垂直面板的例子(2个列表和1个细节),请给我指点。
下面是详细的场景:
发布于 2013-02-21 16:55:12
我假设activity_three_pane.xml只在平板电脑上膨胀,在ActivitySectionList类中。在这种情况下,对于平板电脑来说,FragmentSectionList和FragmentChapterList这两个片段都是作为布局的一部分而膨胀的,因此需要在ActivitySectionList中实现回调,这是异常的来源。
这个设置看起来非常复杂和混乱。我建议把你的活动分开,这样你就不会有重叠的代码了。这将帮助您更容易地诊断问题,并清理代码。示例:
public class BookActivity extends FragmentActivity implements FragmentSectionList.Callbacks, FragmentChapterList.Callbacks
{
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_three_pane);
}
}
public class ActivitySectionList extends FragmentActivity implements FragmentSectionList.Callbacks
{
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_section_list);
}
}然后,在任何调用这些活动的活动中:
if( ( getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK ) == Configuration.SCREENLAYOUT_SIZE_XLARGE ) {
startActivity( new Intent(this, BookActivity.class);
}
else {
startActivity( new Intent(this, ActivitySectionList.class);
}https://stackoverflow.com/questions/14840128
复制相似问题