App在应用viewpager和片段时崩溃了

时间:2014-11-28 18:04:48

标签: java android android-fragments

我正在尝试在我的主菜单上为我的应用创建一个幻灯片屏幕(viewpager),但遗憾的是它崩溃了。有人能告诉我我做错了什么吗?这是我的代码: -

MainMenuActivity.java

public class MainMenuActivity extends ActionBarActivity {

    private PagerAdapter mPagerAdapter;

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
        if (toolbar != null) {
            setSupportActionBar(toolbar);
        }

        List<Fragment> fragments = new Vector<Fragment>();
        fragments.add(Fragment.instantiate(this,EducationActivity.class.getName()));

        mPagerAdapter = new PagerAdapter(this.getSupportFragmentManager(), fragments);
        ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
        pager.setAdapter(mPagerAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        else if (id == R.id.action_disclaimer) {
            Intent intent = new Intent(this, DisclaimerActivity.class);
            startActivity(intent);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

EducationActivity.java

public class EducationActivity extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        if(container == null)
        {
            return null;
        }
        return (LinearLayout) inflater.inflate(R.layout.activity_education,
                container,false);
    }
}

PagerAdapter.java

public class PagerAdapter extends FragmentPagerAdapter {

    private List<Fragment> fragments;

    public PagerAdapter(FragmentManager fm, List<Fragment> fragments) {
        super(fm);
        // TODO Auto-generated constructor stub
        this.fragments = fragments;
    }

    @Override
    public Fragment getItem(int arg0) {
        // TODO Auto-generated method stub

        return this.fragments.get(arg0);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return this.fragments.size();
    }

}

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

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</LinearLayout>

我想MainMenuActivity中的pager.setAdapter(mPagerAdapter)存在问题,但我不知道如何解决它。

logcat的:

11-28 13:07:46.040: E/AndroidRuntime(1879): FATAL EXCEPTION: main
11-28 13:07:46.040: E/AndroidRuntime(1879): java.lang.RuntimeException: Unable to start activity ComponentInfo{***********}: java.lang.NullPointerException
11-28 13:07:46.040: E/AndroidRuntime(1879):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-28 13:07:46.040: E/AndroidRuntime(1879):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-28 13:07:46.040: E/AndroidRuntime(1879):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-28 13:07:46.040: E/AndroidRuntime(1879):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-28 13:07:46.040: E/AndroidRuntime(1879):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-28 13:07:46.040: E/AndroidRuntime(1879):     at android.os.Looper.loop(Looper.java:137)
11-28 13:07:46.040: E/AndroidRuntime(1879):     at android.app.ActivityThread.main(ActivityThread.java:5103)
11-28 13:07:46.040: E/AndroidRuntime(1879):     at java.lang.reflect.Method.invokeNative(Native Method)
11-28 13:07:46.040: E/AndroidRuntime(1879):     at java.lang.reflect.Method.invoke(Method.java:525)
11-28 13:07:46.040: E/AndroidRuntime(1879):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-28 13:07:46.040: E/AndroidRuntime(1879):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-28 13:07:46.040: E/AndroidRuntime(1879):     at dalvik.system.NativeStart.main(Native Method)
11-28 13:07:46.040: E/AndroidRuntime(1879): Caused by: java.lang.NullPointerException
11-28 13:07:46.040: E/AndroidRuntime(1879):     at *******.MainMenuActivity.onCreate(MainMenuActivity.java:34)
11-28 13:07:46.040: E/AndroidRuntime(1879):     at android.app.Activity.performCreate(Activity.java:5133)
11-28 13:07:46.040: E/AndroidRuntime(1879):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-28 13:07:46.040: E/AndroidRuntime(1879):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-28 13:07:46.040: E/AndroidRuntime(1879):     ... 11 more

我希望有人可以帮助我......谢谢!!!

1 个答案:

答案 0 :(得分:1)

在您的活动中,您正在调用setContentView(R.layout.activity_main_menu),但包含ViewPager的布局称为viewpager_layout.xml

尝试将该行更改为setContentView(R.layout.viewpager_layout)