0

单击我的列表视图项时,下一个活动将不会启动,并且我收到“java.lang.IndexOutOfBoundsException”错误。logcat 指向第 131 行(我认为),但我不确定要进行哪些更改。我的列表视图应该按字母顺序显示我的数据库中的项目,但是当将项目输入我的数据库时,我没有看到我的列表视图中显示的名称列。我认为这可能与我收到的错误有关。在我用内容提供者实现了一个游标加载器之前,这一直很好。

日志猫:

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)

列表显示:

 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. }
4

2 回答 2

1

很明显:

LoginDetails clickedObject = loginArrayList.get(arg2);

该行假设arg2小于loginArrayList.size()-1

JVM 告诉你它不是,不管你的假设是什么。

您可以通过查看将该值传递arg2给您的方法的代码来轻松纠正此问题。它的逻辑很糟糕。

我还建议在args2List. 您还需要考虑如果您获得了错误的价值,您将返回什么。null 合适吗?你应该抛出某种异常吗?

于 2013-03-25T17:37:39.400 回答
0

您说populateList() 应该在单击项目时调用,但实际上从未在您发布的代码中调用它。它是唯一LoginDetails添加any 的地方loginArrayList。由于从不调用该函数,loginArrayList 始终为空,因此在第 131 行调用时,大小为 0部分。java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0LoginDetails clickedObject = loginArrayList.get(arg2);

于 2013-03-25T20:35:40.780 回答