在导航抽屉活动中包含片段

时间:2019-06-14 08:43:21

标签: android xml kotlin menu fragment

我正在制作一个应用,我想在Android的示例Navigation Drawer Activity中包含片段。默认情况下,它会为我生成一个activity_main布局,其中包括带有以下内容的app_bar_main布局:

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

在app_bar_main活动中,我在include中包含另一种布局:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".UI.MainActivity">

<android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main"/>

<android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email"/>

当我将Fragment放入包含项时,就会出现问题:

    <include layout="@layout/content_main"/>

替换为:

    <fragment
        android:name="com.example.urbandictionary.SearchFragment"
        android:id="@+id/fSearch"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

我认为这应该可行,但是在进入MainActivty时会中断:

        setContentView(R.layout.activity_main)

错误似乎是试图将main thread中的内容添加到某个List中:

    public void registerOnActivityPausedListener(Activity activity,
        OnActivityPausedListener listener) {
    synchronized (mOnPauseListeners) {
        ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
        if (list == null) {
            list = new ArrayList<OnActivityPausedListener>();
            mOnPauseListeners.put(activity, list);
        }
        list.add(listener);
    }
}

并在创建list.add(listener)

时中断

日志:

  

2019-06-14 11:39:52.350 15257-15257 / com.example.urbandictionary E / AndroidRuntime:FATAL EXCEPTION:main       流程:com.example.urbandictionary,PID:15257       java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.urbandictionary / com.example.urbandictionary.UI.MainActivity}:android.view.InflateException:二进制XML文件行#12:二进制XML文件行26:错误膨胀类片段           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)           在android.app.ActivityThread.-wrap12(ActivityThread.java)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1477)           在android.os.Handler.dispatchMessage(Handler.java:102)           在android.os.Looper.loop(Looper.java:154)           在android.app.ActivityThread.main(ActivityThread.java:6126)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)        原因:android.view.InflateException:二进制XML文件第12行:二进制XML文件第26行:夸大类片段的错误        原因:android.view.InflateException:二进制XML文件行#26:错误夸大了类片段        引起原因:java.lang.RuntimeException:com.example.urbandictionary.UI.MainActivity@83a791c必须实现OnFragmentInteractionListener           在com.example.urbandictionary.SearchFragment.onAttach(SearchFragment.kt:58)           在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1404)           在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1684)           在android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1930)           在android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3745)           在android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)           在android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:405)           在android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:387)           在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:777)           在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)           在android.view.LayoutInflater.rInflate(LayoutInflater.java:858)           在android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)           在android.view.LayoutInflater.parseInclude(LayoutInflater.java:994)           在android.view.LayoutInflater.rInflate(LayoutInflater.java:854)           在android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)           在android.view.LayoutInflater.inflate(LayoutInflater.java:518)           在android.view.LayoutInflater.inflate(LayoutInflater.java:426)           在android.view.LayoutInflater.inflate(LayoutInflater.java:377)           在android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)           在android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)           在com.example.urbandictionary.UI.MainActivity.onCreate(MainActivity.kt:21)           在android.app.Activity.performCreate(Activity.java:6679)           在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)           在android.app.ActivityThread.-wrap12(ActivityThread.java)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1477)           在android.os.Handler.dispatchMessage(Handler.java:102)           在android.os.Looper.loop(Looper.java:154)           在android.app.ActivityThread.main(ActivityThread.java:6126)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

0 个答案:

没有答案