方向改变后的奇怪行为

时间:2012-06-14 12:53:29

标签: android android-viewpager android-orientation

我希望有人可以提供帮助。

我有点难以解释活动,它是一个经典的视图寻呼机,但在操作栏中有一个按钮,使视图向右移动并显示菜单(原始隐藏在屏幕外)。这是一个类似Facebook的菜单。我有2个布局(菜单和寻呼机),我使用自定义ScrollView来获取我寻找的行为。

当我改变方向时,我希望菜单每次都出来。所以我创建了一个不同的布局,并测试了onCreate方法的方向。

我有几次尝试: 在清单中没有configChange的情况下,应用程序崩溃时出现“IllegalStateException:Activity已被破坏”,指向“mPager.setAdapter(mAdapter);”这一行。

配置更改时,应用程序在旋转时会执行废话,它不会将布局更改为布局中的布局...

配置更改和“onConfigurationChanged”上onCreate的内容从纵向到横向是好的,但反向出错(视图很大,因为它应该很高,而且应该很大)

我希望你们中的一个人不会感到困惑。

如果你愿意,我可以发布代码,但是有很多代码,我不知道哪个特定部分可以帮助你(只是问我)。

谢谢!

when i pass from portrait to landscape with the 2de try

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

    inflater = LayoutInflater.from(this);
    me = this;

    View v = findViewById(R.id.landscape);
    if (v == null) {
        isLand = false;

        scrollView = (MyHorizontalScrollView) inflater.inflate(
                R.layout.horz_scroll_layout, null);

        setContentView(scrollView);

        menu = inflater.inflate(R.layout.main_pager_menu_layout, null);
        app = inflater.inflate(R.layout.main_pager_layout, null);

        final View[] children = new View[] { menu, app };
        int scrollToViewIdx = 1;
        TextView tv = (TextView) app.findViewById(R.id.sizerTV);
        scrollView.initViews(children, scrollToViewIdx,
                new SizeCallbackForMenu(tv));

    } else {
        isLand = true;
        menu = findViewById(R.id.main_pager_land_menu);
        app = findViewById(R.id.main_pager_land_app);
        // scrollView = new MyHorizontalScrollView(null);
    }
    // Menu INIT
    new MenuBehaviour(me, menu, inflater);
    // AppBarInit
    initializeAppBar();

    // DAO
    controlDAO = new Controller(me);
    new Thread(new Runnable() {
        @Override
        public void run() {
            arrayEvent = controlDAO.ebdd.getAllEvent();
            if (arrayEvent != null) {
                runOnUiThread(onUiThread);
            } else {
                runOnUiThread(new Runnable() {
                    public void run() {
                        Toast.makeText(
                                me,
                                me.getResources().getString(
                                        R.string.error_no_database_yet),
                                Toast.LENGTH_LONG);
                    }
                });

            }
        }
    }).start();

    // WS
    final AQuery aq = new AQuery(me);
    aq.ajax(uriTest, JSONObject.class, new AjaxCallback<JSONObject>() {

        @Override
        public void callback(String url, JSONObject json, AjaxStatus status) {
            if (json != null) {
                // Toast.makeText(aq.getContext(), status.getCode() + ":" +
                // json.toString(), Toast.LENGTH_LONG).show();
                try {
                    JSONArray array = json.getJSONObject("events")
                            .getJSONArray("event");

                    controlDAO.ebdd.open();
                    // On met le nouveau flux dans la BDD
                    controlDAO.updateBDD(array);
                    // On récupere les event de la BDD (au cas ou on gererai
                    // les maj)
                    arrayEvent = controlDAO.ebdd.getAllEvent();
                    controlDAO.ebdd.close();
                    runOnUiThread(onUiThread);

                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } else {
                Toast.makeText(
                        aq.getContext(),
                        "Error:"
                                + status.getCode()
                                + ", connectez vous pour acceder aux mis a jours",
                        Toast.LENGTH_LONG).show();
            }
        }
    });

}

Runnable onUiThread = new Runnable() {

    @Override
    public void run() {
        arrtype = new ArrayList<String>();

        arrtype.add("tout les evenements");
        arrtype.add("sport");
        arrtype.add("concert");
        arrtype.add("spectacle");
        arrtype.add("visite");
        controlDAO.ebdd.close();
        // On sépare les évenements en fonction de leurs type et on rempli
        // arrayOfEventByType
        arrayOfEventByType = new ArrayList<ArrayList<Event>>();
        // ajout d'une arraylist pour tout les evenements
        arrayOfEventByType.add(new ArrayList<Event>()); // tout les
                                                        // evenements
        arrayOfEventByType.add(new ArrayList<Event>()); // sport
        arrayOfEventByType.add(new ArrayList<Event>()); // concert
        arrayOfEventByType.add(new ArrayList<Event>()); // spectacle
        arrayOfEventByType.add(new ArrayList<Event>()); // visite
        for (int i = 0; i < arrayEvent.size(); i++) {
            if (!arrtype.contains(arrayEvent.get(i).type.toLowerCase())) {
                arrayOfEventByType.add(new ArrayList<Event>());
                arrtype.add(arrayEvent.get(i).type.toLowerCase());
            }

            Log.i("renaud", "arrtype.size() = " + arrtype.size());
            Log.i("renaud", "arrayOfEventByType.size() = "
                    + arrayOfEventByType.size());

            arrayOfEventByType.get(
                    arrtype.indexOf(arrayEvent.get(i).type.toLowerCase()))
                    .add(arrayEvent.get(i));
            arrayOfEventByType.get(0).add(arrayEvent.get(i));
        }
        // Test
        Log.i("renaud",
                "arrayOfEventByType.size() : " + arrayOfEventByType.size());
        for (int i = 0; i < arrayOfEventByType.size(); i++) {
            Log.i("renaud", "arrayOfEventByType.get(" + i + ").length() : "
                    + arrayOfEventByType.get(i).size());
        }

        for (int i = 0; i < arrayOfEventByType.size(); i++) {
            if (arrayOfEventByType.get(i).isEmpty()) {
                arrayOfEventByType.remove(i);
                arrtype.remove(i);
                i--;
            }
        }
        // ArrayList<Event> tous = arrayOfEventByType.remove(0);
        // Collections.sort(arrayOfEventByType, new EventSort());
        // arrayOfEventByType.add(0, tous);
        // // On crée un nombre de panneau en fonction des types

        mAdapter = new MyAdapter(getSupportFragmentManager(),
                arrayOfEventByType, arrtype);
        mPager = (ViewPager) findViewById(R.id.pager);
        try {
            mPager.setAdapter(mAdapter);

            // +pour rajouter un titre etc..
            mPager.setOnPageChangeListener(new OnPageChangeListener() {
                @Override
                public void onPageSelected(int arg0) {

                    // Omniture
                    if (arg0 == 0) {
                        StadeDeFranceActivity.s.pageName = "Main Pager all event";
                    } else if (!arrayOfEventByType.isEmpty()
                            && arrayOfEventByType.get(arg0) != null
                            && !arrayOfEventByType.get(arg0).isEmpty()
                            && arrayOfEventByType.get(arg0).get(0) != null
                            && arrayOfEventByType.get(arg0).get(0).type != null) {

                        StadeDeFranceActivity.s.pageName = "Main Pager number "
                                + arg0
                                + " category:"
                                + arrayOfEventByType.get(arg0).get(0).type;
                    } else {
                        StadeDeFranceActivity.s.pageName = "Main Pager number "
                                + arg0;
                    }
                    // StadeDeFranceActivity.s.track();

                    Log.i("renaud", "page " + arg0);

                };

                @Override
                public void onPageScrolled(int arg0, float arg1, int arg2) {
                    // TODO Auto-generated method stub
                }

                @Override
                public void onPageScrollStateChanged(int arg0) {
                    // TODO Auto-generated method stub
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
};

public static class MyAdapter extends FragmentPagerAdapter {

    private ArrayList<ArrayList<Event>> arrayOfEventByType;
    private ArrayList<String> arrayOfname;

    public MyAdapter(FragmentManager fm,
            ArrayList<ArrayList<Event>> arrayOfEventByType,
            ArrayList<String> arrayOfname) {
        super(fm);
        this.arrayOfEventByType = arrayOfEventByType;
        this.arrayOfname = arrayOfname;
    }

    @Override
    public int getCount() {
        return arrayOfEventByType.size();
    }

    @Override
    public Fragment getItem(int position) {

        return MainPagerFragment.newInstance(
                arrayOfEventByType.get(position), arrtype.get(position));

    }

    // private ArrayList<Event> concatArray(
    // ArrayList<ArrayList<Event>> arrayOfEventByType2) {
    // ArrayList<Event> concatArray = new ArrayList<Event>();
    //
    // for (int i = 0; i < arrayOfEventByType2.size(); i++) {
    // concatArray.addAll(arrayOfEventByType2.get(i));
    // }
    //
    // return concatArray;
    // }

}

static class SizeCallbackForMenu implements SizeCallback {
    int btnWidth;
    View btnSlide;

    public SizeCallbackForMenu(View btnSlide) {
        super();
        this.btnSlide = btnSlide;
    }

    @Override
    public void onGlobalLayout() {
        btnWidth = btnSlide.getMeasuredWidth();
        System.out.println("btnWidth=" + btnWidth);
    }

    @Override
    public void getViewSize(int idx, int w, int h, int[] dims) {
        dims[0] = w;
        dims[1] = h;
        final int menuIdx = 0;
        if (idx == menuIdx) {
            dims[0] = w - btnWidth;
        }
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    return true;
}

@Override
public boolean onPrepareOptionsMenu(Menu menu2) {

    if (!isLand) {
        int menuWidth = menu.getMeasuredWidth();

        // Ensure menu is visible
        menu.setVisibility(View.VISIBLE);

        if (!menuDisplayed) {
            // Scroll to 0 to reveal menu
            int left = 0;
            scrollView.smoothScrollTo(left, 0);
        } else {
            // Scroll to menuWidth so menu isn't on screen.
            int left = menuWidth;
            scrollView.smoothScrollTo(left, 0);
        }
        menuDisplayed = !menuDisplayed;
    }
    return true;

}

public class EventSort implements Comparator<ArrayList<Event>> {
    ArrayList<String> cattab;

    public EventSort() {
        cattab = new ArrayList<String>();
        cattab.add("sport");
        cattab.add("rugby");
        cattab.add("concert");
        cattab.add("spectacle");
        cattab.add("visite");
    }

    @Override
    public int compare(ArrayList<Event> lhs, ArrayList<Event> rhs) {

        if (lhs.isEmpty()) {
            if (rhs.isEmpty()) {
                return 0;
            } else {
                return 1;
            }
        } else if (rhs.isEmpty()) {
            return -1;
        } else {
            String typelhs = lhs.get(0).type;
            String typerhs = rhs.get(0).type;
            if (cattab.indexOf(typelhs) < cattab.indexOf(typerhs)) {
                return 1;
            } else if (cattab.indexOf(typelhs) > cattab.indexOf(typerhs)) {
                return -1;
            } else {
                return 0;
            }
        }

    }

}

这就是清单中没有配置更改的代码,我抓住了错误,以便我的应用程序不会崩溃

1 个答案:

答案 0 :(得分:0)

当屏幕方向改变时,Android会破坏您的活动并再次创建它。 因此,如果您的适配器持有对Activity的引用(可能是片段管理器),则会导致错误。