选择不同的选项卡时,应用程序崩溃

时间:2016-03-08 07:04:05

标签: android tabs material-design

当我选择其他标签时,应用程序显示不幸的是应用已停止?

请找到日志并尽快帮我解决,

先谢谢

java.lang.ClassCastException: android.view.AbsSavedState$1 cannot be cast to android.widget.CompoundButton$SavedState
    at android.widget.CompoundButton.onRestoreInstanceState(CompoundButton.java:511)
    at android.view.View.dispatchRestoreInstanceState(View.java:13629)
    at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2893)
    at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2893)
    at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2893)
    at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2893)
    at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2893)
    at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2893)
    at android.view.View.restoreHierarchyState(View.java:13607)
    at android.support.v4.app.Fragment.restoreViewState(Fragment.java:468)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1094)
    at android.support.v4.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1426)
    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:728)
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
    at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
    at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
    at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:552)
    at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:514)
    at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:495)
    at android.support.design.widget.TabLayout$ViewPagerOnTabSelectedListener.onTabSelected(TabLayout.java:1778)
    at android.support.design.widget.TabLayout.selectTab(TabLayout.java:871)
    at android.support.design.widget.TabLayout.selectTab(TabLayout.java:841)
    at android.support.design.widget.TabLayout$Tab.select(TabLayout.java:1115)
    at android.support.design.widget.TabLayout$1.onClick(TabLayout.java:665)
    at android.view.View.performClick(View.java:4756)
    at android.view.View$PerformClick.run(View.java:19761)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5253)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:900)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695)

我有10个片段(10个标签),所有片段都有不同的ID

    public OneFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
       db = new DBAdapter(this.getActivity());

        return inflater.inflate(R.layout.fragment_one, container, false);
    }

    private void saveorder() {
        LinearLayout pLayout= (LinearLayout) getActivity().findViewById(R.id.laymenu1);
        for(int i = 0; i < pLayout.getChildCount(); i++)
        {
            if ((pLayout.getChildAt(i) instanceof LinearLayout) ) {
                LinearLayout SubLayout = (LinearLayout) pLayout.getChildAt(i);

                for (int j = 0; j < SubLayout.getChildCount(); j++) {
                    TextView MenuId = (TextView) SubLayout.getChildAt(0);
                    menu_id = Integer.parseInt(MenuId.getText().toString());

                    TextView MenuName = (TextView) SubLayout.getChildAt(1);
                    menu_name = MenuName.getText().toString();

                    CheckBox stat=(CheckBox) SubLayout.getChildAt(2);
                    if(stat.isChecked()==true)
                    {
                        status=1;
                    }
                    else
                    {
                        status=0;
                    }

                    EditText Item = (EditText) SubLayout.getChildAt(3);
                    if(Item.getText().toString().length()>0 )
                    {
                        items = Integer.parseInt(Item.getText().toString());
                    }
                    else
                    {
                        items=0;
                    }
                }
                db.insertorder(menu_id, menu_name, status, items, 1);
                }
            }
        }


    @Override
     public void onViewCreated(View view, Bundle savedInstanceState) {
         super.onViewCreated(view, savedInstanceState);
         lay1= (LinearLayout) getActivity().findViewById(R.id.laymenu1);
        initviews();

        menulist();
        btnmenu1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveorder();
            }
        });
     }

    private void initviews() {
        btnmenu1 = (Button) getActivity().findViewById(R.id.btnmenu1);

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }


    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void menulist() {

        Cursor c1;
        count=menuitems.length;
        int k=0;
        String array_menuid[]= new String[count];
        String array_menuname[]= new String[count];


        db.open();
      c1=db.getQueryResult("SELECT itemid,itemname,status,items FROM menu_order where cat_id=1");
        int total =c1.getCount();
        String array_menuitems[]=new String[total];
        String array_manustat[] = new String[total];
        if (c1.moveToFirst()) {
            do {
                array_manustat[k] = c1.getString(2);
                array_menuitems[k] = c1.getString(3);
                k += 1;
            } while (c1.moveToNext());
        }

        else
        {
            for(int m=0; m < count; m++)
            {
                array_menuid[m] ="";
                array_menuname[m] = "";
                array_manustat[m] = "";
                array_menuitems[m] = "";
            }
        }
        c1.close();
db.close();;

        int i;
        for (i = 0; i < menuitems.length; i++) {
            linearLayout = new LinearLayout(this.getActivity());
            linearLayout.setId(i);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
            linearLayout.setWeightSum(150);
            linearLayout.setOrientation(LinearLayout.HORIZONTAL);
            linearLayout.setBackgroundColor(getResources().getColor(android.R.color.transparent));
            params.setMargins(100, 100, 100, 100);
            lay1.addView(linearLayout);


            for (int j = 0; j < 5; j++) {
                if (j == 0) {
                    tvsinum = new TextView(this.getActivity());
                    tvsinum.setId(+i + j);
                    tvsinum.setTypeface(type);
                    tvsinum.setTextColor(getResources().getColor(R.color.white));
                    tvsinum.setText(String.valueOf(i + 1));
                    tvsinum.setLayoutParams(new LinearLayout.LayoutParams(0, 80, 18));
                    tvsinum.setPadding(8, 8, 8, 8);
                    linearLayout.addView(tvsinum);
                }
                if (j == 1) {
                    tvitem = new TextView(this.getActivity());
                    tvitem.setId(+i + j);
                    tvitem.setTypeface(type);
                    tvitem.setTextColor(getResources().getColor(R.color.white));
                    tvitem.setLayoutParams(new LinearLayout.LayoutParams(0, 80, 65));
                    tvitem.setPadding(18, 8, 8, 8);
                    tvitem.setText(menuitems[i]);
                    linearLayout.addView(tvitem);
                }
                if (j == 2) {
                    selcheck = new CheckBox(this.getActivity());
                    selcheck.setId(+i + j);
                    selcheck.setLayoutParams(new LinearLayout.LayoutParams(0, 80, 15));
                    selcheck.setTag(TAG_DEC);
                    if(Integer.parseInt(array_manustat[i])>0)
                    {
                        selcheck.setChecked(true);
                    }
                    else
                    {
                        selcheck.setChecked(false);
                    }
                    selcheck.setPadding(68, 8, 8, 8);
                    selcheck.setTextColor(getResources().getColor(android.R.color.transparent));
                    linearLayout.addView(selcheck);
                }
                if (j == 3) {
                    etitem = new EditText(this.getActivity());
                    etitem.setId(+i + j);
                    etitem.setTypeface(type);
                    if(Integer.parseInt(array_menuitems[i])>0)
                    {
                        etitem.setText(array_menuitems[i]);
                    }
                    else
                    {
                        etitem.setText("");
                    }
                    etitem.setInputType(InputType.TYPE_CLASS_NUMBER);
                    etitem.setHint("Qty");
                    etitem.setTextColor(getResources().getColor(R.color.white));
                    etitem.setLayoutParams(new LinearLayout.LayoutParams(0, 80, 20));
                    etitem.setPadding(8, 8, 8, 8);
                    linearLayout.addView(etitem);
                }


                if (j == 4) {
                    tvprice = new TextView(this.getActivity());
                    tvprice.setId(+i + j);
                    tvprice.setTypeface(type);
                    tvprice.setText(String.valueOf(price[i]));
                    tvprice.setLayoutParams(new LinearLayout.LayoutParams(0, 80, 20));
                    tvprice.setPadding(8, 8, 8, 8);
                    tvprice.setTextColor(getResources().getColor(R.color.white));
                    linearLayout.addView(tvprice);
                }
            }
            count = count + 1;
        }
    }
}
主要活动

将适配器和网页浏览器设置为

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

      /*  toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);*/
      //  getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFrag(new OneFragment(), "Chicken");
        adapter.addFrag(new TwoFragment(), "Mutton");
        adapter.addFrag(new ThreeFragment(), "Veg");
        adapter.addFrag(new FourFragment(), "Brewery");
        adapter.addFrag(new FiveFragment(), "Coffee");
        adapter.addFrag(new SixFragment(), "North Indian");
        adapter.addFrag(new SevenFragment(), "Chicken Spl");
        adapter.addFrag(new EightFragment(), "Mutton Spl");
        adapter.addFrag(new NineFragment(), "Soups");
        adapter.addFrag(new TenFragment(), "Starters");
        viewPager.setAdapter(adapter);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
          //  return mFragmentList.size();
            return 10;
        }

        public void addFrag(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }
}

0 个答案:

没有答案