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