导航抽屉的片段上的ArrayIndexOutOfBounds异常

时间:2014-08-20 18:31:46

标签: android

我的某个Android活动中有一个导航抽屉。导航抽屉中的行将通过以下代码动态添加:

ObjectDrawerItem[] drawerItem = new ObjectDrawerItem[3];
drawerItem[0] = new ObjectDrawerItem(R.drawable.ic_speedometer, mNavigationDrawerItemTitles[0]);
drawerItem[1] = new ObjectDrawerItem(R.drawable.ic_place, mNavigationDrawerItemTitles[1]);
drawerItem[2] = new ObjectDrawerItem(R.drawable.ic_price, mNavigationDrawerItemTitles[2]);

到目前为止,这么好。我一直在使用onItemClick功能,通过以下方式实施ListView&#39} OnItemClickListener

Fragment fragment = null;
switch (position) {
case 0:
    // Code to load the first fragment.
    break;
case 1:
    // Code to load the second fragment.
    break;
case 2:
    // Code to load the third fragment.
    break;

它一直在正常工作。但是,当我在导航抽屉中添加headerView时,就像这样:

View headerView = View.inflate(this, R.layout.listview_item_row, null);
((ImageView) headerView.findViewById(R.id.imageViewIcon)).setImageDrawable(getResources().getDrawable(R.drawable.myimage));
((TextView) headerView.findViewById(R.id.textViewName)).setText("Header Row");

mDrawerList.addHeaderView(headerView);

并通过将片段加载向下移1来添加case 3:,Android给了我一个ArrayIndexOutOfBounds例外。 但是,case 0:(应该是headerview)正在正确加载片段。

帮助! Android新手在这里。

更新 我注意到一个陌生人的行为。我在代码中添加了case 3:。从技术上讲,它不应该执行,因为我只在drawerItem中包含了3个元素。但是,它执行AND然后给出ArrayIndexOutOfBounds异常。 参见:

case 3:
    // Code to load the fourth fragment.
    Log.d("Fragment", "In Case 3");
    break; 

更新2: 正如@hrishitiwari建议的那样,我添加了清除mDrawerList

的代码
private ListView mDrawerList;
mDrawerList = (ListView) findViewById(R.id.left_drawer);

left_drawer是:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <LinearLayout
            android:id="@+id/left_linear"
            android:layout_width="@dimen/navigation_drawer_width"
            android:layout_height="match_parent"
            android:layout_gravity="start">
        <ListView
            android:id="@+id/left_drawer"
            android:layout_width="@dimen/navigation_drawer_width"
            android:layout_height="match_parent"
            android:background="#111"
            android:choiceMode="singleChoice"
            android:divider="@color/navigation_divider"
            android:dividerHeight="1dp" />
    </LinearLayout>
</android.support.v4.widget.DrawerLayout>

更新3: logcat的相关部分,例外。

08-21 00:19:08.638: E/AndroidRuntime(954): FATAL EXCEPTION: main
08-21 00:19:08.638: E/AndroidRuntime(954): java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
08-21 00:19:08.638: E/AndroidRuntime(954):  at com.myproj.android.NavigationActivity.selectItem(NavigationActivity.java:231)
08-21 00:19:08.638: E/AndroidRuntime(954):  at com.myproj.android.NavigationActivity.access$0(NavigationActivity.java:201)
08-21 00:19:08.638: E/AndroidRuntime(954):  at com.myproj.android.NavigationActivity$DrawerItemClickListener.onItemClick(NavigationActivity.java:195)
08-21 00:19:08.638: E/AndroidRuntime(954):  at android.widget.AdapterView.performItemClick(AdapterView.java:292)
08-21 00:19:08.638: E/AndroidRuntime(954):  at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
08-21 00:19:08.638: E/AndroidRuntime(954):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
08-21 00:19:08.638: E/AndroidRuntime(954):  at android.widget.AbsListView$1.run(AbsListView.java:3168)
08-21 00:19:08.638: E/AndroidRuntime(954):  at android.os.Handler.handleCallback(Handler.java:605)
08-21 00:19:08.638: E/AndroidRuntime(954):  at android.os.Handler.dispatchMessage(Handler.java:92)
08-21 00:19:08.638: E/AndroidRuntime(954):  at android.os.Looper.loop(Looper.java:137)
08-21 00:19:08.638: E/AndroidRuntime(954):  at android.app.ActivityThread.main(ActivityThread.java:4424)
08-21 00:19:08.638: E/AndroidRuntime(954):  at java.lang.reflect.Method.invokeNative(Native Method)
08-21 00:19:08.638: E/AndroidRuntime(954):  at java.lang.reflect.Method.invoke(Method.java:511)
08-21 00:19:08.638: E/AndroidRuntime(954):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-21 00:19:08.638: E/AndroidRuntime(954):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)

0 个答案:

没有答案