【问题标题】:activity class intent into a specific fragment (fragment in a navigation drawer)活动类意图进入特定片段(导航抽屉中的片段)
【发布时间】:2019-10-21 01:14:57
【问题描述】:

我试图从 Activity 到导航抽屉中的特定片段。我正在尝试片段交易,但它对我不起作用。

这是我的代码:

我的活动(点击活动从导航抽屉转到特定片段):

 private void checkCarts() {
        checkCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                fragment=new EppViewCartV2();
                if (fragment != null) {
                    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                    ft.replace(R.id.frams, fragment);
                    ft.commit();
                }



            }
        });
    }

Error code upon logcat

  [1]: https://i.stack.imgur.com/uuFMq.png

my error (upon using getFragmentManager)

my farams (framelayout):

这是我的基本活动(): - 这是应用栏活动:

<?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=".ItemViewProducts.EppItemPreviewPhone">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#f2f2f2"
        app:elevation="0dp"
        android:theme="@style/AppTheme.NoActionBar.AppBarOverlay">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        <ImageView
            android:id="@+id/view_back"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_marginLeft="18dp"
            android:layout_marginTop="20dp"
            android:src="@drawable/arrow_back"/>


            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_marginRight="15dp"
                android:layout_height="wrap_content">
            <RelativeLayout
                android:id="@+id/qqt"
                android:layout_width="wrap_content"
                android:layout_marginTop="20dp"
                android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/view_cart2"
                android:layout_width="26dp"
                android:layout_marginRight="10dp"
                android:layout_height="26dp"
                android:src="@drawable/shopping"/>
            </RelativeLayout>
            <TextView
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_marginTop="12dp"
                android:id="@+id/badge_notification"
                android:layout_alignRight="@id/qqt"
                android:background="@drawable/item_status"
                android:textAlignment="center"
                android:text="12"
                android:textColor="#fff"
                android:padding="3dp"
                android:textSize="10sp"
                android:textStyle="bold"
                ></TextView>

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

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

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fbutton_add_to_cart"
        app:rippleColor="@null"
        app:backgroundTint="@null"
        app:maxImageSize="30dp"
        android:scaleType="center"
        android:layout_margin="@dimen/fab_margin"
        android:backgroundTint="#b578ff"
        app:srcCompat="@drawable/cart_big"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end" />


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

内容活动:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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="wrap_content"
    android:layout_height="wrap_content"
    android:background="#f2f2f2"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".ItemViewProducts.EppItemPreviewPhone"
    tools:showIn="@layout/activity_epp_item_preview_phone">


    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/relativeLayout2"
        android:layout_width="match_parent"
        android:layout_height="410dp"
        android:layout_marginBottom="8dp"
        android:background="@drawable/radius_all"
        app:layout_constraintBottom_toTopOf="@+id/relativeLayout4"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="240dp">

            <android.support.v4.view.ViewPager
                android:id="@+id/viewPager2"
                android:layout_width="match_parent"
                android:background="@drawable/radius_all"
                android:adjustViewBounds="true"
                android:scaleType="centerCrop"
                android:layout_height="240dp"
                android:layout_marginBottom="5dp" />

            <ImageView
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:id="@+id/rightright"
                android:layout_centerVertical="true"
                android:src="@drawable/leftleft"
                android:layout_marginLeft="10dp">
            </ImageView>

            <ImageView
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_centerVertical="true"
                android:id="@+id/leftleft"
                android:layout_alignParentRight="true"
            android:src="@drawable/rightright"
            android:layout_marginRight="10dp">
            </ImageView>


        </RelativeLayout>


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_marginTop="70dp"
            android:layout_height="match_parent">
        <TextView
            android:layout_width="280dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="190dp"
            android:text="Harman Karton - AURA STUDIO 2"
            android:textColor="#000"
            android:id="@+id/view_subject"
            android:textSize="18dp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="90dp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginTop="245dp"
            android:id="@+id/view_on_hand"
            android:text="Available(150)"
            android:textSize="11dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="238dp"
            android:text="P 14,999.00"
            android:id="@+id/view_new_price"
            android:textColor="#f24800"
            android:textSize="21dp"
            android:textStyle="bold" />


        <TextView
            android:layout_width="110dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="270dp"
            android:text="P 145,999.00"
            android:id="@+id/view_srp"
            android:textSize="16dp" />


        <TextView
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="150dp"
            android:layout_marginTop="270dp"
            android:text=" - 50%"
            android:id="@+id/view_discount"
            android:textColor="#af0000"
            android:textSize="17dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="275dp"
            android:layout_marginRight="110dp"
            android:layout_alignParentRight="true"
            android:text="x"
            android:textSize="12dp" />

        <ImageView
            android:layout_width="20dp"
            android:id="@+id/view_btn_less"
            android:layout_height="20dp"
            android:layout_marginTop="273dp"
            android:layout_marginRight="85dp"
            android:layout_alignParentRight="true"
            android:src="@drawable/left_arrow" />

        <TextView
            android:layout_width="30dp"
            android:layout_height="20dp"
            android:layout_marginTop="273dp"
            android:layout_marginRight="53dp"
            android:layout_alignParentRight="true"
            android:text="1"
            android:textAlignment="center"
            android:id="@+id/view_count"
            android:textColor="#000"
            android:textSize="14dp" />


        <ImageView
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:id="@+id/view_btn_add"
            android:layout_marginTop="273dp"
            android:layout_marginRight="30dp"
            android:layout_alignParentRight="true"
            android:src="@drawable/right_arrow"
            android:textSize="17dp" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="300dp"
            android:layout_marginRight="20dp"
            android:background="#bababa"
            android:src="@drawable/right_arrow"
            android:textSize="17dp" />


        <TextView
            android:layout_width="45dp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="310dp"
            android:layout_marginRight="150dp"
            android:text="Total"
            android:textSize="15dp" />

        <TextView
            android:layout_width="140dp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="305dp"
            android:layout_marginRight="10dp"
            android:text="P 14,999.00"
            android:id="@+id/view_total"
            android:textColor="#000"
            android:textSize="18dp"
            android:textStyle="bold" />

        </RelativeLayout>

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/relativeLayout4"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="420dp"
        android:layout_marginBottom="8dp"
        android:background="@drawable/radius_all"
        app:layout_constraintBottom_toTopOf="@+id/relativeLayout3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/relativeLayout2">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="25dp"
            android:text="Color Variation"
            android:textSize="12dp" />


        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="?android:attr/selectableItemBackground"
            android:clickable="true"
            android:focusable="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="190dp"
            android:id="@+id/color5"
            android:layout_alignParentRight="true"

            android:textSize="17dp" />

        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="?android:attr/selectableItemBackground"
            android:clickable="true"
            android:focusable="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="150dp"
            android:id="@+id/color4"
            android:layout_alignParentRight="true"

            android:textSize="17dp" />

        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="?android:attr/selectableItemBackground"
            android:clickable="true"
            android:focusable="true"
            android:id="@+id/color3"
            android:layout_centerVertical="true"
            android:layout_marginRight="110dp"
            android:layout_alignParentRight="true"

            android:textSize="17dp" />

        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="?android:attr/selectableItemBackground"
            android:clickable="true"
            android:focusable="true"
            android:layout_marginRight="70dp"
            android:id="@+id/color2"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"

            android:textSize="17dp" />

        <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
            android:background="?android:attr/selectableItemBackground"
            android:clickable="true"
            android:focusable="true"
        android:layout_marginRight="30dp"
        android:layout_centerVertical="true"
        android:id="@+id/color1"
        android:layout_alignParentRight="true"

        android:textSize="17dp" />




    </RelativeLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_marginTop="478dp"
            android:orientation="vertical"
            android:layout_height="match_parent">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:text="Product description"
            android:textSize="14dp"/>




        <TextView
            android:layout_width="match_parent"
            android:layout_marginTop="3dp"
            android:layout_height="wrap_content"
            android:background="@drawable/radius_all"
            android:textColor="#000"
            android:id="@+id/view_product_description"
            android:text=" \n \n
            - Enter your model number \n  \n
            - Compatible with all devices with a stereo minijack output \n \n
            - Outstanding bass performance \n \n
            - Touch volume and mute controls \n \n
            - Subwoofer volume control \n \n"
            android:textSize="11dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginTop="8dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:text="Product specification"
            android:textSize="14dp"/>


            <TextView
                android:layout_width="match_parent"
                android:layout_marginTop="3dp"
                android:layout_height="wrap_content"
                android:background="@drawable/radius_all"
                android:textColor="#000"
                android:id="@+id/view_product_specification"
                android:text=" \n \n
            - Enter your model number \n  \n
            - Compatible with all devices with a stereo minijack output \n \n
            - Outstanding bass performance \n \n
            - Touch volume and mute controls \n \n
            - Subwoofer volume control \n \n"
                android:textSize="11dp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:gravity="center"
            android:layout_height="match_parent">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="10dp"
            android:layout_marginRight="20dp"
            android:text="Other Products"
            android:textSize="15dp"
            android:textStyle="bold"
            android:layout_centerHorizontal="true"/>


            <android.support.v7.widget.RecyclerView
                android:layout_width="wrap_content"
                android:id="@+id/view_product_other_items"
                android:layout_height="wrap_content"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:layout_marginTop="3dp"
                android:layout_centerHorizontal="true">
            </android.support.v7.widget.RecyclerView>
        </LinearLayout>




        </LinearLayout>



    </RelativeLayout>
</ScrollView>

【问题讨论】:

  • 错误提示您的布局中没有视图 R.id.frams。您可以发布您在活动中使用的布局吗?
  • 我有,但它是空白的,因为我只用于捕获数据
  • 您发布了content_epp_drawer.xml 文件 - 这与您的活动有什么关系?
  • 那是我的导航抽屉内容
  • 您现在可以看到我的答案,先生,我是如何解决问题的 :)

标签: android android-fragments


【解决方案1】:

如果您使用android.support.v4.app.FragmentManager,则应使用getSupportFragmentManager(),如果您使用android.app.FragmentManager,则使用getFragmentManager()

 private void checkCarts() {
        checkCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                fragment=new EppViewCartV2();
                if (fragment != null) {
                    FragmentTransaction ft = getFragmentManager().beginTransaction();
                    ft.replace(R.id.frams, fragment);
                    ft.commit();
                }



            }
        });
    }

【讨论】:

  • 我尝试了这个,但我发现了一个错误。我将在我的问题中添加详细的错误:)
  • 如果这是问题所在,你会得到一个完全不同的编译时错误,而不是运行时错误,所以这与问题无关。
  • 在我的 appbar 活动中,带有 id (@+id/view_cart2) 的 Imageview 是我正在修复的按钮...
  • 您现在可以看到我的答案,先生,我是如何解决问题的 :)
【解决方案2】:

当您说ft.replace(R.id.frams, fragment); 时,您是在说“将fragment 放入R.id.frams 指定的容器中。

这意味着 ID 为 R.id.frams 的布局必须在您的布局中,在这种情况下 - 在您的活动布局中(因为您正在调用 getSupportFragmentManager() 并获取活动的 FragmentManager)。

您需要更改 Activity 的布局以包含该布局。

【讨论】:

  • 是的,也许我会创建将嵌入到我的应用程序中的片段,因为我没有调用我现有的片段......会尝试你的想法先生谢谢。
  • 我尝试重新创建示例片段布局位,这与我遇到的错误相同。也许有可能解决这个问题,因为我正在进行一项活动并试图从导航抽屉片段中访问特定页面或片段。
  • 您能否将活动的布局 xml 添加到您的问题中?
  • 好的,先生,这也是基本活动。让我补充一下先生:)
  • 那么您希望希望添加的片段出现在该布局的哪个位置?这就是重点:片段被添加到容器中。该容器需要已经存在于您的活动布局中。
【解决方案3】:

我想出了我的答案.. 我不使用片段事务,而是使用意图

首先,我打算将我的活动添加到我的导航抽屉并添加一个静态字符串数据(捕获数据)

我的活动:

 private void checkCarts() {
        checkCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

               Intent i = new Intent(getApplicationContext(),EppDrawer.class);
               openSisame="okay";
               startActivity(i);



            }
        });
    }

在我的抽屉里,我设置了一个try catch hat,如果可以的话,它将转到我喜欢显示的片段,如果不是,我将显示我的主页。 喜欢这个:

try{
        if(EppItemPreviewPhone.openSisame.equals("okay")){
            displaySelectedScreen(2131296453);

        }
    }catch (Exception e){
        Toast.makeText(this, "NONE", Toast.LENGTH_SHORT).show();
        fragment=new EppMainFragment();
        if (fragment != null) {
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.frams, fragment);
            ft.commit();
        }

    }

  private void displaySelectedScreen(int itemId) {


    //initializing the fragment object which is selected
    switch (itemId) {
        case R.id.nav_home:
            fragment = new EppMainFragment();
            break;

        case R.id.nav_my_account:
            fragment = new EppMyAccount();
            break;
        case R.id.nav_view_cart:
            fragment = new EppViewCartV2();
            break;

        case R.id.nav_order:
            fragment = new EppOrderHistory();
            break;
        case R.id.nav_find:
            fragment = new EppFindProduct();
            break;
        case R.id.nav_about:
            fragment = new AboutFragment();
            break;
    }

    //replacing the fragment
    if (fragment != null) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.frams, fragment);
        ft.commit();
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
}

因此:这个值 (2131296453) 是来自我的片段 id 的值。 谢谢你们的回答,即使它解决了我的代码,但你给了我一个解决它的逻辑:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-16
    • 2016-01-03
    • 1970-01-01
    • 2014-04-14
    相关资源
    最近更新 更多