2

This question has been asked before, however, I have not found an appropriate answer for my problem. I am building a simple fragment activity that displays three buttons at the top of the screen in one container that when pressed update the fragment in the rest of the screen. Just like a tab host. I am using android.support.v4.app.Fragment in all code, but still cannot get the transaction manager to display the fragment in the container. I've been banging my head on the keyboard for hours and connot find the problem.

Here is the main activity extending FragmentActivity:

package com.example.testudpfragment;

import com.example.testudp.R;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

public class TestUDPFragmentActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_udpfragment);

        if(savedInstanceState == null)  {
            getSupportFragmentManager().beginTransaction()
                .add(R.id.menu_container, new MenuFragment()).commit();

        }
    }
}

Here is the MenuFragment class extending fragment for my tabs:

package com.example.testudpfragment;

import com.example.testudp.R;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.support.v4.app.*;

public class MenuFragment extends Fragment{

    Fragment frag;
    FragmentTransaction fragTransaction;

    public MenuFragment()   {

    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)   {

        View view = inflater.inflate(R.layout.frament_menu, container, false);

        frag = new StringsFragment();
        fragTransaction = getFragmentManager().beginTransaction().add(R.id.container, frag);
        System.out.println(fragTransaction.toString());
        fragTransaction.commit();

        Button btnString = (Button) view.findViewById(R.id.button_string);
        Button btnSlider = (Button) view.findViewById(R.id.button_slider);
        Button btnTouch = (Button) view.findViewById(R.id.button_touch);



        btnString.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                frag = new StringsFragment();
                fragTransaction = getFragmentManager().beginTransaction().replace(R.id.container, frag);

                fragTransaction.commit();

            }
        });



        btnSlider.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                frag = new SlidersFragment();
                fragTransaction = getFragmentManager().beginTransaction().replace(R.id.container, frag);
                fragTransaction.commit();

            }
        });



        btnTouch.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                frag = new TouchFragment();
                fragTransaction = getFragmentManager().beginTransaction().replace(R.id.container, frag);
                fragTransaction.commit();

            }
        });
        return view;
    }
}

Here is one of the three fragments that I can't get to display:

package com.example.testudpfragment;

import com.example.testudp.R;

import android.support.v4.app.*;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class StringsFragment extends Fragment   {

    public StringsFragment()    {

    }

    public View onCreatView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)  {

        View rootView = inflater.inflate(R.layout.fragment_strings, null);

        return rootView;
    }
}

activity_test_udpfragment:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<FrameLayout
    android:id="@+id/menu_container"
    android:background="#eee"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

</FrameLayout>

<FrameLayout
    android:id="@+id/container"
    android:background="#aaa"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</FrameLayout>

</LinearLayout>

Here is frament_menu:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
style="android.att/buttonBarStyle" >``

<Button
    android:id="@+id/button_string"
    style="android.att/buttonBarButtonStyle"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="String" />

<Button
    android:id="@+id/button_slider"
    style="android.att/buttonBarButtonStyle"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Slider" />

<Button
    android:id="@+id/button_touch"
    style="android.att/buttonBarButtonStyle"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Touch" />

</LinearLayout>

And here is fragment_strings:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">


<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:scaleType = "centerCrop"
    android:src="@drawable/me_hooded" />

</RelativeLayout>

Any help is greatly appreciated!

EDIT: Here is new TestUDPActivityFragment with buttons to change fragment:

package com.example.testudpfragment;

import com.example.testudp.R;

import android.support.v4.app.*;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.os.Bundle;

public class TestUDPFragmentActivity extends FragmentActivity {
Fragment frag;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test_udpfragment);

    Button btnString = (Button)findViewById(R.id.btn_string);
    Button btnSlider = (Button)findViewById(R.id.btn_slider);
    Button btnTouch = (Button)findViewById(R.id.btn_touch);


    if(savedInstanceState == null)  {
        getSupportFragmentManager().beginTransaction().add(R.id.container, new StringsFragment()).commit();

    btnString.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            frag = new StringsFragment();
            getSupportFragmentManager().beginTransaction().replace(R.id.container, frag).commit();
            }
    }); 

    btnSlider.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            frag = new SlidersFragment();
            getSupportFragmentManager().beginTransaction().replace(R.id.container, frag).commit();
            }
    }); 

    btnTouch.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            frag = new TouchFragment();
            getSupportFragmentManager().beginTransaction().replace(R.id.container, frag).commit();
            }
    }); 



    }
}

}

Jonnie Blout
  • 21
  • 1
  • 3
  • Are the fragments really not loading or is there something among your layouts that's hiding them from view? I'm curious to see your activity_test_udpfragment.xml, menu_container.xml, frament_menu.xml, and container.xml. – mjp66 Jan 18 '15 at 08:24
  • It's weird because the menu_container will display the buttons/tabs, but the container won't display the fragment_string. – Jonnie Blout Jan 18 '15 at 08:47
  • I don't think the fragments are loading because when I try to print a FragmentTransaction.toString() after instantiating it in MenuFragment nothing happens. – Jonnie Blout Jan 18 '15 at 08:49
  • I'm thinking your best bet would be to put your menu buttons inside your activity and have the fragments generated from there. Squonk's answer below pretty much explains the probable issue. – mjp66 Jan 18 '15 at 11:16

1 Answers1

1

I can see a couple of problems...

First, your FrameLayout containers are defined in your activity_test_udpfragment.xml layout file which is inflated by your TestUDPFragmentActivity. The problem with attempting to create / add your StringsFragment in your MenuFragment code using the following...

fragTransaction = getFragmentManager().beginTransaction().add(R.id.container, frag);

...is the MenuFragment inflates a layout from your frament_menu.xml which doesn't contain a FrameLayout with id of R.id.container.

That's your first problem but the second is with attempting to use a Fragment to create / add a Fragment in the first place. Your MenuFragment is not able to use the Framelayout in the TestUDPFragmentActivity.

In short, I suspect you want both Fragments to appear in your Activity layout (and not have the StringsFragment as a 'child' of MenuFragment) in which case you need to put all code for creating / adding both Fragments into your Activity.

Squonk
  • 48,735
  • 19
  • 103
  • 135
  • To simplify it I deleted the MenuFragment class and just put buttons in the TestUDPFragmentActivity that then call the fragment manager for the other fragments to be displayed on the onclick. However, they are still not displaying. I have edited my original post to show the new TestUDPAcitvityFragment. – Jonnie Blout Jan 19 '15 at 06:24
  • @JonnieBlout : OK, now one thing concerns me. You're defining `Fragment frag;` but in your `OnClickListener(...)` code you do things like this... `frag = new StringsFragment();`. Are you sure you're not getting a `ClassCastException` when creating your `Fragments` (check logcat)? What happens if you change your code to explicitly cast the different classes, e.g., `frag = (Fragment) new StringsFragment();`? – Squonk Jan 19 '15 at 08:36
  • Well, what is strange to me is that the initial StringsFragment inflation before the first onClick isn't displaying either. I did not see any cast exceptions; I thought that since StringsFragment is a subclass of Fragment that I could just instantiate it normally as a Fragment. I'm definitely not an expert though, ha. However, I did try to explicitly cast my StringsFragment and others in the onClicks. Because the first fragment outside of the onClicks doesn't work, I believe that the problem lies elsewhere. I still haven't been able to figure it out. Thanks for the responses! – Jonnie Blout Jan 19 '15 at 16:23