2015-10-21 72 views
-1

這是onCreate,它給mDrawerList.setOnItemClickListener(this)NPE 我只想創建列表菜單與國家圖標和文本,如:俄語,英語,意大利語。當用戶點擊它時,它必須更改App的語言環境語言。 我以爲使用DrawerNavigation而不是將所有不同的語言添加到OverFlowMenu。mDrawerList.setOnItemClickListener NPE

@SuppressLint("NewApi") 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(activity_main); 

      /** 
      * 
      * 
      * 
      * 
      */ 

      mTitle = mDrawerTitle = getTitle(); 

      // load slide menu items 
      navMenuTitles = getResources().getStringArray(nav_drawer_items); 

      // nav drawer icons from resources 
      navMenuIcons = getResources() 
        .obtainTypedArray(nav_drawer_icons); 

      mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 

      mDrawerList = (ListView) findViewById(list_slidermenu); 

      navDrawerItems = new ArrayList<>(); 

      // adding nav drawer items to array 
      // English language 
      navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1))); 
      // Russian language 
      navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1))); 
      // Italian language 
      navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1))); 


      // Recycle the typed array 
      navMenuIcons.recycle(); 

      mDrawerList.setOnItemClickListener(this); 

      // setting the nav drawer list adapter 
      adapter = new NavDrawerListAdapter(getApplicationContext(), 
        navDrawerItems); 
      mDrawerList.setAdapter(adapter); 

      // enabling action bar app icon and behaving it as toggle button 
      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) { 
       // on first time display view for first nav item 
       displayView(0); 
      }*/ 
      /** 
      * 
      * 
      * 
      * 
      */ 


      storyButtonMainBody = (Button) findViewById(storyButton); 
      storyButtonMainBody.setOnClickListener(this); 

      specSectButton = (Button) findViewById(specSectionButtonId); 
      specSectButton.setOnClickListener(this); 

      webRegistryButton = (Button) findViewById(e_registryId); 
      webRegistryButton.setOnClickListener(this); 

      feedBackButton = (Button) findViewById(feedback); 
      feedBackButton.setOnClickListener(this); 

      findUsButton = (Button) findViewById(findus); 
      findUsButton.setOnClickListener(this); 

      communicationButton = (Button) findViewById(R.id.button_show_comunications); 
      communicationButton.setOnClickListener(this); 
      makeActionOverflowMenuShown(); 
     } 

     /*private class SlideMenuClickListener implements 
       OnItemClickListener { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, 
            long id) { 
       // display view for selected nav drawer item 
       displayView(position); 
      } 
     }*/ 

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


     /** 
     * Diplaying fragment view for selected nav drawer list item 
     */ 
     private void displayView(int position) { 
      // update the main content by replacing fragments 
      Fragment fragment = null; 
      switch (position) { 
       case 0: 
        Locale locale = new Locale("en"); 
        Locale.setDefault(locale); 
        Configuration config = new Configuration(); 
        config.locale = locale; 
        getBaseContext() 
          .getResources() 
          .updateConfiguration(config, getBaseContext() 
            .getResources() 
            .getDisplayMetrics()); 
        restartActivity(); 
        Toast.makeText(this, R.string.locale_lang, LENGTH_LONG).show(); 
        break; 
       case 1: 
        Locale locale2 = new Locale("ru"); 
        Locale.setDefault(locale2); 
        Configuration config2 = new Configuration(); 
        config2.locale = locale2; 
        getBaseContext() 
          .getResources() 
          .updateConfiguration(config2, getBaseContext() 
            .getResources() 
            .getDisplayMetrics()); 
        restartActivity(); 
        Toast.makeText(this, R.string.locale_lang, LENGTH_LONG).show(); 
        break; 
       case 2: 
        Locale locale3 = new Locale("it"); 
        Locale.setDefault(locale3); 
        Configuration config3 = new Configuration(); 
        config3.locale = locale3; 
        getBaseContext() 
          .getResources() 
          .updateConfiguration(config3, getBaseContext() 
            .getResources() 
            .getDisplayMetrics()); 
        restartActivity(); 
        Toast.makeText(this, R.string.locale_lang, LENGTH_LONG).show(); 
        break; 
      } 

      if (fragment != null) { 

       FragmentManager fragmentManager = getFragmentManager(); 
       fragmentManager.beginTransaction() 
         .replace(R.id.frame_container, fragment).commit(); 

       // update selected item and title, then close the drawer 
       mDrawerList.setItemChecked(position, true); 
       mDrawerList.setSelection(position); 
       setTitle(navMenuTitles[position]); 
       mDrawerLayout.closeDrawer(mDrawerList); 
      } else { 
       // error in creating fragment 
       Log.e("MainActivity", "Error in creating fragment"); 
      } 
     } 
+0

發佈onCreate()方法的完整代碼(mDrawerList變量爲空) – bonnyz

+0

檢查導致問題的主要活動的第116行。 – Toppers

+0

是的,但它的線/ /一些評論 –

回答

0

在這條線

mDrawerList = (ListView) findViewById(list_slidermenu); 

你正在尋找不通過可變activity_main

標識佈局文件中存在的視圖(其ID存儲在list_slidermenu)檢查你如何分配activity_mainlist_slidermenu,它們必須是有效的android R標識符(像R.id.drawer_layout);如果沒有,這是問題所在。之後,打開佈局文件,檢查由list_slidermenu標識的項目。

+0

嗯......我發現這個問題,但另一個問題是: 我想添加navigationdrawer到我的存在的項目沒有改變contentview(activity_main); –

+0

@ArnoldMrARNOLDCharyyev這與你原來的問題完全無關(你問到NPE的例外情況)。 如果您解決了此問題,請標記此問題,然後,只有在確實有必要時,才能打開一個新的特定問題 – bonnyz

+0

瞭解。似乎這個問題得到解決,但在這個問題之後,我在mDrawerLayout.setDrawerListener(mDrawerToggle)上獲得了新的NPE; –