Android - setAdapter ViewPager上的NullPointerException

时间:2013-03-04 14:12:42

标签: android android-fragments android-viewpager android-fragmentactivity

我有点问题。我正在尝试使用片段创建布局,但是当我调用setAdapter方法时,我有一个Java NullPointerException,我找不到错误。 谢谢大家!

班级代码

public class Agenda extends FragmentActivity{

PagerAdapter mPagerAdapter;

ViewPager mViewPager;

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.agenda_sw);

    mPagerAdapter = new PagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.agendaPager);

    mViewPager.setAdapter(mPagerAdapter); //Line 32, the error is on this line.

}

xml代码

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

LogCat

03-04 11:06:14.686: E/AndroidRuntime(1356): FATAL EXCEPTION: main
03-04 11:06:14.686: E/AndroidRuntime(1356): java.lang.RuntimeException: Unable to start activity ComponentInfo{conf.main/conf.main.Agenda}: java.lang.NullPointerException
...
03-04 11:06:14.686: E/AndroidRuntime(1356): Caused by: java.lang.NullPointerException
03-04 11:06:14.686: E/AndroidRuntime(1356): at conf.main.Agenda.onCreate(Agenda.java:32)

1 个答案:

答案 0 :(得分:4)

错误的viewpager id。你用xml编写:

android:id="@+id/agenda_sw"

但是在代码中你找到了R.id.agendaPager,应该是R.id.agenda_sw。