首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Jetpack导航从活动中打开片段

使用Jetpack导航从活动中打开片段
EN

Stack Overflow用户
提问于 2021-03-03 03:28:36
回答 1查看 355关注 0票数 0

我真的很难找到一种方法,用Navcontroller打开活动中的片段。

我收到一个错误,指出该视图是一个按钮,不包含Navcontroller。

我尝试使用按钮视图创建navcontroller,但是这会导致一个错误,指出它不包含navcontroller

我想从一个按钮的onclick监听器中开始这个片段:

代码语言:javascript
复制
register_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent registerIntent = new Intent(StartActivity.this, RegisterActivity.class);
            startActivity(registerIntent);
            finish();
        }
    });
}

布局代码

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".LoginRegister.StartActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome to Aston Connect"
        android:textColor="@color/black"
        android:textSize="24dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.38" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:text="Do you have an account?"
        android:textColor="@color/black"
        android:textSize="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        app:layout_constraintVertical_bias="0.13" />

    <Button
        android:id="@+id/register_btn"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="Need a new account?"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2"
        app:layout_constraintVertical_bias="0.365" />

    <Button
        android:id="@+id/login_btn"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="Already have an account?"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/register_btn"
        app:layout_constraintVertical_bias="0.17000002" />


</androidx.constraintlayout.widget.ConstraintLayout>

我的导航图:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<navigation 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/navgraph"
    app:startDestination="@id/newsfeedFragment">

    <fragment
        android:id="@+id/activityOverviewFragment"
        android:name="com.devin.astonconnect.ActivityOverviewFragment"
        android:label="fragment_activity_overview"
        tools:layout="@layout/fragment_activity_overview" >
        <action
            android:id="@+id/action_activityOverviewFragment_to_selectedPostFragment"
            app:destination="@id/selectedPostFragment" />
        <action
            android:id="@+id/action_activityOverviewFragment_to_profileFragment"
            app:destination="@id/profileFragment" />
    </fragment>


    <fragment
        android:id="@+id/newsfeedFragment"
        android:name="com.devin.astonconnect.NewsfeedFragment"
        android:label="fragment_newsfeed"
        tools:layout="@layout/fragment_newsfeed">
        <action
            android:id="@+id/action_homeFragment_to_profileFragment"
            app:destination="@id/profileFragment" />
    </fragment>
    <fragment
        android:id="@+id/chatFragment"
        android:name="com.devin.astonconnect.Chat.ChatFragment"
        android:label="fragment_chat"
        tools:layout="@layout/fragment_chat" />
    <fragment
        android:id="@+id/searchFragment"
        android:name="com.devin.astonconnect.SearchFragment"
        android:label="fragment_search"
        tools:layout="@layout/fragment_search" >
        <action
            android:id="@+id/action_searchFragment_to_profileFragment"
            app:destination="@id/profileFragment" />
    </fragment>
    <fragment
        android:id="@+id/journalFragment"
        android:name="com.devin.astonconnect.Journal.JournalFragment"
        android:label="fragment_search"
        tools:layout="@layout/fragment_journal" >
        <action
            android:id="@+id/action_journalFragment_to_journalEntry1Fragment"
            app:destination="@id/journalEntry1Fragment" />
        <action
            android:id="@+id/action_journalFragment_to_viewJournalEntryFragment"
            app:destination="@id/viewJournalEntryFragment" />
    </fragment>
    <fragment
        android:id="@+id/profileFragment"
        android:name="com.devin.astonconnect.ProfileFragment"
        android:label="fragment_profile"
        tools:layout="@layout/fragment_profile" >
        <action
            android:id="@+id/action_profileFragment_to_selectedPostFragment"
            app:destination="@id/selectedPostFragment" />
    </fragment>
    <fragment
        android:id="@+id/selectedPostFragment"
        android:name="com.devin.astonconnect.SelectedPostFragment"
        android:label="fragment_selected_post"
        tools:layout="@layout/fragment_selected_post" />
    <fragment
        android:id="@+id/journalEntry1Fragment"
        android:name="com.devin.astonconnect.Journal.JournalEntry1Fragment"
        android:label="fragment_journal_entry1"
        tools:layout="@layout/fragment_journal_entry1" >
        <action
            android:id="@+id/action_journalEntry1Fragment_to_journalEntry2Fragment"
            app:destination="@id/journalEntry2Fragment" />
    </fragment>
    <fragment
        android:id="@+id/journalEntry2Fragment"
        android:name="com.devin.astonconnect.Journal.JournalEntry2Fragment"
        android:label="fragment_journal_entry2"
        tools:layout="@layout/fragment_journal_entry2" >
        <argument
            android:name="journalItemArg"
            app:argType="com.devin.astonconnect.Model.JournalItem" />
        <action
            android:id="@+id/action_journalEntry2Fragment_to_newsfeedFragment"
            app:destination="@id/newsfeedFragment" />
    </fragment>
    <fragment
        android:id="@+id/addReflectionFragment"
        android:name="com.devin.astonconnect.Journal.addReflectionFragment"
        android:label="fragment_journal_entry4"
        tools:layout="@layout/fragment_add_reflection" >
        <action
            android:id="@+id/action_addReflectionFragment_to_newsfeedFragment"
            app:destination="@id/newsfeedFragment" />
    </fragment>
    <fragment
        android:id="@+id/viewJournalEntryFragment"
        android:name="com.devin.astonconnect.Journal.ViewJournalEntryFragment"
        android:label="fragment_view_journal_entry"
        tools:layout="@layout/fragment_view_journal_entry" >
        <action
            android:id="@+id/action_viewJournalEntryFragment_to_addReflectionFragment"
            app:destination="@id/addReflectionFragment" />
    </fragment>
</navigation>
EN

回答 1

Stack Overflow用户

发布于 2021-03-03 18:17:30

我创建了一个单独的导航图,添加了我需要的片段,并将其用于导航,从而绕过了这个问题。我不想使用我在我的应用程序中一直使用的主导航图,因为理想情况下应该只有一个活动与之关联。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66445881

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档