动态附加片段会导致应用程序崩溃

时间:2017-09-29 07:52:10

标签: android android-fragments

我正在使用android studio中的导航抽屉活动模板,我创建了一个名为WelcomeFragment的片段,我的问题是,每当我尝试将WelcomeFragment添加到我的Activity时应用程序崩溃。

以下是我的Android应用程序中的代码。

NavigationDrawerActivity onCreate()方法:

    super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigation_drawer);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);


    WelcomeFragment newFragment = WelcomeFragment.newInstance("testString","testString");
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.add(R.id.fragment_container, newFragment);
    transaction.commit();

content_navigation_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.test.thisisatest.NavigationDrawerActivity"
    tools:showIn="@layout/app_bar_navigation_drawer">

    <!-- FRAGMENT GOES HERE -->

</FrameLayout>

app_bar_navigation_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<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="com.example.test.thisisatest.NavigationDrawerActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        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
       android:id="@+id/content_navigation_drawer"
       layout="@layout/content_navigation_drawer"
       android:layout_height="wrap_content"
       android:layout_width="wrap_content"/>

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

activity_navigation_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        android:id="@+id/app_bar"
        layout="@layout/app_bar_navigation_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_navigation_drawer"
        app:menu="@menu/activity_navigation_drawer" />

</android.support.v4.widget.DrawerLayout>

WelcomeFragment的内容由android studio自动生成。

我已经按照here

中提到的问题的答案执行了片段

以下是我的logcat中的错误

null
java.util.NoSuchElementException
    at java.util.RegularEnumSet$EnumSetIterator.next(RegularEnumSet.java:102)
    at java.util.RegularEnumSet$EnumSetIterator.next(RegularEnumSet.java:78)
    at com.android.tools.idea.ui.resourcechooser.ChooseResourceDialog.getSelectedPanel(ChooseResourceDialog.java:500)
    at com.android.tools.idea.ui.resourcechooser.ChooseResourceDialog.setupViewOptions(ChooseResourceDialog.java:836)
    at com.android.tools.idea.ui.resourcechooser.ChooseResourceDialog.<init>(ChooseResourceDialog.java:338)
    at com.android.tools.idea.ui.resourcechooser.ChooseResourceDialog.<init>(ChooseResourceDialog.java:117)
    at com.android.tools.idea.ui.resourcechooser.ChooseResourceDialog$Builder.build(ChooseResourceDialog.java:268)
    at com.android.tools.idea.uibuilder.property.editors.BrowsePanel.showResourceChooser(BrowsePanel.java:164)
    at com.android.tools.idea.uibuilder.property.editors.BrowsePanel$BrowseAction.actionPerformed(BrowsePanel.java:145)
    at com.intellij.openapi.actionSystem.ex.ActionUtil$1.run(ActionUtil.java:197)
    at com.intellij.openapi.application.TransactionGuardImpl.runSyncTransaction(TransactionGuardImpl.java:88)
    at com.intellij.openapi.application.TransactionGuardImpl.submitTransactionAndWait(TransactionGuardImpl.java:156)
    at com.intellij.openapi.actionSystem.ex.ActionUtil.performActionDumbAware(ActionUtil.java:211)
    at com.intellij.openapi.actionSystem.impl.ActionButton.actionPerformed(ActionButton.java:189)
    at com.intellij.openapi.actionSystem.impl.ActionButton.performAction(ActionButton.java:155)
    at com.intellij.openapi.actionSystem.impl.ActionButton.click(ActionButton.java:138)
    at com.android.tools.idea.uibuilder.property.editors.BrowsePanel.mousePressed(BrowsePanel.java:118)
    at com.android.tools.idea.uibuilder.property.renderer.NlAttributeRenderer.mousePressed(NlAttributeRenderer.java:124)
    at com.android.tools.idea.uibuilder.property.NlFlagPropertyItem.mousePressed(NlFlagPropertyItem.java:203)
    at com.android.tools.idea.uibuilder.property.ptable.PTable$MouseTableListener.mousePressed(PTable.java:529)
    at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:280)
    at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:279)
    at java.awt.Component.processMouseEvent(Component.java:6530)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6298)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4522)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:795)
    at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:627)
    at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:387)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)


java.util.NoSuchElementException
    at java.util.RegularEnumSet$EnumSetIterator.next(RegularEnumSet.java:102)
    at java.util.RegularEnumSet$EnumSetIterator.next(RegularEnumSet.java:78)
    at com.android.tools.idea.ui.resourcechooser.ChooseResourceDialog.getSelectedPanel(ChooseResourceDialog.java:500)
    at com.android.tools.idea.ui.resourcechooser.ChooseResourceDialog.setupViewOptions(ChooseResourceDialog.java:836)
    at com.android.tools.idea.ui.resourcechooser.ChooseResourceDialog.<init>(ChooseResourceDialog.java:338)
    at com.android.tools.idea.ui.resourcechooser.ChooseResourceDialog.<init>(ChooseResourceDialog.java:117)
    at com.android.tools.idea.ui.resourcechooser.ChooseResourceDialog$Builder.build(ChooseResourceDialog.java:268)
    at com.android.tools.idea.uibuilder.property.editors.BrowsePanel.showResourceChooser(BrowsePanel.java:164)
    at com.android.tools.idea.uibuilder.property.editors.BrowsePanel$BrowseAction.actionPerformed(BrowsePanel.java:145)
    at com.intellij.openapi.actionSystem.ex.ActionUtil$1.run(ActionUtil.java:197)
    at com.intellij.openapi.application.TransactionGuardImpl.runSyncTransaction(TransactionGuardImpl.java:88)
    at com.intellij.openapi.application.TransactionGuardImpl.submitTransactionAndWait(TransactionGuardImpl.java:156)
    at com.intellij.openapi.actionSystem.ex.ActionUtil.performActionDumbAware(ActionUtil.java:211)
    at com.intellij.openapi.actionSystem.impl.ActionButton.actionPerformed(ActionButton.java:189)
    at com.intellij.openapi.actionSystem.impl.ActionButton.performAction(ActionButton.java:155)
    at com.intellij.openapi.actionSystem.impl.ActionButton.click(ActionButton.java:138)
    at com.android.tools.idea.uibuilder.property.editors.BrowsePanel.mousePressed(BrowsePanel.java:118)
    at com.android.tools.idea.uibuilder.property.renderer.NlAttributeRenderer.mousePressed(NlAttributeRenderer.java:124)
    at com.android.tools.idea.uibuilder.property.NlFlagPropertyItem.mousePressed(NlFlagPropertyItem.java:203)
    at com.android.tools.idea.uibuilder.property.ptable.PTable$MouseTableListener.mousePressed(PTable.java:529)
    at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:280)
    at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:279)
    at java.awt.Component.processMouseEvent(Component.java:6530)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6298)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4522)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:795)
    at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:627)
    at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:387)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

0 个答案:

没有答案