单击列表视图项时,下一个活动将无法启动,并且收到“java.lang.IndexOutOfBoundsException”错误。 logcat指向131行(我认为),但我不确定要做什么更改。我的列表视图应该按字母顺序显示我的数据库中的项目,但是当我在数据库中输入项目时,我看不到列表视图中显示的名称colunm。我认为这可能与我收到的错误有关。在我使用内容提供程序实现游标加载器之前,这很好用。
logcat的:
03-18 10:05:23.439: E/AndroidRuntime(1286): FATAL EXCEPTION: main
03-18 10:05:23.439: E/AndroidRuntime(1286): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
03-18 10:05:23.439: E/AndroidRuntime(1286): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
03-18 10:05:23.439: E/AndroidRuntime(1286): at java.util.ArrayList.get(ArrayList.java:304)
03-18 10:05:23.439: E/AndroidRuntime(1286): at com.loginplus.home.LoginList.onItemClick(LoginList.java:131)
03-18 10:05:23.439: E/AndroidRuntime(1286): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
03-18 10:05:23.439: E/AndroidRuntime(1286): at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
03-18 10:05:23.439: E/AndroidRuntime(1286): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
03-18 10:05:23.439: E/AndroidRuntime(1286): at android.widget.AbsListView.onTouchEvent(AbsListView.java:3180)
03-18 10:05:23.439: E/AndroidRuntime(1286): at android.view.View.dispatchTouchEvent(View.java:5541)
03-18 10:05:23.439: E/AndroidRuntime(1286): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1951)
03-18 10:05:23.439: E/AndroidRuntime(1286): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1712)
03-18 10:05:23.439: E/AndroidRuntime(1286): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
03-18 10:05:23.439: E/AndroidRuntime(1286): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
03-18 10:05:23.439: E/AndroidRuntime(1286): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
03-18 10:05:23.439: E/AndroidRuntime(1286): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
03-18 10:05:23.439: E/AndroidRuntime(1286): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
03-18 10:05:23.439: E/AndroidRuntime(1286): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
03-18 10:05:23.439: E/AndroidRuntime(1286): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
03-18 10:05:23.439: E/AndroidRuntime(1286): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
03-18 10:05:23.439: E/AndroidRuntime(1286): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1912)
03-18 10:05:23.439: E/AndroidRuntime(1286): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1371)
03-18 10:05:23.439: E/AndroidRuntime(1286): at android.app.Activity.dispatchTouchEvent(Activity.java:2364)
03-18 10:05:23.439: E/AndroidRuntime(1286): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1860)
03-18 10:05:23.439: E/AndroidRuntime(1286): at android.view.View.dispatchPointerEvent(View.java:5721)
03-18 10:05:23.439: E/AndroidRuntime(1286): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:2890)
03-18 10:05:23.439: E/AndroidRuntime(1286): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2466)
03-18 10:05:23.439: E/AndroidRuntime(1286): at android.view.ViewRootImpl.processInputEvents(ViewRootImpl.java:845)
03-18 10:05:23.439: E/AndroidRuntime(1286): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:859)
03-18 10:05:23.439: E/AndroidRuntime(1286): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
03-18 10:05:23.439: E/AndroidRuntime(1286): at android.os.Handler.dispatchMessage(Handler.java:99)
03-18 10:05:23.439: E/AndroidRuntime(1286): at android.os.Looper.loop(Looper.java:137)
03-18 10:05:23.439: E/AndroidRuntime(1286): at android.app.ActivityThread.main(ActivityThread.java:4424)
03-18 10:05:23.439: E/AndroidRuntime(1286): at java.lang.reflect.Method.invokeNative(Native Method)
03-18 10:05:23.439: E/AndroidRuntime(1286): at java.lang.reflect.Method.invoke(Method.java:511)
03-18 10:05:23.439: E/AndroidRuntime(1286): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-18 10:05:23.439: E/AndroidRuntime(1286): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-18 10:05:23.439: E/AndroidRuntime(1286): at dalvik.system.NativeStart.main(Native Method)
的ListView:
39. public void onCreate(Bundle savedInstanceState) {
40. super.onCreate(savedInstanceState);
41.
42. setContentView(R.layout.login_listview);
43. getSupportLoaderManager().initLoader(LOADER_ID, null, this);
44.
45. String[] from = { BaseColumns._ID, dataStore.COLUMN_NAME_SITE};
46. int[] to = {R.id.rusName};
47.
48. adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, null, from, to);
49.
50.
51. loginList = (ListView)
52. findViewById(R.id.loginlist);
53. loginList.setOnItemClickListener(this);
54.
55. webLogin = (Button)
56. findViewById(R.id.button3);
57. webLogin.setOnClickListener(this);
58. }
59.
60. public Loader<Cursor> onCreateLoader(int id, Bundle args) {
61.
62. String[] projection = { BaseColumns._ID, dataStore.COLUMN_NAME_SITE};
63. CursorLoader cursorloader = new CursorLoader(this, ListProvider.CONTENT_URI, projection, null , null, null);
64. return cursorloader;
65. }
66.
67. public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
68. adapter.changeCursor(cursor);
69. }
70. public void onLoaderReset(Loader<Cursor> cursorLoader) {
71. adapter.changeCursor(null);
72. }
73.
74.
75. @Override
76. public void onClick (View v) {
77. Intent webLoginIntent = new Intent (this, LoginPlusActivity.class);
78. startActivity(webLoginIntent);
79. }
80.
81. public List<String> populateList (){
82.
83. List<String> webNameList = new ArrayList<String>();
84.
85. dataStore openHelperClass = new dataStore (this);
86.
87. SQLiteDatabase sqliteDatabase = openHelperClass.getReadableDatabase();
88.
89. Cursor cursor = sqliteDatabase.query(dataStore.TABLE_NAME_INFOTABLE, null, null, null, null, null, dataStore.COLUMN_NAME_SITE, null);
90.
91. while (cursor.moveToNext()){
92. String sName = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_SITE));
93. String wUrl = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_ADDRESS));
94. String uName = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_USERNAME));
95. String pWord = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_PASSWORD));
96. String lNotes = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_NOTES));
97.
98. LoginDetails lpDetails = new LoginDetails();
99. lpDetails.setsName(sName);
100. lpDetails.setwUrl(wUrl);
101. lpDetails.setuName(uName);
102. lpDetails.setpWord(pWord);
103. lpDetails.setlNotes(lNotes);
104.
105. loginArrayList.add(lpDetails);
106. webNameList.add(sName);
107. }
108.
109. cursor.close();
110. return webNameList;
111. }
112.
113.
114.
115. @Override
116. protected void onResume() {
117. super.onResume();
118.
119.
120. adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, null, new String[] { dataStore.COLUMN_NAME_SITE}, new int[]{R.id.loginlist});
121. loginList.setAdapter(adapter);
122. }
123.
124. @Override
125. public void onItemClick(AdapterView<?> arg0 , View arg1, int arg2, long arg3) {
126. Toast.makeText(getApplicationContext(), "Selected ID :" + arg2, Toast.LENGTH_SHORT).show();
127.
128. Intent updateDeleteLoginInfo = new Intent (this, UpdateDeleteLoginList.class);
129.
130.
131. LoginDetails clickedObject = loginArrayList.get(arg2);
132.
133. Bundle loginBundle = new Bundle();
134. loginBundle.putString("clickedWebSite",clickedObject.getsName());
135. loginBundle.putString("clickedWebAddress",clickedObject.getwUrl());
136. loginBundle.putString("clickedUserName",clickedObject.getuName());
137. loginBundle.putString("clickedPassWord",clickedObject.getpWord());
138. loginBundle.putString("clickedNotes",clickedObject.getlNotes());
139.
140. updateDeleteLoginInfo.putExtras(loginBundle);
141.
142. startActivityForResult(updateDeleteLoginInfo, 0);
143. }
答案 0 :(得分:1)
很明显:
LoginDetails clickedObject = loginArrayList.get(arg2);
此行假定arg2
小于loginArrayList.size()-1
。
JVM告诉你它不是,不管你的假设是什么。
您可以通过查看将arg2
的值传递给方法的代码来轻松纠正此问题。它的逻辑很糟糕。
我还建议您在检查args2
之前确保获得List
的有效值时保持警惕。如果你的价值不好,你也会想想你将会得到什么。 null是否合适?你应该抛出某种例外吗?
答案 1 :(得分:0)
您说在点击某个项目时应调用populateList()
,但在您发布的代码中从不实际。这是唯一将LoginDetails
添加到loginArrayList
的地方。由于函数永远不会被调用,因此loginArrayList始终为空,因此当您在第131行调用java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
时,大小为LoginDetails clickedObject = loginArrayList.get(arg2);
的部分。