Attempt to invoke virtual method 'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)' on a null object reference

匿名 (未验证) 提交于 2019-12-03 02:20:02

问题:

I'm trying to implement sliding tab layout using android material design. But it gives me NullPointerException. Here is my code so far:

MainActivity.java

public class MainActivity extends ActionBarActivity {     private Toolbar toolbar;     private ViewPager pager;     private SlidingTabLayout nLayout;       @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main_appbar);         toolbar = (Toolbar) findViewById(R.id.app_bar);         setSupportActionBar(toolbar);         getSupportActionBar().setDisplayShowHomeEnabled(true);          NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment)                 getSupportFragmentManager().findFragmentById(R.id.navigation_drawer_fragment);         drawerFragment.setUp(R.id.navigation_drawer_fragment, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);         pager = (ViewPager) findViewById(R.id.pager);         pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));          nLayout = (SlidingTabLayout) findViewById(R.id.tabs);         nLayout.setViewPager(pager);     }      @Override     public boolean onCreateOptionsMenu(Menu menu) {         // Inflate the menu; this adds items to the action bar if it is present.         getMenuInflater().inflate(R.menu.menu_main, 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_settings) {             return true;         }         if (id == R.id.navigate) {             startActivity(new Intent(this, SubActivity.class));          }          return super.onOptionsItemSelected(item);     }      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;         }     }      public static class MyFragment extends Fragment {         private TextView textView;          public static MyFragment getInstance(int position) {             MyFragment myFragment = new MyFragment();             Bundle args = new Bundle();             args.putInt("position", position);             myFragment.setArguments(args);             return myFragment;         }          @Nullable         @Override         public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {             View layout = inflater.inflate(R.layout.fragment_my, container, false);             textView = (TextView) layout.findViewById(R.id.position);             Bundle bundle = getArguments();             if (bundle != null) {                 textView.setText("The page selected is " + bundle.getInt("position"));             }             return layout;         }     }   } 

activity_main.xml

<LinearLayout     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical"     tools:context="carsaleapp.shanaka.com.carsaleapp.MainActivity">      <include         android:id="@+id/app_bar"         layout="@layout/app_bar" />      <app.shanaka.tabs.SlidingTabLayout         android:id="@+id/tabs"         android:layout_width="match_parent"         android:layout_height="wrap_content" />       <android.support.v4.view.ViewPager         android:id="@+id/pager"         android:layout_width="match_parent"         android:layout_height="0dp"         android:layout_weight="1" />   </LinearLayout> 

This is the error I got after run my application:

They keep on saying that the pager object is null. But for me I have set it correctly.

pager = (ViewPager) findViewById(R.id.pager); 

回答1:

You have your view pager in activity_main.xml while your layout for activity is setContentView(R.layout.activity_main_appbar). You do not have view pager in activity_main_appbar.xml.

findViewById looks for a view in the current view hierarchy. I guess it should be setContentView(R.layout.activity_main); but do check what views you have in included layout app_bar.xml



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!