-1

On two different occurrences I am passing variables from MainActivity to HomeFragment like this:

First I call this method in MainActivity:

buttonCreator(d, Name);

Then I send the variables "d" and "Name" to HomeFragment like this:

public void buttonCreator(Drawable d,String a) {
    int fragsize = getSupportFragmentManager().getFragments().size();
    Log.d("tag_name", "Size of Home Fragment" + fragsize);
    HomeFragment homeFragment = (HomeFragment)getSupportFragmentManager().getFragments().get(1);
    homeFragment.createButton(d, a);
}

The first time I call buttonCreator(d,Name) I don't get any errors. The second time I try passing the variables to HomeFragment through buttonCreator, I get the error "IndexOutOfBoundsException: Invalid index 1, size is 1". I think this has something to do with the array size of HomeFragment, but I don't fully understand the connection between fragments and arrays here.

I know that the first time I try to access HomeFragment I call:

int fragsize = getSupportFragmentManager().getFragments().size();

and I get an output of "3", and the second time it is called (when it crashes), I get an output of "1". Can someone please explain why this is happening and how I might fix the problem?

Here is my full MainActivity if that is helpful. The first time I call buttonCreator is in "RepeaterTask" and the second time (when it crashes) is through onCreate(), through the "else" statement.

public class MainActivity extends FragmentActivity implements HomeFragment.ButtonCreator {


private Thread repeatTaskThread;
private byte[] byteArray;
private static Context mContext;
private Cursor c;

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

   mContext = getApplicationContext();

   DatabaseStructure database = new DatabaseStructure(mContext);
   database.checkDatabase();


   // if database is empty (checkDatabase == false) Open empty HomeFragment
   if (!database.checkDatabase()) {

       Log.d("tag_name", "Database is Empty: Open HomeFragment");

       FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
       ft.replace(R.id.container, new HomeFragment(),"Home_Fragment")
               .addToBackStack("Home_Fragment").commit();
   }

   else {

       DatabaseStructure appDatabase = new DatabaseStructure(this);
       c = appDatabase.getApps();
       c.moveToFirst();

       String Name = c.getString(0);
       Log.d("tag_name", "String from Database" + appName);
       byte[] image = c.getBlob(1);
       Log.d("tag_name", "Image from Database" + image);


       Bitmap bitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
       Drawable d = new BitmapDrawable(Resources.getSystem(), bitmap);
       Log.d("tag_name", "Drawable from Database" + d);

       buttonCreator(d, Name);
   }

   RepeatTask();

}


public void buttonCreator(Drawable d,String a) {
    int fragsize = getSupportFragmentManager().getFragments().size();
    Log.d("tag_name", "Size of Home Fragment" + fragsize);
    HomeFragment homeFragment = (HomeFragment)getSupportFragmentManager().getFragments().get(1);
    homeFragment.createButton(d, a);
}



public void RepeatTask() {

    repeatTaskThread = new Thread() {
        public void run() {

            while (true) {

                try {

                    Socket socket = new Socket("192.168.0.26", 5050);

                    // Get data sent through socket
                    DataInputStream DIS = new DataInputStream(socket.getInputStream());

                    System.out.println("DataInputStream Started");
                    final String Name = DIS.readUTF();
                    int len = DIS.readInt();
                    final byte[] data = new byte[len];

                    DIS.readFully(data, 0, data.length);

                    Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

                    final Drawable d = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap, 90, 90, true));


                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            buttonCreator(d, Name);

                            DatabaseStructure database = new DatabaseStructure(mContext);
                            database.addEntry(Name, data);

                        }
                    });


                    socket.close();

                } catch (Exception e) {

                    System.out.println("Exception is " + e.toString());
                }

                try {
                    // Sleep for 5 seconds
                    Thread.sleep(5000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        ;
    };
    repeatTaskThread.start();
}

}
Natalie
  • 315
  • 5
  • 18
  • When you want to get hold of another fragment, you should generally not try to find it in a list. Find if by id (http://stackoverflow.com/a/8545206/995891 ) or by tag (http://stackoverflow.com/questions/16918854/find-fragment-by-tag-name-in-container ) in case you've set one. – zapl May 18 '16 at 18:16
  • @zapl When I do this: "HomeFragment hm = (HomeFragment)getSupportFragmentManager().findFragmentByTag("Home_Fragment"); Log.d("tag_name", "HomeFragment" + hm); hm.createButton(d,appName);" I get hm = null :( trying to fix that. – Natalie May 18 '16 at 19:05

1 Answers1

3

Arrays and Lists have zero based indexes (thr first index is 0 not 1) so try

HomeFragment homeFragment = 
      (HomeFragment)getSupportFragmentManager().getFragments().get(0);
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64