Solution 1 :
I guess the reason why it is not showing properly it is because you don’t have the oncreate() implemented in your project so under
private ViewPager mMyViewPager;
private TabLayout mTabLayout;
Add this :
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main);
}
Don’t forget to delete this line of code setContentView(R.layout.main);
from your Init function
Problem :
My problem is when I go to a viewpager it shows for a second and then I get a blank page and I don’t where is the problem.
Here is my code to go from an activity to a viewpager:
final WordAdapter adapter = new WordAdapter(this, words,R.color.category_numbers);
// Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
// There should be a {@link ListView} with the view ID called list, which is declared in the
// activity_numbers.xml layout file.
// word_list.xml layout file.
final ListView listView = (ListView) findViewById(R.id.list);
// Make the {@link ListView} use the {@link WordAdapter} we created above, so that the
// {@link ListView} will display list items for each {@link Word} in the list.
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getApplicationContext(),Main.class);
adapter.getItemId(i);
init(i);
startActivity(intent);
}
});
And my the viewpager is here:
public class Main extends AppCompatActivity {
// widgets
private ViewPager mMyViewPager;
private TabLayout mTabLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void init(int i) {
mTabLayout = findViewById(R.id.tab_layout);
mMyViewPager = findViewById(R.id.view_pager);
ArrayList<Fragment> fragments = new ArrayList<>();
Hat[] hats = Hats.getHats();
for (Hat hat : hats) {
com.example.android.miwok.ViewPagerItemFragment fragment = com.example.android.miwok.ViewPagerItemFragment.getInstance(hat);
fragments.add(fragment);
}
MyPagerAdapter pagerAdapter = new MyPagerAdapter(getSupportFragmentManager() , fragments);
mMyViewPager.setAdapter(pagerAdapter);
mTabLayout.setupWithViewPager(mMyViewPager);
mMyViewPager.setCurrentItem(i);
}
}
Xml for viewpager is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns_android="http://schemas.android.com/apk/res/android"
xmlns_tools="http://schemas.android.com/tools"
android_layout_width="match_parent"
android_layout_height="match_parent"
xmlns_app="http://schemas.android.com/apk/res-auto"
tools_context=".Main"
android_orientation="vertical"
android_weightSum="100"
android_background="#fff">
<android.support.v4.view.ViewPager
android_layout_width="match_parent"
android_layout_height="0dp"
android_id="@+id/view_pager"
android_layout_weight="90">
</android.support.v4.view.ViewPager>
<android.support.design.widget.TabLayout
android_id="@+id/tab_layout"
android_layout_width="match_parent"
android_layout_height="wrap_content"
app_tabBackground="@drawable/tab_selector"
app_tabGravity="center"
app_tabIndicatorHeight="0dp"
app_tabMaxWidth="10dp"/>
</LinearLayout>
And my is here Pageadapter:
public class MyPagerAdapter extends FragmentStatePagerAdapter {
private ArrayList<Fragment> mFragments = new ArrayList<>();
public MyPagerAdapter(FragmentManager fm, ArrayList<Fragment> fragments) {
super(fm);
mFragments = fragments;
}
@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
@Override
public int getCount() {
return mFragments.size();
}
}
I edited my code here so you can know more about my code hope someone can help!
Comments
Comment posted by Taki
can you please show more of your code to examinate it further
Comment posted by Moataz El-Naggar
you want to see more about the arraylists or the adapter code ?
Comment posted by Taki
Check out my answer , it might help you if not let me know here again , Yeah i want to see your whole code to better understand the structure of your code
Comment posted by Taki
Did you solve it ?
Comment posted by Moataz El-Naggar
No it didn’t 🙁
Comment posted by Moataz El-Naggar
when i debug it gives me this : E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.android.miwok, PID: 11955 java.lang.NullPointerException: Attempt to invoke virtual method ‘void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)’ on a null object reference at com.example.android.miwok.Main.init(Main.java:36) at com.example.android.miwok.PhrasesActivity$1.onItemClick(PhrasesActivity.java:52)