Search code examples
androidcrashrequestandroid-gallery

Behavior’s Gallery crash when it invokes requestFocus


Depending on the position of the gallery, one or another edittext is to receive focus. Althought I could do it, behavior's gallery crashs: When Gallery run a position and set focus on edittext, Gallery's velocity crashes. It seems as if Gallery was too slow to get the request on time .

I've not found any significant information about it.

galleryView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(
            @SuppressWarnings("rawtypes") AdapterView adapterView,
            View view, int pos, long l) {

        if (pos == 0) {
            pie_foto_1.post(new Runnable() {
                public void run() {
                    pie_foto_1.requestFocus();
                }
            });
        }

        if (pos == 1) {
            pie_foto_2.post(new Runnable() {
                public void run() {
                    pie_foto_2.requestFocus();
                }
            });
        }

        if (pos == 2) {

            pie_foto_3.post(new Runnable() {
                public void run() {
                    pie_foto_3.requestFocus();
                }
            });

            pie_foto_1.setVisibility(View.INVISIBLE);
            pie_foto_2.setVisibility(View.INVISIBLE);
            pie_foto_3.setVisibility(View.VISIBLE);

        }
    }

    public void onNothingSelected(
            @SuppressWarnings("rawtypes") AdapterView adapterView
    ) {
    }
});

Solution

  • Gallery needs more time to get its new position. So I put a delay before Gallery changes position in order to get a nice transition.

                                   else if (pos == 1)
                                    {
                                        Runnable mMyRunnable = new Runnable() {
                                            public void run() {
                                                pie_foto_1.post(new Runnable() {
                                                    public void run() {
                                                        pie_foto_2.requestFocus();
                                                        // headline.clearFocus();
    
                                                    }
                                                });
                                            }
                                        };
    
                                        Handler myHandler = new Handler();
                                        myHandler.postDelayed(mMyRunnable, 100);
                                    }