在sdk更新后有片段显示空白屏幕的选项卡

时间:2017-04-24 12:05:42

标签: android android-studio android-fragments android-viewpager android-tabs

我的滑动标签在SDKversion 23上运行得非常好但是在我更新了我的Android工作室后,它开始显示空白屏幕,滑动停止工作,所以我尝试创建一个新的Fragment然后我意识到一些新的方法,如OnAttach()是预加载的,我运行应用程序我得到了这个错误Link,我删除了他们说的OnAttach方法,但现在没有错误,但空白屏幕仍然存在。

public class AdmissionActivity extends AppCompatActivity {
    private PagerAdmissionAdapter mAdapter;
    private TabLayout mTabLayout;
    private ViewPager mPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admission);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);




        mTabLayout = (TabLayout) findViewById(R.id.tab_layout_admission);
        mAdapter = new PagerAdmissionAdapter(getSupportFragmentManager());
        mPager = (ViewPager) findViewById(R.id.pager_admission);
        mPager.setAdapter(mAdapter);


        //Notice how the Tab Layout links with the Pager Adapter
        mTabLayout.setTabsFromPagerAdapter(mAdapter);
       // mTabLayout.addTab(mTabLayout.newTab().setText("Hello Word"));


        //Notice how The Tab Layout adn View Pager object are linked
        mTabLayout.setupWithViewPager(mPager);
        mPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
        mPager.setCurrentItem(1);


    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.admission_menu, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_apply) {
            Intent intent1 = new Intent(AdmissionActivity.this, WebViewActivity.class);
            intent1.putExtra("url","http://ntnu.edu.ng/?online_application");
            startActivity(intent1);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}

    class PagerAdmissionAdapter extends FragmentStatePagerAdapter {

    public PagerAdmissionAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        //FourthActivity.MyFragment myFragment = FourthActivity.MyFragment.newInstance(position);
        Fragment fragment = null;


        if (position == 0) {
            fragment = new ExtraFragmentA();
        } else if (position == 1) {
            fragment = new ExtraFragmentB();

        } else if (position == 2) {
            fragment = new ExtraFragmentC();

        }

        return fragment;

    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        String tab=null;
        switch(position)
        {
            case 0:
                tab= "Faculties";
                break;
            case 1:
                tab= "Guidelines";
                break;
            case 2:
                tab="Entry Req";
                break;

        }
        return tab;
    }
   }

public class ExtraFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

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

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment ExtraFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static ExtraFragment newInstance(String param1, String param2) {
        ExtraFragment fragment = new ExtraFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_extra, container, false);
        return view;
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }



    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
   }

0 个答案:

没有答案