ViewPager Andorid:NullPointerException: 尝试调用虚拟方法

ViewPager Andorid:NullPointerException: Attempt to invoke virtual method

我有我的片段,我在 xml 文件中添加了两个滑块:

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;
    }

并且 xml 文件看起来:

<?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>

现在,当我尝试使用以下代码从主 activity 访问任何滑块时:

这是我的主要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 这里是 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());
    }

它显​​示以下错误:

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

如果有任何解决错误的建议,我将不胜感激。

我已经在真实设备 (API10) 上进行了测试并且可以正常工作。 (重写了很多部分。使用 diff 与您的源代码进行检查。)

需要更多信息来找出问题..


// 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>