Android のナビゲーション ドロワー メニューを適切に使用する方法を学ぶために、以下のチュートリアルに従っています。
http://gmariotti.blogspot.ca/2013/05/creating-navigation-drawer.html https://github.com/gabrielemariotti/androiddev/tree/master/NavigationDrawer
ただし、arrays.xml の配列「ns_menu_items」内の NavigationDrawer メニューの項目の名前を変更しようとしています。
名前を変更するとアプリケーションを実行しようとすると、サンプル アプリケーションがクラッシュし続けるため、MainMenu がこれらのメニュー項目名を ID に変換する方法がわかりません。
配列.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="ns_menu_items">
<item>ns_menu_snippet1</item>
<item>ns_menu_snippet2</item>
<item>ns_menu_snippet3</item>
<item>ns_menu_snippet4</item>
<item>ns_menu_snippet5</item>
<item>ns_menu_snippet6</item>
<item>ns_menu_snippet7</item>
</string-array>
<array name="ns_menu_items_icon">
<item>ic_action_settings</item>
<item>ic_save</item>
<item>action_add</item>
<item>ic_launcher_</item>
<item>action_add</item>
<item>ic_action_settings</item>
<item>ic_launcher_</item>
</array>
</resources>
MainActivity.java
public class MainActivity extends Activity {
private ListView mDrawerList;
private DrawerLayout mDrawer;
private CustomActionBarDrawerToggle mDrawerToggle;
private String[] menuItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_drawer);
// enable ActionBar app icon to behave as action to toggle nav drawer
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
// set a custom shadow that overlays the main content when the drawer
// opens
mDrawer.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
_initMenu();
mDrawerToggle = new CustomActionBarDrawerToggle(this, mDrawer);
mDrawer.setDrawerListener(mDrawerToggle);
}
private void _initMenu() {
NsMenuAdapter mAdapter = new NsMenuAdapter(this);
// Add Header
mAdapter.addHeader(R.string.ns_menu_main_header);
// Add first block
menuItems = getResources().getStringArray(
R.array.ns_menu_items);
String[] menuItemsIcon = getResources().getStringArray(
R.array.ns_menu_items_icon);
int res = 0;
for (String item : menuItems) {
int id_title = getResources().getIdentifier(item, "string",
this.getPackageName());
int id_icon = getResources().getIdentifier(menuItemsIcon[res],
"drawable", this.getPackageName());
NsMenuItemModel mItem = new NsMenuItemModel(id_title, id_icon);
if (res==1) mItem.counter=12; //it is just an example...
if (res==3) mItem.counter=3; //it is just an example...
mAdapter.addItem(mItem);
res++;
}
mAdapter.addHeader(R.string.ns_menu_main_header2);
mDrawerList = (ListView) findViewById(R.id.drawer);
if (mDrawerList != null)
mDrawerList.setAdapter(mAdapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
/* Called whenever we call invalidateOptionsMenu() */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
boolean drawerOpen = mDrawer.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_save).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
/*
* The action bar home/up should open or close the drawer.
* ActionBarDrawerToggle will take care of this.
*/
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle your other action bar items...
return super.onOptionsItemSelected(item);
}
private class CustomActionBarDrawerToggle extends ActionBarDrawerToggle {
public CustomActionBarDrawerToggle(Activity mActivity,DrawerLayout mDrawerLayout){
super(
mActivity,
mDrawerLayout,
R.drawable.ic_drawer,
R.string.ns_menu_open,
R.string.ns_menu_close);
}
@Override
public void onDrawerClosed(View view) {
getActionBar().setTitle(getString(R.string.ns_menu_close));
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
@Override
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(getString(R.string.ns_menu_open));
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// Highlight the selected item, update the title, and close the drawer
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
String text= "menu click... should be implemented";
Toast.makeText(MainActivity.this, text , Toast.LENGTH_LONG).show();
//You should reset item counter
mDrawer.closeDrawer(mDrawerList);
}
}
}
NsMenuAdapter.java
public class NsMenuAdapter extends ArrayAdapter<NsMenuItemModel> {
/*
* public NsMenuAdapter(Context context, int resource, int
* textViewResourceId, String[] objects) { super(context,
* R.layout.ns_menu_row, textViewResourceId, objects); }
*/
public NsMenuAdapter(Context context) {
super(context, 0);
}
public void addHeader(int title) {
add(new NsMenuItemModel(title, -1, true));
}
public void addItem(int title, int icon) {
add(new NsMenuItemModel(title, icon, false));
}
public void addItem(NsMenuItemModel itemModel) {
add(itemModel);
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
return getItem(position).isHeader ? 0 : 1;
}
@Override
public boolean isEnabled(int position) {
return !getItem(position).isHeader;
}
public static class ViewHolder {
public final TextView textHolder;
public final ImageView imageHolder;
public final TextView textCounterHolder;
public ViewHolder(TextView text1, ImageView image1,TextView textcounter1) {
this.textHolder = text1;
this.imageHolder = image1;
this.textCounterHolder=textcounter1;
}
}
public View getView(int position, View convertView, ViewGroup parent) {
NsMenuItemModel item = getItem(position);
ViewHolder holder = null;
View view = convertView;
if (view == null) {
int layout = R.layout.ns_menu_row_counter;
if (item.isHeader)
layout = R.layout.ns_menu_row_header;
view = LayoutInflater.from(getContext()).inflate(layout, null);
TextView text1 = (TextView) view.findViewById(R.id.menurow_title);
ImageView image1 = (ImageView) view.findViewById(R.id.menurow_icon);
TextView textcounter1 = (TextView) view.findViewById(R.id.menurow_counter);
view.setTag(new ViewHolder(text1, image1,textcounter1));
}
if (holder == null && view != null) {
Object tag = view.getTag();
if (tag instanceof ViewHolder) {
holder = (ViewHolder) tag;
}
}
if(item != null && holder != null)
{
if (holder.textHolder != null)
holder.textHolder.setText(item.title);
if (holder.textCounterHolder != null){
if (item.counter > 0){
holder.textCounterHolder.setVisibility(View.VISIBLE);
holder.textCounterHolder.setText(""+item.counter);
}else{
holder.textCounterHolder.setVisibility(View.GONE);
}
}
if (holder.imageHolder != null) {
if (item.iconRes > 0) {
holder.imageHolder.setVisibility(View.VISIBLE);
holder.imageHolder.setImageResource(item.iconRes);
} else {
holder.imageHolder.setVisibility(View.GONE);
}
}
}
return view;
}
}
NsMenuItemModel.java
public class NsMenuItemModel {
public int title;
public int iconRes;
public int counter;
public boolean isHeader;
public NsMenuItemModel(int title, int iconRes,boolean header,int counter) {
this.title = title;
this.iconRes = iconRes;
this.isHeader=header;
this.counter=counter;
}
public NsMenuItemModel(int title, int iconRes,boolean header){
this(title,iconRes,header,0);
}
public NsMenuItemModel(int title, int iconRes) {
this(title,iconRes,false);
}
}
Logcat エラー:
11-03 23:20:09.124: E/AndroidRuntime(801): FATAL EXCEPTION: main
11-03 23:20:09.124: E/AndroidRuntime(801): android.content.res.Resources$NotFoundException: String resource ID #0x0
11-03 23:20:09.124: E/AndroidRuntime(801): at android.content.res.Resources.getText(Resources.java:239)
11-03 23:20:09.124: E/AndroidRuntime(801): at android.widget.TextView.setText(TextView.java:3844)
11-03 23:20:09.124: E/AndroidRuntime(801): at com.example.navtest.MenuAdapter.getView(MenuAdapter.java:93)
11-03 23:20:09.124: E/AndroidRuntime(801): at android.widget.AbsListView.obtainView(AbsListView.java:2177)
11-03 23:20:09.124: E/AndroidRuntime(801): at android.widget.ListView.makeAndAddView(ListView.java:1840)
11-03 23:20:09.124: E/AndroidRuntime(801): at android.widget.ListView.fillDown(ListView.java:675)
11-03 23:20:09.124: E/AndroidRuntime(801): at android.widget.ListView.fillFromTop(ListView.java:736)
11-03 23:20:09.124: E/AndroidRuntime(801): at android.widget.ListView.layoutChildren(ListView.java:1655)
11-03 23:20:09.124: E/AndroidRuntime(801): at android.widget.AbsListView.onLayout(AbsListView.java:2012)
11-03 23:20:09.124: E/AndroidRuntime(801): at android.view.View.layout(View.java:14289)
11-03 23:20:09.124: E/AndroidRuntime(801): at android.view.ViewGroup.layout(ViewGroup.java:4562)
11-03 23:20:09.124: E/AndroidRuntime(801): at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:702)
11-03 23:20:09.124: E/AndroidRuntime(801): at android.view.View.layout(View.java:14289)
11-03 23:20:09.124: E/AndroidRuntime(801): at android.view.ViewGroup.layout(ViewGroup.java:4562)
11-03 23:20:09.124: E/AndroidRuntime(801): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
11-03 23:20:09.124: E/AndroidRuntime(801): at android.view.View.layout(View.java:14289)
11-03 23:20:09.124: E/AndroidRuntime(801): at android.view.ViewGroup.layout(ViewGroup.java:4562)
11-03 23:20:09.124: E/AndroidRuntime(801): at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:349)
11-03 23:20:09.124: E/AndroidRuntime(801): at android.view.View.layout(View.java:14289)
11-03 23:20:09.124: E/AndroidRuntime(801): at android.view.ViewGroup.layout(ViewGroup.java:4562)
11-03 23:20:09.124: E/AndroidRuntime(801): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
11-03 23:20:09.124: E/AndroidRuntime(801): at android.view.View.layout(View.java:14289)
11-03 23:20:09.124: E/AndroidRuntime(801): at android.view.ViewGroup.layout(ViewGroup.java:4562)
11-03 23:20:09.124: E/AndroidRuntime(801): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976)
11-03 23:20:09.124: E/AndroidRuntime(801): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
11-03 23:20:09.124: E/AndroidRuntime(801): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
11-03 23:20:09.124: E/AndroidRuntime(801): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
11-03 23:20:09.124: E/AndroidRuntime(801): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
11-03 23:20:09.124: E/AndroidRuntime(801): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
11-03 23:20:09.124: E/AndroidRuntime(801): at android.view.Choreographer.doFrame(Choreographer.java:532)
11-03 23:20:09.124: E/AndroidRuntime(801): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
11-03 23:20:09.124: E/AndroidRuntime(801): at android.os.Handler.handleCallback(Handler.java:730)
11-03 23:20:09.124: E/AndroidRuntime(801): at android.os.Handler.dispatchMessage(Handler.java:92)
11-03 23:20:09.124: E/AndroidRuntime(801): at android.os.Looper.loop(Looper.java:137)
11-03 23:20:09.124: E/AndroidRuntime(801): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-03 23:20:09.124: E/AndroidRuntime(801): at java.lang.reflect.Method.invokeNative(Native Method)
11-03 23:20:09.124: E/AndroidRuntime(801): at java.lang.reflect.Method.invoke(Method.java:525)
11-03 23:20:09.124: E/AndroidRuntime(801): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-03 23:20:09.124: E/AndroidRuntime(801): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-03 23:20:09.124: E/AndroidRuntime(801): at dalvik.system.NativeStart.main(Native Method)
誰かが私にこれを説明できますか?ありがとう