我正在尝试调用一个调用列表的类。选中时从我的原始列表项。所以列表中的列表非常多,而且我从弹跳错误中得到一个数组。 logcat显示的错误是ArrayIndexOutOfBoundsException。
我最初只希望我的列表值从另一个列表中调用更多值。我知道可能有一种更简单的方法可以做到这一点,但我还没有偶然发现它。 这个cerealBarApps类是我试图为此做的27个类中的一个。
package com.cerealBarApps;
import android.app.ListActivity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class HelloListViewActivity extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle cerealBarApps) {
super.onCreate(cerealBarApps);
final String[] DepartmentNames = { "a",
"b", "c",
"d", "e",
"f",
"g", "h",
"i", "j",
"k", "l", "m", "n",
"o",
"o",
"q", "r",
"s" };
String[] departments = getResources().getStringArray(
R.array.departments_array);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,
departments));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String openDepartment = DepartmentNames[position];
try {
Class selected = Class.forName("com.cerealBarApps"
+ openDepartment);
Intent selectedIntent = new Intent(/*this, selected*/);
startActivity(selectedIntent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
});
};
};
我试图调用的类是AboutCerealBarApps
package com.cerealBarApps;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
import android.widget.Toast;
public class AboutCerealBarApps extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.list_item);
String[] cerealBarApps = getResources().getStringArray(
R.array.cerealBarApps_array);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,
cerealBarApps));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
};
};
My String.xml是我存储数组值的地方。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, HelloListViewActivity!</string>
<string name="app_name">HelloListView</string>
<string-array name="departments_array">
<item>B</item>
<item>A(AFROTC)</item>
<item>O</item>
<item>H</item>
<item>C</item>
<item>E</item>
<item>M</item>
<item>P</item>
<item>E</item>
<item>M</item>
<item>T</item>
<item>P</item>
<item>B</item>
<item>H</item>
<item>E</item>
<item>R</item>
<item>A</item>
<item>M</item>
<item>A</item>
<item>C</item>
<item>I</item>
<item>I</item>
<item>M</item>
<item>About Cereal Bar Apps</item>
</string-array>
<string-array name="cerealBarApps_array">
<item>Ex</item>
<item>xi</item>
<item>An App Within an App</item>
<item>BADUMM!!!!!</item>
<item>In Your Soup</item>
<item>GGWP</item>
<item>Ebenezer Ackon</item>
<item>Ernest Grzybowski</item>
<item>An App Within an App</item>
<item>BADUMM!!!!!</item>
<item>In Your Soup</item>
<item>GGWP</item>
<item>Ex</item>
<item>xki</item>
<item>An App Within an App</item>
<item>BADUMM!!!!!</item>
<item>In Your Soup</item>
<item>GGWP</item>
<item>Exn</item>
<item>Exi</item>
<item>An App Within an App</item>
<item>BADUMM!!!!!</item>
<item>In Your Soup</item>
<item>GGWP</item>
</string-array>
</resources>
我的Logcat
12-17 12:48:26.072: E/AndroidRuntime(229): Uncaught handler: thread main exiting due to uncaught exception
12-17 12:48:26.082: E/AndroidRuntime(229): java.lang.ArrayIndexOutOfBoundsException
12-17 12:48:26.082: E/AndroidRuntime(229): at com.cerealBarApps.HelloListViewActivity$1.onItemClick(HelloListViewActivity.java:43)
12-17 12:48:26.082: E/AndroidRuntime(229): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
12-17 12:48:26.082: E/AndroidRuntime(229): at android.widget.ListView.performItemClick(ListView.java:3285)
12-17 12:48:26.082: E/AndroidRuntime(229): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1640)
12-17 12:48:26.082: E/AndroidRuntime(229): at android.os.Handler.handleCallback(Handler.java:587)
12-17 12:48:26.082: E/AndroidRuntime(229): at android.os.Handler.dispatchMessage(Handler.java:92)
12-17 12:48:26.082: E/AndroidRuntime(229): at android.os.Looper.loop(Looper.java:123)
12-17 12:48:26.082: E/AndroidRuntime(229): at android.app.ActivityThread.main(ActivityThread.java:4363)
12-17 12:48:26.082: E/AndroidRuntime(229): at java.lang.reflect.Method.invokeNative(Native Method)
12-17 12:48:26.082: E/AndroidRuntime(229): at java.lang.reflect.Method.invoke(Method.java:521)
12-17 12:48:26.082: E/AndroidRuntime(229): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-17 12:48:26.082: E/AndroidRuntime(229): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-17 12:48:26.082: E/AndroidRuntime(229): at dalvik.system.NativeStart.main(Native Method)
12-17 12:48:26.094: I/dalvikvm(229): threadid=7: reacting to signal 3
12-17 12:48:26.094: E/dalvikvm(229): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
这是第43-46行的代码 lv.setOnItemClickListener(new OnItemClickListener(){ public void onItemClick(AdapterView父视图,视图视图, int position,long id){ String openDepartment = DepartmentNames [position];
答案 0 :(得分:0)
将索引传递给不存在的数组时会发生ArrayIndexOutOfBounds异常。
如果我有一个数组{“One”,“two”,“three”},则有效索引将为0,1和2.
如果我有一个循环或方法函数试图像这样访问数组:
String string = myArray[-1] //ArrayIndexOutOfBoundsException
String string = myArray[0] //string="One"
String string = myArray[1] //string="two"
String string = myArray[2] //string="three"
String string = myArray[3] //ArrayIndexOutOfBoundsException
任何无效的索引都会传递此异常。