导航抽屉中的NPE

时间:2014-03-27 06:14:43

标签: android navigation drawer

我从Null Pointer ExceptionNavigationDrawer拨打包含MainActivity.javalogin.class)的课程时收到intent。在login中我正在执行登录检查,之后我在提交NavigationDrawer点击时调用包含button的类。

(如果我单独运行导航抽屉类,我的代码工作正常。)

错误: -

03-27 01:05:50.741: E/AndroidRuntime(3823): FATAL EXCEPTION: main
03-27 01:05:50.741: E/AndroidRuntime(3823): Process: com.sunmobileappnow.mobileappnow, PID: 3823
03-27 01:05:50.741: E/AndroidRuntime(3823): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sunmobileappnow.mobileappnow/com.sunmobileappnow.mobileappnow.MainActivity}: java.lang.NullPointerException
03-27 01:05:50.741: E/AndroidRuntime(3823):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
03-27 01:05:50.741: E/AndroidRuntime(3823):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
03-27 01:05:50.741: E/AndroidRuntime(3823):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
03-27 01:05:50.741: E/AndroidRuntime(3823):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
03-27 01:05:50.741: E/AndroidRuntime(3823):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-27 01:05:50.741: E/AndroidRuntime(3823):     at android.os.Looper.loop(Looper.java:137)
03-27 01:05:50.741: E/AndroidRuntime(3823):     at android.app.ActivityThread.main(ActivityThread.java:4998)
03-27 01:05:50.741: E/AndroidRuntime(3823):     at java.lang.reflect.Method.invokeNative(Native Method)
03-27 01:05:50.741: E/AndroidRuntime(3823):     at java.lang.reflect.Method.invoke(Method.java:515)
03-27 01:05:50.741: E/AndroidRuntime(3823):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-27 01:05:50.741: E/AndroidRuntime(3823):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-27 01:05:50.741: E/AndroidRuntime(3823):     at dalvik.system.NativeStart.main(Native Method)
03-27 01:05:50.741: E/AndroidRuntime(3823): Caused by: java.lang.NullPointerException
03-27 01:05:50.741: E/AndroidRuntime(3823):     at com.sunmobileappnow.mobileappnow.MainActivity.onCreate(MainActivity.java:93)
03-27 01:05:50.741: E/AndroidRuntime(3823):     at android.app.Activity.performCreate(Activity.java:5243)
03-27 01:05:50.741: E/AndroidRuntime(3823):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-27 01:05:50.741: E/AndroidRuntime(3823):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
03-27 01:05:50.741: E/AndroidRuntime(3823):     ... 11 more

这是我的代码: -

package com.sunmobileappnow.mobileappnow;
import com.sunmobile.navigationdrawer.adapter.NavDrawerListAdapter;
import com.sunmobile.navigationdrawer.model.NavDrawerItem;
import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;

import com.sunmobile.navigationdrawer.*;

public class MainActivity extends Activity {
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;


    private CharSequence mDrawerTitle;


    private CharSequence mTitle;


    private String[] navMenuTitles;
    private TypedArray navMenuIcons;

    private ArrayList<NavDrawerItem> navDrawerItems;
    private NavDrawerListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
        setContentView(R.layout.activity_main1);

        mTitle = mDrawerTitle = getTitle();


        navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);


        navMenuIcons = getResources()
                .obtainTypedArray(R.array.nav_drawer_icons);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.list_slidermenu);

        navDrawerItems = new ArrayList<NavDrawerItem>();



        navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));

        navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));

        navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));

        navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));

        navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
        // list item in slider at 6 LinkedIn details
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1)));

        navDrawerItems.add(new NavDrawerItem(navMenuTitles[6], navMenuIcons.getResourceId(6, -1)));

        navDrawerItems.add(new NavDrawerItem(navMenuTitles[7], navMenuIcons.getResourceId(7, -1)));

        navDrawerItems.add(new NavDrawerItem(navMenuTitles[8], navMenuIcons.getResourceId(8, -1)));



        navMenuIcons.recycle();

        mDrawerList.setOnItemClickListener(new SlideMenuClickListener());


        adapter = new NavDrawerListAdapter(getApplicationContext(),
                navDrawerItems);
        mDrawerList.setAdapter(adapter);

        // Enable action bar icon_luncher as toggle Home Button
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer, R.string.app_name, R.string.app_name) {

                    public void onDrawerClosed(View view) {
                        getActionBar().setTitle(mTitle);
                        invalidateOptionsMenu(); 
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(mDrawerTitle);
                    invalidateOptionsMenu();
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (savedInstanceState == null) {
              displayView(0);
        }
    }


    private class SlideMenuClickListener implements
            ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {

            displayView(position);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }

        switch (item.getItemId()) {
        case R.id.action_settings:
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    //called when invalidateOptionsMenu() invoke 

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // if Navigation drawer is opened, hide the action items
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
        menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }

    private void displayView(int position) {
        // update the main content with called Fragment
        Fragment fragment = null;
        switch (position) {
        case 0:
            fragment = new Fragment1SendJob();
            break;
        case 1:
            fragment = new Fragment2Facebook();
            break;
        case 2:
            fragment = new Fragment3Google();
            break;
        case 3:
            fragment = new Fragment4Apple();
            break;
        case 4:
            fragment = new Fragment5Microsoft();
            break;
        case 5:
            fragment = new Fragment6LinkedIn();
            break;

        default:
            break;
        }

        if (fragment != null) {
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
            mDrawerList.setItemChecked(position, true);
            mDrawerList.setSelection(position);
            setTitle(navMenuTitles[position]);
            mDrawerLayout.closeDrawer(mDrawerList);
        } else {

            Log.e("this is mainActivity", "Error in else case");
        }
    }

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


    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

}

0 个答案:

没有答案