1

我有一个小问题。我正在尝试使用片段创建布局,但是当我调用该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" 
/>

日志猫

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)
4

1 回答 1

4

错误的浏览器 ID。你在 xml 中写道:

android:id="@+id/agenda_sw"

但在代码中,您可以找到 R.id.agendaPager 并且应该找到 R.id.agenda_sw。

于 2013-03-04T14:17:11.607 回答