Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9224911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:26:47+00:00 2026-06-18T04:26:47+00:00

I am having some trouble with this piece of code: MyAdapter.java: import android.support.v4.app.Fragment; import

  • 0

I am having some trouble with this piece of code:

MyAdapter.java:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;


public static class MyAdapter extends FragmentStatePagerAdapter {
    public MyAdapter(FragmentManager fm) {
        super(fm);

    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
        case 0: return new SpeedZonesFragment();
        case 1: return new DistanceFragment();
        case 2: return new FuelConsumptionFragment();
        default: return null;
        }
    }
}

MainActivity.java:

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

public class MainActivity extends FragmentActivity {

    private MyAdapter myAdapter;
    private ViewPager myViewPager;

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

        myAdapter = new MyAdapter(getSupportFragmentManager());

        myViewPager = (ViewPager) findViewById(R.id.pager);
        myViewPager.setAdapter(myAdapter);

view_pager.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/view_pager" />

</LinearLayout>

My problem goes that every example i can find declares MyAdapter as a public static class however Eclipse raise the error: Illegal modifier for the class MyAdapter; only public, abstract & final are permitted.

If i try to run my code without the static it returns (from LogCat):

Caused by: java.lang.NullPointerException
02-02 23:13:26.076: E/AndroidRuntime(928):
at simcas.fartberegneren.MainActivity.onCreate(MainActivity.java:23)

EDIT: Line 23 is the line myViewPager.setAdapter(myAdapter);

Clearly i am doing something wrong but i just can’t locate it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:26:48+00:00Added an answer on June 18, 2026 at 4:26 am

    Firstly, no, static classes are only used for inner classes wherein the inner class’s instances should not be tied to the outer class’s instance. (That is, in an outer class, the static modified should not be used.)

    Second, the null pointer is in the statement myViewPager.setAdapter(myAdapter);, as you deduced, meaning myViewPager is null. Therefore, the findViewById() is returning null (item not found):

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

    Notice, in your XML: android:id="@+id/view_pager". The above line should then be:

    myViewPager = (ViewPager) findViewById(R.id.view_pager);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some trouble understanding import This piece of code works properly. I from
I'm having some trouble understanding this piece of code that my professor used as
I am having some trouble with a piece of code below: Input: li is
I'm having some trouble getting this code to compile on Linux but it works
I am having trouble with this piece of code and I can't figure out
I'm having a small bit of trouble trying to get this piece of code
I am having some trouble with this code . The problem is when i
I'm having some trouble with a particular piece of code, if anyone can enlighten
I'm having some trouble getting this to work. This is the code I want
I'm having some trouble with this statement, owing no doubt to my ignorance of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.