我有一个滑动菜单应用程序,并且想要添加一个新的项目块。我添加了它,但是当我运行我的应用程序时崩溃,日志显示此错误,我无法弄清楚原因。非常感谢!!!
这是我的: 的 MainActivity.java
package com.cngcnasaud.orar;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends Activity implements OnClickListener {
private ListView mDrawerList;
private DrawerLayout mDrawer;
private CustomActionBarDrawerToggle mDrawerToggle;
private String[] menuItems;
private static final String TAG = "AudioDemo";
private static final String isPlaying = "Media is Playing";
private static final String notPlaying = "Media has stopped Playing";
MediaPlayer player;
Button playerButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_drawer);
player = MediaPlayer.create(this, R.raw.gc);
player.setLooping(false); // Set looping
// Get the button from the view
playerButton = (Button) this.findViewById(R.id.buttonmp);
playerButton.setText(R.string.play_label);
playerButton.setTextColor(Color.WHITE);
playerButton.setOnClickListener((OnClickListener) this);
// 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);
}
public void onClick(View v) {
Log.d(TAG, "onClick: " + v);
if (v.getId() == R.id.buttonmp) {
playPause();
}
}
private void demoPause() {
// TODO Auto-generated method stub
player.pause();
playerButton.setText(R.string.play_label);
Log.d(TAG, notPlaying);
}
private void playPause() {
// TODO Auto-generated method stub
if(player.isPlaying()) {
demoPause();
} else {
demoPlay();
}
}
private void demoPlay() {
// TODO Auto-generated method stub
player.start();
playerButton.setText(R.string.stop_label);
Log.d(TAG, isPlaying);
}
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=0; //it is just an example...
if (res==3) mItem.counter=0; //it is just an example...
mAdapter.addItem(mItem);
res++;
}
mAdapter.addHeader(R.string.ns_menu_main_header2);
menuItems = getResources().getStringArray(
R.array.ns_menu_items2);
String[] menuItemsIcon1 = getResources().getStringArray(
R.array.ns_menu_items_icon2);
int res2 = 0;
for (String item : menuItems) {
int id_title = getResources().getIdentifier(item, "string",
this.getPackageName());
int id_icon = getResources().getIdentifier(menuItemsIcon1[res2],
"drawable", this.getPackageName());
NsMenuItemModel mItem = new NsMenuItemModel(id_title, id_icon);
if (res2==1) mItem.counter=0; //it is just an example...
mAdapter.addItem(mItem);
res2++;
}
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 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) {
mDrawerList.setItemChecked(position, true);
mDrawer.closeDrawer(mDrawerList);
if (position == 1) {
Intent intent = new Intent(MainActivity.this, Istoric.class);
startActivity(intent);
mDrawer.closeDrawers();
}
else if (position == 2) {
Intent intent2 = new Intent(MainActivity.this, Academicieni.class);
startActivity(intent2);
mDrawer.closeDrawers();
}
if (position == 3) {
Intent intent3 = new Intent(MainActivity.this, Profesori.class);
startActivity(intent3);
mDrawer.closeDrawers();
}
if (position == 4) {
Intent intent4 = new Intent(MainActivity.this, Elevi.class);
startActivity(intent4);
mDrawer.closeDrawers();
}
if (position == 5) {
Intent intent5 = new Intent(MainActivity.this, ShowImageActivity.class);
startActivity(intent5);
mDrawer.closeDrawers();
}
if (position == 6) {
Intent intent6 = new Intent(MainActivity.this, Medii.class);
startActivity(intent6);
mDrawer.closeDrawers();
}
if (position == 7) {
Intent intent7 = new Intent(MainActivity.this, Despre.class);
startActivity(intent7);
mDrawer.closeDrawers();
}
if (position == 8) {
Intent intent8 = new Intent(MainActivity.this, Feedback.class);
startActivity(intent8);
mDrawer.closeDrawers();
}
}
}
}
logcat的
03-18 20:44:45.243: E/AndroidRuntime(1273): FATAL EXCEPTION: main
03-18 20:44:45.243: E/AndroidRuntime(1273): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cngcnasaud.orar/com.cngcnasaud.orar.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
03-18 20:44:45.243: E/AndroidRuntime(1273): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-18 20:44:45.243: E/AndroidRuntime(1273): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-18 20:44:45.243: E/AndroidRuntime(1273): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-18 20:44:45.243: E/AndroidRuntime(1273): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-18 20:44:45.243: E/AndroidRuntime(1273): at android.os.Handler.dispatchMessage(Handler.java:99)
03-18 20:44:45.243: E/AndroidRuntime(1273): at android.os.Looper.loop(Looper.java:137)
03-18 20:44:45.243: E/AndroidRuntime(1273): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-18 20:44:45.243: E/AndroidRuntime(1273): at java.lang.reflect.Method.invokeNative(Native Method)
03-18 20:44:45.243: E/AndroidRuntime(1273): at java.lang.reflect.Method.invoke(Method.java:511)
03-18 20:44:45.243: E/AndroidRuntime(1273): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-18 20:44:45.243: E/AndroidRuntime(1273): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-18 20:44:45.243: E/AndroidRuntime(1273): at dalvik.system.NativeStart.main(Native Method)
03-18 20:44:45.243: E/AndroidRuntime(1273): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
03-18 20:44:45.243: E/AndroidRuntime(1273): at com.cngcnasaud.orar.MainActivity._initMenu(MainActivity.java:160)
03-18 20:44:45.243: E/AndroidRuntime(1273): at com.cngcnasaud.orar.MainActivity.onCreate(MainActivity.java:72)
03-18 20:44:45.243: E/AndroidRuntime(1273): at android.app.Activity.performCreate(Activity.java:5104)
03-18 20:44:45.243: E/AndroidRuntime(1273): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-18 20:44:45.243: E/AndroidRuntime(1273): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-18 20:44:45.243: E/AndroidRuntime(1273): ... 11 more
谢谢!!!
编辑:
<?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>
</string-array>
<string-array name="ns_menu_items2">
<item>ns_menu_snippet21</item>
<item>ns_menu_snippet22</item>
</string-array>
<array name="ns_menu_items_icon">
<item>istorici</item>
<item>academicienii</item>
<item>orari</item>
<item>orari</item>
<item>orari</item>
<item>orari</item>
</array>
<array name="ns_menu_items_icon2">
<item>desprei</item>
<item>feedbacki</item>
</array>
<array name="ns_menu_items_icon2">
<item>ic_action_settings</item>
</array>
</resources>
答案 0 :(得分:1)
我认为
String[] menuItemsIcon2 = getResources().getStringArray(
R.array.ns_menu_items_icon2);
int res2 = 0;
for (String item : menuItems) {
应该是
String[] menuItemsIcon2 = getResources().getStringArray(
R.array.ns_menu_items_icon2);
int res2 = 0;
for (String item : menuItemsIcon2) {
在每个循环的第二个循环中,您希望循环遍历menuItemsIcon2
数组的项目,menuItems
似乎有更多menuItemsIcon2
元素。
由于您在每次迭代时都会递增res2
,因此您将超出menuItemsIcon2
的范围。
答案 1 :(得分:0)
检查menuitems和iconmenuitems中是否有相同数量的元素。这可能是错误的来源。