简体   繁体   中英

Android onClick on Main to open fragment

I'm having problems using fragments. I want to open a fragment programatically on MainActivity when I press a button. With the code below the app runs but it don't opens the fragment:

MyFragment fragment = new MyFragment();
android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.fragment_container,fragment, "Hello");
transaction.commit();

What I'm doing wrong here? I've done a lot of research on stack overflow but I can't see a question that suits my needs.

EDIT

This is the layout where I want the fragment to appear:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:id="@+id/content1"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:layout_marginTop="96dp"
    tools:context="com.example.sdilab.pap.MainActivity">

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_weight="1"
        android:weightSum="2">

        <android.support.v7.widget.CardView
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:id="@id/cardView"
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:layout_weight="1"

            card_view:cardUseCompatPadding="true">


            // Other layouts and views here

            <Space
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageView1"
                android:layout_gravity="center"
                android:layout_marginBottom="16dp"
                android:src="@drawable/exercicio"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:id="@+id/exerciciotext"
                android:textAlignment="center"
                android:gravity="bottom"
                android:layout_marginBottom="36dp"
                android:text="Registar\nexercício"
                android:textSize="@dimen/activity_horizontal_margin"
                />
            <Space
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
        </android.support.v7.widget.CardView>
        <android.support.v7.widget.CardView
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:id="@id/cardView"
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:layout_weight="1"

            card_view:cardUseCompatPadding="true">


            // Other layouts and views here

            <Space
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageView2"
                android:layout_gravity="center"
                android:layout_marginBottom="16dp"
                android:src="@drawable/food"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:textAlignment="center"
                android:gravity="bottom"
                android:id="@+id/foodtext"
                android:layout_marginBottom="36dp"
                android:text="Registar\nrefeição"
                android:textSize="@dimen/activity_horizontal_margin"
                />
            <Space
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
        </android.support.v7.widget.CardView>


    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_weight="1"
        android:weightSum="2">

        <android.support.v7.widget.CardView
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:id="@+id/cardViewCalendar"
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:elevation="0dp"
            card_view:cardUseCompatPadding="true">


            // Other layouts and views here

            <Space
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageView3"
                android:layout_gravity="center"
                android:layout_marginBottom="16dp"
                android:src="@drawable/calendar"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:textAlignment="center"
                android:id="@+id/calendartext"
                android:gravity="bottom"
                android:layout_marginBottom="36dp"
                android:text="Agendar\ntreinos"
                android:textSize="@dimen/activity_horizontal_margin"
                />
            <Space
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
        </android.support.v7.widget.CardView>
        <android.support.v7.widget.CardView
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:id="@id/cardView"
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:layout_weight="1"

            card_view:cardUseCompatPadding="true">


            // Other layouts and views here

            <Space
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageView4"
                android:layout_gravity="center"
                android:layout_marginBottom="16dp"
                android:src="@drawable/settings"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:textAlignment="center"
                android:id="@+id/settingstext"
                android:gravity="bottom"
                android:layout_marginBottom="36dp"
                android:text="Alterar\ndefinições"
                android:textSize="@dimen/activity_horizontal_margin"
                />
            <Space
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
        </android.support.v7.widget.CardView>
    </LinearLayout>

</LinearLayout>

Replace

transaction.add(R.id.fragment_container,fragment, "Hello");

With

transaction.add(R.id.fragment_container,fragment);

Try with this

 FragmentManager mFragmentManager = getSupportFragmentManager();
            FragmentTransaction mFragmentTransaction =  mFragmentManager.beginTransaction();
            Fragment fragment = new MyFragment();
            mFragmentTransaction.add(R.id.fragment_container, fragment);
            mFragmentTransaction.commit();

Try to do a explicit call of show for your fragment after adding it. Something like this:

FragmentManager mFragmentManager = getSupportFragmentManager();
FragmentTransaction mFragmentTransaction =  mFragmentManager.beginTransaction();
Fragment fragment = new MyFragment();
mFragmentTransaction.add(R.id.fragment_container, fragment);
mFragmentTransaction.show(fragment);
mFragmentTransaction.commit();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM