ViewPager setAdapter = NullPointerException

时间:2015-09-22 10:53:26

标签: java android android-fragments nullpointerexception

我正在尝试使用PagerSlidingTabStrip库以新的方式实现ViewPager。我认为代码很好,但是当我尝试将适配器设置为ViewPager时,我遇到了错误。在我开始使用PagerSlidingTabStrip之前,我的代码工作正常,但该代码现已弃用。

错误:

java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.adrissa.klea/com.adrissa.klea.MainActivity}:
java.lang.NullPointerException Caused by:
java.lang.NullPointerException at
com.adrissa.klea.MainActivity.onInt(MainActivity.java:103) at
br.liveo.navigationliveo.NavigationLiveo.mountListNavigation(NavigationLiveo.java:248)
at
br.liveo.navigationliveo.NavigationLiveo.onCreate(NavigationLiveo.java:104)
at android.app.Activity.performCreate(Activity.java:5451)

这是ViewPager的实现:

    public class MainActivity extends NavigationLiveo implements
      br.liveo.interfaces.OnItemClickListener, NavigationDrawerFragment.NavigationDrawerCallbacks,
      ActionBar.TabListener {

   /**
    * Fragment managing the behaviors, interactions and presentation of the
    * navigation drawer.
    */
   private HelpLiveo mLiveo;
   ViewPager mViewPager;
   FragmentPagerAdapter mSectionsPagerAdapter;
   String category;
   public static ArrayList<String> categoriesForSellClass = new ArrayList<String>();

   /**
    * Used to store the last screen title. For use in
    * {@link #restoreActionBar()}.
    */
   private CharSequence mTitle;

   @Override
   public void onInt(Bundle savedInstanceState) {
      //super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      //user information NavDrawer
      this.userName.setText("Name");
      this.userEmail.setText("Email");
      this.userPhoto.setImageResource(R.drawable.selfie);
      this.userBackground.setImageResource(R.drawable.nav_drawer_bg);

      //setup navigation drawer
      mLiveo = new HelpLiveo();
      mLiveo.add(getString(R.string.home), R.drawable.home);
      mLiveo.add(getString(R.string.my_adverts), R.drawable.price);
      mLiveo.add(getString(R.string.messages), R.drawable.envelope);
      mLiveo.addSeparator();
      mLiveo.add(getString(R.string.settings), R.drawable.settings);
      mLiveo.add(getString(R.string.log_out), R.drawable.power);

      with(this, Navigation.THEME_LIGHT).startingPosition(0)
            .addAllHelpItem(mLiveo.getHelp())
            .setOnClickUser(onClickPhoto)
            .setOnPrepareOptionsMenu(onPrepare)
            .build();

      mTitle = getTitle();

      mSectionsPagerAdapter = new MyPagerAdapter(
            getSupportFragmentManager());
      final ActionBar actionBar = getSupportActionBar();
      //actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
      // Set up the ViewPager with the sections adapter.
      mViewPager = (ViewPager) findViewById(R.id.pager);

      //here is line 103 \/
      mViewPager.setAdapter(mSectionsPagerAdapter);

      PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
      tabs.setViewPager(mViewPager);

      // When swiping between different sections, select the corresponding
      // tab. We can also use ActionBar.Tab#select() to do this if we have
      // a reference to the Tab.
      tabs
            .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
               @Override
               public void onPageSelected(int position) {
                  actionBar.setSelectedNavigationItem(position);
               }
            });

      mViewPager.requestTransparentRegion(mViewPager);

      ParseQuery<ParseObject> query = ParseQuery.getQuery("Categories");
      query.findInBackground(new FindCallback<ParseObject>() {
         @SuppressLint("NewApi")
         public void done(List<ParseObject> objects, ParseException e) {
            if (e == null) {
               categoriesForSellClass.add("Alle");
               categoriesForSellClass.add("Favoritter");
               for (int i = 0; i < objects.size(); i++) {
                  ParseObject pObject = objects.get(i);
                  category = pObject.getString("name");
                  categoriesForSellClass.add(category);

               }
               // onSucced(objects);
            } else {

            }
         }
      });
   }

   @Override
   public void onNavigationDrawerItemSelected(int position) {
      // update the main content by replacing fragments
      FragmentManager fragmentManager = getSupportFragmentManager();
      fragmentManager.beginTransaction()
            .replace(R.id.container, AllFragment.newInstance(position + 1))
            .commit();
   }

   private View.OnClickListener onClickPhoto = new View.OnClickListener() {
      @Override
      public void onClick(View v) {
         Toast.makeText(getApplicationContext(), "onClickPhoto :D", Toast.LENGTH_SHORT).show();
         closeDrawer();
      }
   };

   private OnPrepareOptionsMenuLiveo onPrepare = new OnPrepareOptionsMenuLiveo() {
      @Override
      public void onPrepareOptionsMenu(Menu menu, int position, boolean visible) {
      }
   };

   public void onSectionAttached(int number) {

      switch (number) {
      case 1:
         mTitle = getString(R.string.title_section1);
         break;
      case 2:
         mTitle = getString(R.string.title_section2);
         break;
      case 3:
         mTitle = getString(R.string.title_section3);
         break;
      }
   }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
   }

   @Override
   public void onItemClick(int position) {
      Fragment mFragment;
      FragmentManager mFragmentManager = getSupportFragmentManager();
      mFragment = null;

      switch (position){
         case 2:
            //mFragment = new ViewPagerFragment();
            break;

         default:
            //mFragment = MainFragment.newInstance(mHelpLiveo.get(position).getName());
            break;
      }

      if (mFragment != null){
         mFragmentManager.beginTransaction().replace(R.id.container, mFragment).commit();
      }
   }

PagerAdapter类:

public class MyPagerAdapter extends FragmentPagerAdapter implements PagerSlidingTabStrip.IconTabProvider{

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

    @Override
    public int getCount() {
        // Show 5 total pages.
        return 5;
    }

    final int[] ICONS = new int[] {
        R.drawable.ic_action_camera,
        R.drawable.ic_action_person,
        R.drawable.ic_action_important,
        R.drawable.ic_action_time,
        R.drawable.ic_action_map
    };

    @Override
    public Fragment getItem(int i) {
        switch (i) {
        case 0:
            return new AllFragment();
        case 1:
            return new ProfileFragment();
        case 2:
            return new ThirdFragment();
        case 3:
            return new ProfileFragment();
        case 4:
            return new MapFragment().newInstance(new LatLng(55.7200,
                    10.5700));
        }
        return null;
    }

    @Override
    public int getPageIconResId(int i) {
        return ICONS[i];
    }
}

main_activity.xml

<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent">


<com.astuetz.PagerSlidingTabStrip
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="48dip" />

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f3f6f7"></android.support.v4.view.ViewPager>

</RelativeLayout>

感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

看起来您已将onCreate(Bundle args)的名称更改为onInt():

   @Override
   public void onInt(Bundle savedInstanceState) {
      //super.onCreate(savedInstanceState);

将方法名称更改回onCreate()。