java.lang.NullPointerException进入SlidingMenu

时间:2015-01-19 11:47:14

标签: java android

这是我的MainActivity。

    public class MainActivity extends Activity {
    private DrawerLayout mDrawerLayout;
    private ListView mdListView;
    private ActionBarDrawerToggle mDrawerToggle;
    private CharSequence mdrawerTitle;
    private CharSequence mtitle;
    private String[] navManuTitles;
    private TypedArray navMenuIcons;
    private ArrayList<NavDrowerItem> navDrowerItems;
    private NavDrawerAdapter adapter;

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mtitle = mdrawerTitle = getTitle();
        navManuTitles = getResources().getStringArray(R.array.nav_drawer_items);
        navMenuIcons = getResources()
                .obtainTypedArray(R.array.nav_drawer_icons);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mdListView = (ListView) findViewById(R.id.list_slidermenu);
        navDrowerItems = new ArrayList<NavDrowerItem>();

        navDrowerItems.add(new NavDrowerItem(navManuTitles[0], navMenuIcons
                .getResourceId(0, -1)));
        navDrowerItems.add(new NavDrowerItem(navManuTitles[1], navMenuIcons
                .getResourceId(1, -1)));
        navDrowerItems.add(new NavDrowerItem(navManuTitles[2], navMenuIcons
                .getResourceId(2, -1)));
        navDrowerItems.add(new NavDrowerItem(navManuTitles[3], navMenuIcons
                .getResourceId(3, -1)));
        navDrowerItems.add(new NavDrowerItem(navManuTitles[4], navMenuIcons
                .getResourceId(4, -1)));
        navDrowerItems.add(new NavDrowerItem(navManuTitles[5], navMenuIcons
                .getResourceId(5, -1)));

        navMenuIcons.recycle();
        mdListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub

                displayView(position);
            }
        });
        adapter = new NavDrawerAdapter(getApplicationContext(), navDrowerItems);
        mdListView.setAdapter(adapter);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer, // nav menu toggle icon
                R.string.app_name, // nav drawer open - description for
                                    // accessibility
                R.string.app_name // nav drawer close - description for
                                    // accessibility
        ) {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(mtitle);
                // calling onPrepareOptionsMenu() to show action bar icons
                invalidateOptionsMenu();
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(mdrawerTitle);
                // calling onPrepareOptionsMenu() to hide action bar icons
                invalidateOptionsMenu();
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);
        if (savedInstanceState == null) {
            displayView(0);
        }

    }

    private void displayView(int position) {

        Fragment fragment = null;
        switch (position) {
        case 0:
            fragment = new Home();
            break;

        case 1:

            fragment = new FindPeopleFragment();
            break;
        case 2:
            fragment = new Photos();
            break;
        case 3:
            fragment = new Community();
            break;
        case 4:
            fragment = new PageFragment();
            break;
        case 5:
            fragment = new WhatsHotFragment();
            break;

        }

        if (fragment != null) {
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.frame_container, fragment).commit();
            mdListView.setItemChecked(position, true);
            mdListView.setSelection(position);
            setTitle(navManuTitles[position]);
            mDrawerLayout.closeDrawer(mdListView);
        } else {
            Log.e("MainActivity", "Error in createing Fragment");
        }
    }

    @Override
    public void setTitle(CharSequence title) {
        mtitle = title;
        getActionBar().setTitle(mtitle);
    }

    @SuppressWarnings("deprecation")
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @SuppressWarnings("deprecation")
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggls
        mDrawerToggle.onConfigurationChanged(newConfig);
    }
}

这是我的Log cat Error

01-19 17:41:08.744: E/Trace(15240): error opening trace file: No such file or directory (2)

01-19 17:41:08.824: E/AndroidRuntime(15240): FATAL EXCEPTION: main
01-19 17:41:08.824: E/AndroidRuntime(15240): java.lang.RuntimeException: Unable to start activity 

ComponentInfo{com.example.menusliding/com.example.menusliding.MainActivity}: java.lang.NullPointerException

01-19 17:41:08.824: E/AndroidRuntime(15240):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2355)

01-19 17:41:08.824: E/AndroidRuntime(15240):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)

01-19 17:41:08.824: E/AndroidRuntime(15240):    at android.app.ActivityThread.access$600(ActivityThread.java:151)

01-19 17:41:08.824: E/AndroidRuntime(15240):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335)

01-19 17:41:08.824: E/AndroidRuntime(15240):    at android.os.Handler.dispatchMessage(Handler.java:99)

01-19 17:41:08.824: E/AndroidRuntime(15240):    at android.os.Looper.loop(Looper.java:155)

01-19 17:41:08.824: E/AndroidRuntime(15240):    at android.app.ActivityThread.main(ActivityThread.java:5511)

01-19 17:41:08.824: E/AndroidRuntime(15240):    at java.lang.reflect.Method.invokeNative(Native Method)

01-19 17:41:08.824: E/AndroidRuntime(15240):    at java.lang.reflect.Method.invoke(Method.java:511)

01-19 17:41:08.824: E/AndroidRuntime(15240):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)

当我运行我的android项目时,它表明它已经停止了它的不幸。 在我的Log cat中显示了此错误。 但我没有在我的代码中发现问题。 有什么问题。

0 个答案:

没有答案