0

當單擊我的列表視圖項目時,下一個活動將不會啓動,並且會收到NullPointerException錯誤。 logcat指向130行(我認爲),但我不知道做出什麼樣的改變。這工作得很好,直到我實現了一個帶有內容提供者的遊標加載程Listview java.lang.NullPointerException

的logcat:

03-18 09:52:20.737: E/AndroidRuntime(1143): FATAL EXCEPTION: main 
03-18 09:52:20.737: E/AndroidRuntime(1143): java.lang.NullPointerException 
03-18 09:52:20.737: E/AndroidRuntime(1143):  at com.loginplus.home.LoginList.onItemClick(LoginList.java:130) 
03-18 09:52:20.737: E/AndroidRuntime(1143):  at android.widget.AdapterView.performItemClick(AdapterView.java:292) 
03-18 09:52:20.737: E/AndroidRuntime(1143):  at android.widget.AbsListView.performItemClick(AbsListView.java:1058) 
03-18 09:52:20.737: E/AndroidRuntime(1143):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514) 
03-18 09:52:20.737: E/AndroidRuntime(1143):  at android.widget.AbsListView$1.run(AbsListView.java:3168) 
03-18 09:52:20.737: E/AndroidRuntime(1143):  at android.os.Handler.handleCallback(Handler.java:605) 
03-18 09:52:20.737: E/AndroidRuntime(1143):  at android.os.Handler.dispatchMessage(Handler.java:92) 
03-18 09:52:20.737: E/AndroidRuntime(1143):  at android.os.Looper.loop(Looper.java:137) 
03-18 09:52:20.737: E/AndroidRuntime(1143):  at android.app.ActivityThread.main(ActivityThread.java:4424) 
03-18 09:52:20.737: E/AndroidRuntime(1143):  at java.lang.reflect.Method.invokeNative(Native Method) 
03-18 09:52:20.737: E/AndroidRuntime(1143):  at java.lang.reflect.Method.invoke(Method.java:511) 
03-18 09:52:20.737: E/AndroidRuntime(1143):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
03-18 09:52:20.737: E/AndroidRuntime(1143):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
03-18 09:52:20.737: E/AndroidRuntime(1143):  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. LoginDetails clickedObject = loginArrayList.get(arg2); 
131. 
132. Bundle loginBundle = new Bundle(); 
133. loginBundle.putString("clickedWebSite",clickedObject.getsName()); 
134. loginBundle.putString("clickedWebAddress",clickedObject.getwUrl()); 
135. loginBundle.putString("clickedUserName",clickedObject.getuName()); 
136. loginBundle.putString("clickedPassWord",clickedObject.getpWord()); 
137. loginBundle.putString("clickedNotes",clickedObject.getlNotes()); 
138. 
139. updateDeleteLoginInfo.putExtras(loginBundle); 
140. 
141. startActivityForResult(updateDeleteLoginInfo, 0); 
142. } 
143. } 

回答

0

這是因爲loginArrayList爲空。在使用之前,您應該初始化或填充它。

相關問題