Reputation: 1283
Here's the basic error description (full description at the bottom)
Attempt to invoke virtual method 'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)' on a null object reference
at mViewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
(code related to drawer and toolbar is removed)
MainActivity.java
//variables
private ViewPager mViewPager;
private SlidingTabLayout mSlidingTabLayout;
//onCreate
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_appbar);
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
mSlidingTabLayout = (SlidingTabLayout) findViewById(R.id.tabs);
mSlidingTabLayout.setViewPager(mViewPager);
}
//PagerAdapter
public class MyPagerAdapter extends FragmentPagerAdapter {
String[] tabs;
public MyPagerAdapter(FragmentManager fm) {
super(fm);
tabs = getResources().getStringArray(R.array.tabs);
}
@Override
public Fragment getItem(int position) {
MyFragment myFragment = MyFragment.getInstance(position);
return myFragment;
}
@Override
public CharSequence getPageTitle(int position) {
return tabs[position];
}
@Override
public int getCount() {
return 3;
}
}
//MyFragment
public static class MyFragment extends Fragment {
private TextView textView;
public static MyFragment getInstance(int position) {
MyFragment myFragment = new MyFragment();
Bundle bundleArgs = new Bundle();
bundleArgs.putInt("position", position);
myFragment.setArguments(bundleArgs);
return myFragment;
}
//OnCreateView
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragment_my, container, false);
textView = (TextView) layout.findViewById(R.id.position);
Bundle bundleValues = getArguments();
if(bundleValues!=null){
textView.setText("The page currently selected is" + bundleValues.getInt("position"));
}
return layout;
}
activity_main.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.company.name.mskv4.MainActivity">
<include
android:id="@+id/app_bar"
layout="@layout/app_bar" />
<com.piserve.geejo.mskv4.tabs.SlidingTabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.piserve.geejo.mskv4.tabs.SlidingTabLayout>
<com.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
</LinearLayout>
<fragment
android:id="@+id/fragment_navigation_drawer"
android:name="com.piserve.geejo.mskv4.NavigationDrawerFragment"
android:layout_width="@dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="@layout/fragment_navigation_drawer"
tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
fragmentmy.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/position"
android:layout_width="match_parent" android:layout_height="match_parent"
android:textColor="@color/color_primary_text"
android:textSize="20sp"
android:gravity="center"
android:text="The page number is 1">
</TextView>
strings.xml
<resources>
<string name="app_name">MSK v4</string>
<string name="title_activity_main">MyServiceKart</string>
<string-array name="tabs">
<item>Search</item>
<item>All</item>
<item>Featured</item>
</string-array>
</resources>
More Error Description
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.company.name.mskv4/com.company.name.mskv4.MainActivity}: 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
Upvotes: 2
Views: 8648
Reputation: 2381
You are setting content from activity_main_appbar.xml file, but you have added your pager into activity_main.xml file. When we set content of any xml file then compiler will look into that specific file. So just use
setContentView(R.layout.activity_main);
instead of:
setContentView(R.layout.activity_main_appbar);
Upvotes: 1
Reputation: 475
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager(), getApplicationContext()));
public MyPagerAdapter(FragmentManager fm, Context context) {
super(fm);
tabs = context.getResources().getStringArray(R.array.tabs);
}
try it..
Upvotes: 1