Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 碎片视图页面显示为空_Java_Android_Android Fragments_Android Viewpager - Fatal编程技术网

Java 碎片视图页面显示为空

Java 碎片视图页面显示为空,java,android,android-fragments,android-viewpager,Java,Android,Android Fragments,Android Viewpager,我尝试了很多答案,但都不适合我。我有一个查看寻呼机,其中有三个片段,但当我在手机上运行应用程序时,片段不会出现,并且查看寻呼机显示为空 这是主要活动代码: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); T

我尝试了很多答案,但都不适合我。我有一个查看寻呼机,其中有三个片段,但当我在手机上运行应用程序时,片段不会出现,并且查看寻呼机显示为空

这是主要活动代码:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        assert actionBar != null;
        actionBar.setDisplayShowTitleEnabled(false);


        mAuth = FirebaseAuth.getInstance();
        database = FirebaseDatabase.getInstance();
        myRef = database.getReference("posts");

        mViewPager = findViewById(R.id.viewpager);
        adapter = new SamplePagerAdapter(getSupportFragmentManager(),this);
        tabLayout = findViewById(R.id.tablayout);
        mViewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(mViewPager);

        mViewPager.setCurrentItem(1);
        TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tabOne.setText("تعلم");
        tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.learn, 0, 0);
        tabLayout.getTabAt(0).setCustomView(tabOne);

        TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tabTwo.setText("الصفحة الرئيسية");
        tabTwo.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.home, 0, 0);
        tabLayout.getTabAt(1).setCustomView(tabTwo);

        TextView tabThree = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tabThree.setText("التحديات");
        tabThree.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.challenges, 0, 0);
        tabLayout.getTabAt(2).setCustomView(tabThree);
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                tabPosition = tab.getPosition();
                Toast.makeText(MainActivity.this,
                        "Selected page position: " + tab.getPosition(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }

and this is the view pager adapter code : 

public class SamplePagerAdapter extends FragmentPagerAdapter {
    Context context;

    public SamplePagerAdapter(FragmentManager fragmentManager, Context context) {
        super(fragmentManager);
        this.context = context;
    }

    /**
     * @return the number of pages to display
     */
    @Override
    public int getCount() {
        return 3;
    }

    /**
     * @return true if the value returned from {@link #instantiateItem(ViewGroup, int)} is the
     */
    @Override
    public boolean isViewFromObject(View view, Object o) {
        return o == view;
    }

    // BEGIN_INCLUDE (pageradapter_getpagetitle)

    /**
     * Return the title of the item at {@code position}. This is important as what this method
     * <p>
     * Here we construct one using the position value, but for real application the title should
     * refer to the item's contents.
     */
    @Override
    public CharSequence getPageTitle(int position) {
        String title = "";
        switch (position) {
            case 0:
                title = "تعلم";
                break;
            case 1:
                title = "الصفحة الرئيسية";
                break;
            case 2:
                title = "التحديات";
                break;
        }
        return title;
    }
    // END_INCLUDE (pageradapter_getpagetitle)

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                Toast.makeText(context, "position 0", Toast.LENGTH_SHORT).show();
                return new LearnFragment();
            case 1:
                Toast.makeText(context, "position 1", Toast.LENGTH_SHORT).show();
                return new HomeFragment();
            case 2:
                Toast.makeText(context, "position 2", Toast.LENGTH_SHORT).show();
                return new ChallengesFragment();
        }

        return null;
    }

}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar Toolbar=findviewbyd(R.id.Toolbar);
设置支持操作栏(工具栏);
ActionBar ActionBar=getSupportActionBar();
断言actionBar!=null;
actionBar.setDisplayShowTitleEnabled(false);
mAuth=FirebaseAuth.getInstance();
database=FirebaseDatabase.getInstance();
myRef=database.getReference(“posts”);
mviewpage=findviewbyd(R.id.viewpager);
adapter=new-SamplePagerAdapter(getSupportFragmentManager(),this);
tabLayout=findviewbyd(R.id.tabLayout);
mViewPager.setAdapter(适配器);
tabLayout.setupWithViewPager(mviewpage);
mViewPager.setCurrentItem(1);
TextView tabOne=(TextView)LayoutInflater.from(this).flate(R.layout.custom_tab,null);
tabOne.setText(“تعلم”);
tabOne.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.learn,0,0);
tabLayout.getTabAt(0.setCustomView(tabOne);
TextView tabTwo=(TextView)LayoutInflater.from(this).flate(R.layout.custom_tab,null);
tabTwo.setText(“الح㶟㶟㶟㶟㶟㶟㶟㶟㶀㶟㶟㶟㶟㶀1577;
tabTwo.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.home,0,0);
tabLayout.getTabAt(1).setCustomView(tabTwo);
TextView选项卡三=(TextView)LayoutInflater.from(this).flate(R.layout.custom_选项卡,null);
tabThree.setText(“التحديت”);
表3.SetCompoundDrawablesWithinInsitCbounds(0,R.Drawables.challenges,0,0);
tabLayout.getTabAt(2)、setCustomView(tab三);
tabLayout.addOnTabSelectedListener(新的tabLayout.OnTabSelectedListener(){
@凌驾
已选择的公共选项卡(TabLayout.Tab){
tabPosition=tab.getPosition();
Toast.makeText(MainActivity.this,
所选页面位置:“+tab.getPosition(),Toast.LENGTH_SHORT.show();
}
@凌驾
已选择的公共选项卡(TabLayout.Tab){
}
@凌驾
已重新选择公共选项卡(TabLayout.Tab){
}
});
}
这是查看寻呼机适配器代码:
公共类SamplePagerAdapter扩展了FragmentPagerAdapter{
语境;
公共SamplePagerAdapter(碎片管理器碎片管理器,上下文){
超级(碎片管理器);
this.context=上下文;
}
/**
*@返回要显示的页数
*/
@凌驾
public int getCount(){
返回3;
}
/**
*@return true,如果从{@link#instanceItem(ViewGroup,int)}返回的值是
*/
@凌驾
公共布尔值isViewFromObject(视图,对象o){
返回o==视图;
}
//开始包括(pageradapter_getpagetitle)
/**
*返回{@code position}处项目的标题。这与此方法的作用一样重要
*
*在这里,我们使用位置值构造一个,但对于实际应用,标题应该是
*参考项目的内容。
*/
@凌驾
公共字符序列getPageTitle(int位置){
字符串标题=”;
开关(位置){
案例0:
title=“تعلم”;
打破
案例1:
title=“الح㶡㶟㶞㶀㶡㶡”;
打破
案例2:
title=“التديات”;
打破
}
返回标题;
}
//结束\包括(pageradapter \ getpagetitle)
@凌驾
公共片段getItem(int位置){
开关(位置){
案例0:
Toast.makeText(上下文,“位置0”,Toast.LENGTH_SHORT).show();
返回新的LearnFragment();
案例1:
Toast.makeText(上下文,“位置1”,Toast.LENGTH_SHORT).show();
返回新的HomeFragment();
案例2:
Toast.makeText(上下文,“位置2”,Toast.LENGTH_SHORT).show();
返回新的ChallengesFragment();
}
返回null;
}
}
这是活动主xml代码:

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/sample_main_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/color1">

                <TextView
                    android:id="@+id/toolbar_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="@string/app_name"
                    android:textColor="@color/white"
                    android:textSize="@dimen/text_size_in_toolbar" />
            </android.support.v7.widget.Toolbar>

            <android.support.design.widget.TabLayout
                android:id="@+id/tablayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/color1">

            </android.support.design.widget.TabLayout>
        </android.support.design.widget.AppBarLayout>


        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="0px"
            android:layout_weight="1"
            android:background="@android:color/white" />

    </LinearLayout>

删除此代码

@Override
public boolean isViewFromObject(View view, Object o) {
    return o == view;
}