Android Fragments - 2 个 fragment 和 1 个框架布局

标签 android android-layout android-fragments android-listfragment android-fragmentactivity

我正在尝试创建一个包含三个垂直面板的布局 - 2 个 fragment 和 1 个框架布局。第一个 fragment 将是一个选择列表,第二个 fragment 将有不同的内容。从第二个 fragment 中选择一个列表项时,第三个 Pane 将显示详细信息。

这是我的布局文件:

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.Callbacks

FragmentSectionList.java

public class FragmentSectionList extends ListFragment

ActivityChapterList.java

public class ActivityChapterList extends FragmentActivity implements FragmentChapterList.Callbacks

fragment 章节列表.java

public class FragmentChapterList extends ListFragment

ActivityChapterDetail.java

public class ActivityChapterDetail extends FragmentActivity

FragmentChapterDetail.java

public class FragmentChapterDetail extends Fragment

但是应用程序因 Inflate Exception 和 IllegalStateException 而崩溃。过去两天我一直在尝试,但无法找出问题所在。如果有人对此有想法,请回复。

我注意到的一件事是它在 Phone 上运行良好,它在整个屏幕上使用单个 fragment 。但在试图在屏幕上显示三个 fragment 的平板电脑上崩溃。当我调试时,我开始知道 FragmentChapterList.java 的 OnAttach 在 ActivityChapterList.java 的 onCreate 之前被调用。这就是为什么它会给出 IllegaltStateException:Activity 必须实现 fragment 的回调。因此,我尝试在 ActivitySectionList.java 中实现两个 fragment 回调。

像这样:

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 个详细信息)的教程或示例,请指出我。

这里是详细的场景:

最佳答案

我假设 activity_three_pane.xml 只会在平板电脑和 ActivitySectionList 类中膨胀。在这种情况下,对于平板电脑, fragment FragmentSectionListFragmentChapterList 都作为布局的一部分进行了扩充,因此您需要在 ActivitySectionList 中为这两个 fragment 实现回调>,这是您的异常来源。

这个设置看起来非常复杂和困惑。我建议拆分您的 Activity ,这样您就不会重叠代码。这将帮助您更轻松地诊断问题,并清理您的代码。示例:

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

然后在调用这些 Activity 的任何 Activity 中:

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

关于Android Fragments - 2 个 fragment 和 1 个框架布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14840128/

相关文章:

android - 如何在 for 循环中使用 drawRect 方法?

android - 如何在 android (java/xml) 中创建基本的滑动面板

java - 从 WebView 中获取 Fragment 的实例

android - 将对象从 Activity 传递到 Fragments 是通过引用传递

java - 通过关闭所有堆叠的 Activity 来启动新的 Activity

android - Cordova/Ionic 构建 android Gradle 错误 : Minimum supported Gradle version is 2. 14.1。当前版本是 2.13

android - 优化应用程序背景以改变方向?

android - 无法使按钮仅在单击时改变颜色(Android)

android - PhoneGap 中的 INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES 错误

android - EditText 在 TextInputLayout 内不可见