This question already has an answer here:
I have a problem trying to collect data from a SQLite database, for some reason when you insert the item to the list (RecyclerView) returns null assuming error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{beta.unomasenlafamilia/beta.unomasenlafamilia.PM_MessagesActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void beta.unomasenlafamilia.adapter.MessageAdapter.addListItem(beta.unomasenlafamilia.domain.Message, int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2406)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2466)
at android.app.ActivityThread.access$1200(ActivityThread.java:152)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1341)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5538)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void beta.unomasenlafamilia.adapter.MessageAdapter.addListItem(beta.unomasenlafamilia.domain.Message, int)' on a null object reference
at beta.unomasenlafamilia.PM_MessagesActivity.mostrarChatLocal(PM_MessagesActivity.java:457)
at beta.unomasenlafamilia.PM_MessagesActivity.onCreate(PM_MessagesActivity.java:120)
MainActivity.class
public void showChatLocal(String userfrom,String userto) {
//Show total num records
int limite = h_sql.showMsgNum();
Cursor cursor = h_sql.fetchMessages(String.valueOf(mUserFrom.getId()), String.valueOf(mUserTo.getId()));
MessageAdapter adapter = (MessageAdapter) mRecyclerView.getAdapter();
List<Message> messageList = new ArrayList<Message>();
for(int i =0;i<limite;i++){
Message m = new Message();
m.setUserFrom(cursor.getString(cursor.getColumnIndex("id_user_from")));
m.setUserTo(cursor.getString(cursor.getColumnIndex("id_user_to")));
m.setMessage(cursor.getString(cursor.getColumnIndex("message")));
m.setRegTime(Long.parseLong(cursor.getString(cursor.getColumnIndex("regtime"))));
m.setWasRead(Integer.parseInt(cursor.getString(cursor.getColumnIndex("wasRead"))));
messageList.add(m);
adapter.addListItem(messageList.get(i),0);
mRecyclerView.smoothScrollToPosition(0);
cursor.moveToNext();
}
cursor.close();
}
//MessageAdapter
public void addListItem(Message m, int position) {
mList.add(position, m);
notifyItemInserted(position);
}
Thank you very much to all, regards!
Aucun commentaire:
Enregistrer un commentaire