这是onCreate,它在mDrawerList.setOnItemClickListener(this)上给出了NPE 我只想创建列表菜单,其中包含国家图标和文字,如:俄语,英语,意大利语。用户点击它时,必须更改App的Locale语言。 我想使用DrawerNavigation而不是将所有不同的语言添加到OverFlowMenu。
@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 :(得分:0)
在这一行
mDrawerList = (ListView) findViewById(list_slidermenu);
您正在寻找一个视图(其 id 存储在list_slidermenu
中),该视图在变量activity_main
标识的布局文件中不存在
检查你如何分配activity_main
和list_slidermenu
,它们必须是有效的android R标识符(如R.id.drawer_layout );如果没有,这就是问题所在。之后,打开布局文件,检查list_slidermenu
标识的项目。