0

Solved! Thanks everyone for your help.

I am struggling with Fragments as a concept, and especially seem stuck on this one thing. What I'm trying to do is, using my fragment, manipulate its own layout's data. Every time I try to access an ImageButton from within the Fragment, it crashes the application. It works fine from the activity. Am I just misunderstanding Fragments fundamentally?

Code(cut down for size)-

This is the beginning of the activity my fragment is called from:

Display.java

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

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        MyFragmentClass MyFragment = new MyFragmentClass();

        fragmentTransaction.add(R.id.fragment_container, MyFragment);
        fragmentTransaction.commit();

The XML for that Activity:

activity_display.xml

<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"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                tools:context="com.mycompanyname.myprojectname.Display"
                android:id="@+id/display_layout">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fragment_container">
    </FrameLayout>

</RelativeLayout>

The Fragment

MyFragmentClass.java
public class MyFragmentClass extends Fragment
{
 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_layout_screen, container, false);;

    }

    public void MethodTest()
    {

    }

}

The Fragment's XML file:

fragment_layout_screen.xml

<?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:layout_height="match_parent"
    android:id="@+id/fragment_layout_screen"
    tools:context="com.mycompanyname.myprojectname.MyFragmentClass">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/my_button"
        android:clickable="true"
        android:onClick="buttonPress"
        android:src="@drawable/button"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"/>

</RelativeLayout>

Basically I had all of this originally in Display.java, but wanted to add fragments, so I'm trying to move stuff out and into fragments, but I still need the ability to manipulate the xml info, I just can't.

From inside the Display.java activity I can easily call the ImageButton like this:

ImageButton myButton = (ImageButton) findViewById(R.id.my_button)

but if I do the same from MethodTest in the fragment, the app crashes.

I've searched many suggestions on here, trying various solutions from here: findViewById in Fragment but none of those seemed to work.

I've been reading through this for the setup: http://developer.android.com/guide/components/fragments.html and can't seem to find what I'm doing wrong.

Any help would be appreciated, and if you have any questions about my setup, please ask.

Community
  • 1
  • 1
jagrakye
  • 357
  • 1
  • 2
  • 11

2 Answers2

0

declare on fragment private View rootView; On the onCreateView(...) set rootView = inflater.Inflate(..); and then access the imagebutton as ImageButton mButton =(ImageButton) rootView.findViewById(..);

Filipe Silva
  • 220
  • 4
  • 10
  • I tried this, it crashes if I call methodTest() from anywhere outside of OnCreateView. Is this the only way for it to work? Can I not call fragment methods outside of its own OnCreateView? – jagrakye Mar 28 '15 at 16:20
  • Can you provide the fragment code? The problem is most likely that the method is being called before `OnCreateView` and you use something inside the `methodTest()` that requires the layout to be ready. – Filipe Silva Mar 28 '15 at 16:52
  • `public class MyFragmentClass extends Fragment { private View view; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view = inflater.inflate(R.layout.fragment_layout_screen, container, false); return view; } public void MethodTest() { ImageButton mButton = (ImageButton) view.findViewById(mybutton); } }` – jagrakye Mar 28 '15 at 17:06
  • Just edited with the update of how I'm actually doing it. That was just a general version, hit enter too fast – jagrakye Mar 28 '15 at 17:08
  • Wherever `MethodTest()` is being called from the fragment has not yet initialized its layout. Its either that or `view.findViewById(mybutton);` returns null. Should that not be `view.findViewById(R.id.mybutton);` tho? – Filipe Silva Mar 28 '15 at 17:10
  • Oh yeah. It is in the code. Sorry I'm sort of transposing back and forth. I just tried it again. Perhaps an additional detail which would help is where I'm calling it? I'm calling it from a method in Display.java. Basically created a new MyFragmentClass and then calling that method. Perhaps this is the wrong way to do this. – jagrakye Mar 28 '15 at 17:12
  • ...and I feel dumb, I think you've solved the issue for me. Thanks so much! I feel kind of silly, but I just realized I was created a new fragment instead of using the existing one I had already called. It's always the smallest things. Thanks for your time and patience! – jagrakye Mar 28 '15 at 17:17
0
Do like this in fragment

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

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


        GridView gridview = (GridView) view.findViewById(R.id.grid);

return view ;
Hulk
  • 237
  • 1
  • 13
  • So I guess I don't fully understand where you're suggesting the change be made. So are you suggested I change my layout from a RelativeLayout to a GridView? Or is GridView being used as a stand in here for whatever layout I might have? If I swap GridView with my relative layout, I still can't call that from MethodTest(). If I create it outside of onCreateView, I can call it from MethodTest(), but it still crashes every time. The only way I've gotten it to work, is if I create it outside the fragment and then call MethodTest() from *within* OnCreateView. – jagrakye Mar 28 '15 at 16:16