ViewPager Andorid:NullPointerException: 尝试调用虚拟方法
Posted
技术标签:
【中文标题】ViewPager Andorid:NullPointerException: 尝试调用虚拟方法【英文标题】:ViewPager Andorid:NullPointerException: Attempt to invoke virtual method 【发布时间】:2015-10-06 12:15:15 【问题描述】:我有我的片段,我在 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_
android:layout_>
<SeekBar
android:id="@+id/seekBarMin"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:max="100"/>
<SeekBar
android:id="@+id/seekBarMax"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="50dp"
android:max="100"/>
</RelativeLayout>
现在,当我尝试使用以下代码从主活动访问任何滑块时:
这是我的主要活动课程:
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();
这里是 MyPageAdapter 类:
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.
我非常感谢任何解决错误的建议。
【问题讨论】:
片段是否在您尝试查找视图时附加到活动?你能看到搜索栏吗? 请添加相关类,尤其是计时器周围的部分,以及将片段附加到活动的位置/方式。 @ataulm:我在帖子中添加了更多相关类 怎么样 ((ProgressBar)seekbarMax).getProgress(); ?它会导致同样的错误吗? 抱歉,计时器呢?你说如果你没有计时器,你可以看到搜索栏,但你看不到它 - 听起来回答这个问题很重要。 【参考方案1】:我已经在真实设备 (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_
android:layout_
android:orientation="vertical" >
<SeekBar
android:id="@+id/seekBarMin"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:max="100" />
<SeekBar
android:id="@+id/seekBarMax"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="50dp"
android:max="100" />
<LinearLayout
android:layout_
android:layout_ >
<Button
android:id="@+id/selectButton_CheckBox"
android:layout_
android:layout_
android:text="CheckBox" />
<Button
android:id="@+id/selectButton_RadioButton"
android:layout_
android:layout_
android:text="RadioButton" />
<Button
android:id="@+id/selectButton_Slider"
android:layout_
android:layout_
android:text="Slider" />
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="@+id/myViewPager"
android:layout_
android:layout_ />
</LinearLayout>
filter_slider.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:orientation="vertical" >
<TextView
android:layout_
android:layout_
android:text="Filter Slider"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
【讨论】:
您好 Toris,感谢您的代码。我在我的手机上检查了你的,它工作得很好。我想知道您是否可以建议我进行可能的更改,以便我可以添加另一个带有小部件(例如复选框)的片段并在屏幕之间滑动(类似于 developer.android.com/training/animation/screen-slide.html) 已编辑。部分类更改为 android.support.v4.* 并实现片段分页功能。 (由按钮控制)以上是关于ViewPager Andorid:NullPointerException: 尝试调用虚拟方法的主要内容,如果未能解决你的问题,请参考以下文章
andorid自己定义ViewPager之——子ViewPager滑到边缘后直接滑动父ViewPager