调用getSupportFragmentManager()。getFragments()。get(1)给出错误索引1无效,大小为1

时间:2016-05-18 00:43:28

标签: java android database image android-fragments

在两个不同的情况下,我将变量从MainActivity传递到HomeFragment,如下所示:

首先我在MainActivity中调用此方法:

buttonCreator(d, Name);

然后我将变量“d”和“Name”发送到HomeFragment,如下所示:

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

我第一次调用buttonCreator(d,Name)时,我没有收到任何错误。 第二次尝试通过buttonCreator将变量传递给HomeFragment时,我收到错误“IndexOutOfBoundsException:索引1无效,大小为1”。我认为这与HomeFragment的数组大小有关,但我不完全理解片段和数组之间的联系。

我知道第一次尝试访问HomeFragment时我打电话:

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

我输出“3”,第二次调用(崩溃时),输出为“1”。 有人可以解释为什么会发生这种情况以及如何解决问题吗?

如果有帮助,这是我的完整MainActivity。我第一次调用buttonCreator是在“RepeaterTask”中,第二次(崩溃时)是通过onCreate(),通过“else”语句。

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

}

1 个答案:

答案 0 :(得分:3)

数组和列表具有零基索引(thr第一个索引为0而不是1)所以请尝试

angular.forEach($rootScope.menus, function(menu){
   menu = false;
   console.log(menu); // outputs false
})
console.log($rootScope.menus); //values are still equal to true