Android onClick on Main打开片段

时间:2016-03-24 09:49:30

标签: android android-fragments

我在使用片段时遇到了问题。当我按下按钮时,我想以编程方式在MainActivity上打开一个片段。使用下面的代码运行应用程序,但它不会打开片段:

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();

我在这里做错了什么?我已经对堆栈溢出进行了大量研究,但我无法看到符合我需求的问题。

  

修改

这是我希望片段出现的布局:

<?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>

3 个答案:

答案 0 :(得分:1)

替换

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

使用

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

答案 1 :(得分:0)

试试这个

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

答案 2 :(得分:0)

添加后,尝试为您的片段显式调用FragmentManager mFragmentManager = getSupportFragmentManager(); FragmentTransaction mFragmentTransaction = mFragmentManager.beginTransaction(); Fragment fragment = new MyFragment(); mFragmentTransaction.add(R.id.fragment_container, fragment); mFragmentTransaction.show(fragment); mFragmentTransaction.commit(); 。像这样:

channel.basicPublish("", requestQueueName, props, message.getBytes());

while (true) {
    QueueingConsumer.Delivery delivery = consumer.nextDelivery();
    if (delivery.getProperties().getCorrelationId().equals(corrId)) {
        response = new String(delivery.getBody());
        break;
    }
}