gpt4 book ai didi

android - android:array索引超出范围的异常

转载 作者:行者123 更新时间:2023-12-03 17:48:35 25 4
gpt4 key购买 nike

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.app.ActionBarDrawerToggle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends Activity {
private DrawerLayout drawerLayout;
private ListView lvSlidingMenu;
private ActionBarDrawerToggle drawerToggle; // Navigation Drawer titles
private CharSequence drawerTitle;
private CharSequence appTitle;

// Sliding Menu items
private String[] titles;
private TypedArray icons;
private ArrayList slidingMenuItems;
private SlidingMenuAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
appTitle = drawerTitle = getTitle();

// Load resources
titles = getResources().getStringArray(R.array.nav_drawer_items);
icons = getResources().obtainTypedArray(R.array.nav_drawer_icons);

// Get Sliding Menu ListView istance
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
lvSlidingMenu = (ListView) findViewById(R.id.lv_sliding_menu);
slidingMenuItems = new ArrayList();

// Creating and Adding SlidingMenuItems
slidingMenuItems.add(new SlidingMenuItem(titles[0], icons.getResourceId(0,-1)));
slidingMenuItems.add(new SlidingMenuItem(titles[1], icons.getResourceId(1, -1)));
slidingMenuItems.add(new SlidingMenuItem(titles[2], icons.getResourceId(2, -1)));
slidingMenuItems.add(new SlidingMenuItem(titles[3], icons.getResourceId(3, -1)));

// Recycle the typed array
icons.recycle();
lvSlidingMenu.setOnItemClickListener(new SlideMenuClickListener());

// Assign adapter to listview
adapter = new SlidingMenuAdapter(getApplicationContext(), slidingMenuItems);
lvSlidingMenu.setAdapter(adapter);

// Enable action bar app icon and behaving it as toggle button
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_drawer
// Navigation Drawer
, R.string.app_name,
//Navigation Drawer open - description for accessibility
R.string.app_name)
// Navigation Drawer close - description for accessibility
{
public void onDrawerClosed(View view) {
getActionBar().setTitle(appTitle);
invalidateOptionsMenu();
}

public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(drawerTitle);
invalidateOptionsMenu();
}
}; drawerLayout.setDrawerListener(drawerToggle);
if (savedInstanceState == null) {
// On first time, show Home Fragment
displayView(0);
}
}

/**
* Slide menu item click listener *
*/
private class SlideMenuClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Display appropriate fragment for selected item
displayView(position);
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Toggle Navigation Drawer on selecting action bar app icon/title
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
} /* * * Called when invalidateOptionsMenu() is triggered */

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If Navigation Drawer is opened, hide the action items
boolean drawerOpen = drawerLayout.isDrawerOpen(lvSlidingMenu);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}

/**
* Display fragment view for selected Navigation Drawer list item *
*/
private void displayView(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;

default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fragment_detail, fragment).commit();

// Update selected item and title, then close the drawer
lvSlidingMenu.setItemChecked(position, true);
lvSlidingMenu.setSelection(position);
setTitle(titles[position]);
drawerLayout.closeDrawer(lvSlidingMenu);
} else {
// Log error
Log.e("MainActivity", "Error in creating fragment");
}
}

@Override
public void setTitle(CharSequence title) {
appTitle = title;
getActionBar().setTitle(appTitle);
}

/**
* When using the ActionBarDrawerToggle, you must call it during * onPostCreate() and onConfigurationChanged()...
*/
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
drawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
drawerToggle.onConfigurationChanged(newConfig);
}
}

这是我的MainActivty.java
public class SlidingMenuAdapter extends BaseAdapter {

private Context context;
private ArrayList<SlidingMenuItem> items;

public SlidingMenuAdapter(Context context, ArrayList<SlidingMenuItem> items) {
this.context = context;
this.items = items;
}

@Override
public int getCount() {
return items.size();
}

@Override
public Object getItem(int index) {
return items.get(index);
}

@Override
public long getItemId(int index) {
return index;
}

@Override
public View getView(int index, View view, ViewGroup arg2) {
if (view == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
view = mInflater.inflate(R.layout.iv_item_sliding_menu, null);
}

ImageView imgIcon = (ImageView) view.findViewById(R.id.img_sliding_menu_item);
TextView txtTitle = (TextView) view.findViewById(R.id.tv_sliding_menu_item);

SlidingMenuItem item = items.get(index);

imgIcon.setImageResource(item.getIcon());
txtTitle.setText(item.getTitle());

return view;
}

}

这是我的adapter.java文件
public class SlidingMenuItem {
String title;
int icon;

public SlidingMenuItem(String title, int icon) {
this.title = title;
this.icon = icon;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public int getIcon() {
return icon;
}

public void setIcon(int icon) {
this.icon = icon;
}

}

这是我的菜单项
<resources>
<string name="app_name" translatable="false">Fb slid</string>

<string name="hello_world" translatable="false">Hello world!</string>
<string name="action_settings" translatable="false">Settings</string>
<string name="title_activity_main" translatable="false">MainActivity
</string>


<!-- Sliding Menu items -->
<string-array name="nav_drawer_items">
<item >Home</item>
<item >Notifications</item>
<item >Settings</item>
<item >About</item>
</string-array>

<!-- Sliding Menu item icons -->
<array name="nav_drawer_icons">
<item>@drawable/home</item>
<item>@drawable/notifications</item>
<item>@drawable/settings</item>
<item>@drawable/about</item>
</array>

</resources>

这是我的string.xml文件

日志猫:
10-03 11:02:06.217    6956-6956/com.example.first.fbslid E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.first.fbslid/com.example.first.fbslid.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2146)
at android.app.ActivityThread.access$700(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:177)
at android.app.ActivityThread.main(ActivityThread.java:4947)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
at com.example.first.fbslid.MainActivity.onCreate(MainActivity.java:49)
at android.app.Activity.performCreate(Activity.java:5207)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2085)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2146)
            at android.app.ActivityThread.access$700(ActivityThread.java:140)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:177)
            at android.app.ActivityThread.main(ActivityThread.java:4947)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
            at dalvik.system.NativeStart.main(Native Method).

我不知道哪里有错误,因为它是android的新功能,并且正在尝试创建类似facebook的滑动菜单。有人可以帮我解决

最佳答案

我认为是

<!-- Sliding Menu items -->

它为空,然后当您尝试读取菜单项数组时失败
   titles = getResources().getStringArray(R.array.nav_drawer_items);

关于android - android:array索引超出范围的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32919336/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com