MKS
MKS

Reputation: 786

ViewPager Andorid:NullPointerException: Attempt to invoke virtual method

I've got my Fragment where I have added two sliders in the xml file:

public class FilterSlider extends Fragment implements FragmentLifecycle {

    private static final String TAG = FilterSlider.class.getSimpleName();
    RelativeLayout relativeLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.filter_slider, container, false);
        return view;
    }

and the xml file looks:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <SeekBar
        android:id="@+id/seekBarMin"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="20dp"
        android:max="100"/>

    <SeekBar
        android:id="@+id/seekBarMax"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="50dp"
        android:max="100"/>

</RelativeLayout>

Now when I am trying to access any sliderbar from the main activity with the following codes:

Here is my main activity class:

    setContentView(R.layout.activity_main);
    pageAdapter = new MyPagerAdapter(getSupportFragmentManager());
    final ViewPager pager = (ViewPager)findViewById(R.id.myViewPager);
    pager.setAdapter(pageAdapter);

   seekbarMin = (SeekBar) findViewById(R.id.seekBarMin);
  seekbarMax = (SeekBar) findViewById(R.id.seekBarMax);
 sliderMaxTemp = seekbarMax.getProgress();

adn here is the MyPageAdapter class:

private List<Fragment> fragments;

    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
        this.fragments = new ArrayList<Fragment>();
        fragments.add(new FilterCheckBox());
        fragments.add(new FilterRadioButton());
        fragments.add(new FilterSlider());
    }

it shows following error:

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.SeekBar.getProgress()' on a null object reference.

I would highly appreciate any suggestions to solve the error.

Upvotes: 1

Views: 3774

Answers (1)

Toris
Toris

Reputation: 2376

I've tested on real device (API10) and it works. (Rewritten many parts.. Use diff with your source to check them.)

Need more information to find out the problem..


// part of manifest
<uses-sdk android:minSdkVersion="10" />

//MainActivity.java    
// Changed imports.
// MUST check v4 library class or not. (for each UI parts class etc.)
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener; // for selectButton_* click events.
import android.widget.SeekBar;

// Changed Activity -> FragmentActivity to use getSupportFragmentManager()
public class MainActivity extends FragmentActivity
{
    private ViewPager pager;
    private SeekBar seekbarMin;
    private SeekBar seekbarMax;
    private int sliderMaxTemp;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Init();
    }

    private void Init()
    {
        MyPagerAdapter pageAdapter = new MyPagerAdapter(getSupportFragmentManager());
        pager = (ViewPager) findViewById(R.id.myViewPager);
        pager.setAdapter(pageAdapter);
        seekbarMin = (SeekBar) findViewById(R.id.seekBarMin);
        seekbarMax = (SeekBar) findViewById(R.id.seekBarMax);
        sliderMaxTemp = seekbarMax.getProgress();

        // Events to choose page.
        // You may want to use onTouch or gesture..
        final int idx_CheckBox = 0;
        final int idx_RadioButton = 1;
        final int idx_Slider = 2;
        findViewById(R.id.selectButton_CheckBox).setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // Select page
                pager.setCurrentItem(idx_CheckBox);
            }
        });
        findViewById(R.id.selectButton_RadioButton).setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                pager.setCurrentItem(idx_RadioButton);
            }
        });
        findViewById(R.id.selectButton_Slider).setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                pager.setCurrentItem(idx_Slider);
            }
        });
    }
}

// FilterSlider.java
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

// Changed to "v4" Fragment
public class FilterSlider extends Fragment
{
    private static final String TAG = FilterSlider.class.getSimpleName();
    RelativeLayout relativeLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.filter_slider, container, false);
        return view;
    }
}

// MyPagerAdapter.java
import java.util.ArrayList;
import java.util.List;

// Changed to "v4" classes
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

// Changed PagerAdapter -> FragmentStatePagerAdapter
public class MyPagerAdapter extends FragmentStatePagerAdapter
{
    private List<Fragment> fragments;

    MyPagerAdapter(FragmentManager fm)
    {
        super(fm);
        this.fragments = new ArrayList<Fragment>();
        fragments.add(new FilterCheckBox());
        fragments.add(new FilterRadioButton());
        fragments.add(new FilterSlider());
    }

    @Override
    public int getCount()
    {
        return fragments.size();
    }

    @Override
    public Fragment getItem(int position)
    {
        // returns fragment you want to show
        try
        {
            return fragments.get(position);
        }
        catch (IndexOutOfBoundsException e)
        {
            // TODO: handle exception
            return null;
        }
    }
}

activity_main.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" >

    <SeekBar
        android:id="@+id/seekBarMin"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="20dp"
        android:max="100" />

    <SeekBar
        android:id="@+id/seekBarMax"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="50dp"
        android:max="100" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/selectButton_CheckBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="CheckBox" />

        <Button
            android:id="@+id/selectButton_RadioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RadioButton" />

        <Button
            android:id="@+id/selectButton_Slider"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Slider" />

    </LinearLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/myViewPager"
        android:layout_width="250dp"
        android:layout_height="wrap_content" />

</LinearLayout>

filter_slider.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" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Filter Slider"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

Upvotes: 1

Related Questions