user2316667
user2316667

Reputation: 5634

How Do I Retrieve An Instance of A Fragment Defined in XML?

In short, the problem is that I can find my element by id but it keeps asking me to cast it to a view and I cannot cast it to a type of my object name. I'm not sure if casting to view is how it is suppose to be but the Android sample simply creates the fragment and adds it therefore it is not defined in xml. But the point is that in the sample, the object is not of type view.

More information: This is probably a fast and easy fix. I have a fragment class that holds a custom view I created out of code (it is a game of sorts with constant drawing in it). I added the fragment in the top left portion of my xml layout. Under the fragment are buttons (defined in the xml layout). I have a main activity and I want a handle to the instance of the fragment that contains my custom view so that when buttons are clicked, some variables in the fragment are changed and the view is redrawn.

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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <fragment android:name="com.example.dcubebluetooth"
              android:id="@+id/lEDView1"
              android:layout_width="400px"
              android:layout_height="400px" />

<Button
        android:id="@+id/layer1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/lEDView1"
        android:layout_marginTop="50px"
        android:text="Layer1"
        android:textSize="12dp" />

    <Button
        android:id="@+id/layer2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/layer1"
        android:layout_toRightOf="@id/layer1"
        android:text="Layer2"
        android:textSize="12dp" />

    <Button
        android:id="@+id/layer3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/layer2"
        android:layout_toRightOf="@id/layer2"
        android:text="Layer3"
        android:textSize="12dp" />

    <Button
        android:id="@+id/layer4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/layer3"
        android:layout_toRightOf="@id/layer3"
        android:text="Layer4"
        android:textSize="12dp" />

</RelativeLayout>

Main Activity:

public class MainActivity extends Activity implements LEDGridFragment.TriggerBlueTooth{

    Button l1;
    Button l2;
    Button l3;
    Button l4;
    BluetoothService bt;
    LEDGridFragment gridUI;

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

@Override
public void onledSelected(int layer, int led) {
    // TODO Auto-generated method stub
    String msg = Integer.toString(layer);
    if(led<10) msg+=0;
    msg+= Integer.toString(led);
    bt.write(msg);
}

//Full method below not showing. I don't know why

    public void initialize(){
gridUI = findViewById(R.id.lEDView1);

bt = new BluetoothService();

l1 = (Button) findViewById(R.id.layer1);
l2 = (Button) findViewById(R.id.layer2);
l3 = (Button) findViewById(R.id.layer3);
l4 = (Button) findViewById(R.id.layer4);

l1.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        gridUI.currentLayer = 0;
        gridUI.update();
    }
});

l2.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        gridUI.currentLayer = 1;
        gridUI.update();
    }
});
l3.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        gridUI.currentLayer = 2;
        gridUI.update();
    }
});

l4.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        gridUI.currentLayer = 3;
        gridUI.update();
    }
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}


}

Fragment:

public class LEDGridFragment extends Fragment{

    LEDView view;
    TriggerBlueTooth mCallBack;

    boolean[][][] states = new boolean[4][4][4];

    int currentLayer = 0;

    public interface TriggerBlueTooth{
        public void onledSelected(int layer, int led);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        mCallBack = (TriggerBlueTooth) activity;

        view.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int x = (int) (event.getX())/100;
                int y = (int) (event.getY())/100;
                int led = y+(x*4);

                mCallBack.onledSelected(currentLayer, led); //Notify Main Activity of the led that was toggled
                                                            //So it can send data over Bluetooth
                return false;
            }
        });
    }

    public void update(){
        view.drawGrid(states, currentLayer); //Update the view
    }

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

        for(int i=0; i<states.length; i++){  //Initialize the board off which the view will be drawn
            for(int j=0; j<states[0].length; j++){
                for(int k=0; k<states[0][0].length; k++){
                    states[i][j][k] = false;
                }
            }
        }

        view = new LEDView(getActivity());
        return view;
        //return super.onCreateView(inflater, container, savedInstanceState); //Auto-formed. Commented out
    }

}

On the side (okay if you don't answer this): The graphical layout does not show what it is suppose to in terms of the initial drawing of the game. If I add the view itself, it does. But right now, it is a grey box with " Pick preview layout from 'Fragment Layout' context menu" Where is this menu? I want to see if my view is working. I can't personally test it because 1. It does not compile, and 2. I haven't an android phone

Upvotes: 3

Views: 2569

Answers (1)

Kevin Coppock
Kevin Coppock

Reputation: 134684

This should do it:

FragmentManager mgr = getFragmentManager();
Fragment fragment = mgr.findFragmentById(R.id.lEDView1);

Upvotes: 17

Related Questions