android - My project cant see send or received messages -
it keeps crashing @ 1 point on 2 activities:
@override protected void onpostcreate(bundle savedinstancestate) { super.onpostcreate(savedinstancestate); activityutilities.customiseactionbar(this); }
my activityutilities.customiseactionbar
leads to:
public class activityutilities { public static void customiseactionbar(activity activity) { int titleid = 0; if(build.version.sdk_int>=build.version_codes.lollipop) titleid = activity.getresources().getidentifier("action_bar_title", "id", "android"); else titleid = r.id.action_bar_title; if(titleid>0){ textview titleview = (textview) activity.findviewbyid(titleid); titleview.settextsize(22); } } }
the error code i'm getting is:
11-01 16:19:40.991 17854-17854/com.ajapps.app e/androidruntime: fatal exception: main 11-01 16:19:40.991 17854-17854/com.ajapps.app e/androidruntime: process: com.ajapps.app, pid: 17854 11-01 16:19:40.991 17854-17854/com.ajapps.app e/androidruntime: java.lang.runtimeexception: unable start activity componentinfo{com.ajapps.app/com.ajapps.app.wifichat.login}: java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.textview.settextsize(float)' on null object reference 11-01 16:19:40.991 17854-17854/com.ajapps.app e/androidruntime: @ android.app.activitythread.performlaunchactivity(activitythread.java:3119) 11-01 16:19:40.991 17854-17854/com.ajapps.app e/androidruntime: @ android.app.activitythread.handlelaunchactivity(activitythread.java:3218) 11-01 16:19:40.991 17854-17854/com.ajapps.app e/androidruntime: @ android.app.activitythread.access$1000(activitythread.java:198) 11-01 16:19:40.991 17854-17854/com.ajapps.app e/androidruntime: @ android.app.activitythread$h.handlemessage(activitythread.java:1676) 11-01 16:19:40.991 17854-17854/com.ajapps.app e/androidruntime: @ android.os.handler.dispatchmessage(handler.java:102) 11-01 16:19:40.991 17854-17854/com.ajapps.app e/androidruntime: @ android.os.looper.loop(looper.java:145) 11-01 16:19:40.991 17854-17854/com.ajapps.app e/androidruntime: @ android.app.activitythread.main(activitythread.java:6837) 11-01 16:19:40.991 17854-17854/com.ajapps.app e/androidruntime: @ java.lang.reflect.method.invoke(native method) 11-01 16:19:40.991 17854-17854/com.ajapps.app e/androidruntime: @ java.lang.reflect.method.invoke(method.java:372) 11-01 16:19:40.991 17854-17854/com.ajapps.app e/androidruntime: @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1404) 11-01 16:19:40.991 17854-17854/com.ajapps.app e/androidruntime: @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1199) 11-01 16:19:40.991 17854-17854/com.ajapps.app e/androidruntime: caused by: java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.textview.settextsize(float)' on null object reference 11-01 16:19:40.991 17854-17854/com.ajapps.app e/androidruntime: @ com.ajapps.app.wifichat.util.activityutilities.customiseactionbar(activityutilities.java:23) 11-01 16:19:40.991 17854-17854/com.ajapps.app e/androidruntime: @ com.ajapps.app.wifichat.login.onpostcreate(login.java:95) 11-01 16:19:40.991 17854-17854/com.ajapps.app e/androidruntime: @ android.app.instrumentation.callactivityonpostcreate(instrumentation.java:1201) 11-01 16:19:40.991 17854-17854/com.ajapps.app e/androidruntime: @ android.app.activitythread.performlaunchactivity(activitythread.java:3101) 11-01 16:19:40.991 17854-17854/com.ajapps.app e/androidruntime: @ android.app.activitythread.handlelaunchactivity(activitythread.java:3218) 11-01 16:19:40.991 17854-17854/com.ajapps.app e/androidruntime: @ android.app.activitythread.access$1000(activitythread.java:198) 11-01 16:19:40.991 17854-17854/com.ajapps.app e/androidruntime: @ android.app.activitythread$h.handlemessage(activitythread.java:1676) 11-01 16:19:40.991 17854-17854/com.ajapps.app e/androidruntime: @ android.os.handler.dispatchmessage(handler.java:102) 11-01 16:19:40.991 17854-17854/com.ajapps.app e/androidruntime: @ android.os.looper.loop(looper.java:145) 11-01 16:19:40.991 17854-17854/com.ajapps.app e/androidruntime: @ android.app.activitythread.main(activitythread.java:6837) 11-01 16:19:40.991 17854-17854/com.ajapps.app e/androidruntime: @ java.lang.reflect.method.invoke(native method) 11-01 16:19:40.991 17854-17854/com.ajapps.app e/androidruntime: @ java.lang.reflect.method.invoke(method.java:372) 11-01 16:19:40.991 17854-17854/com.ajapps.app e/androidruntime: @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1404) 11-01 16:19:40.991 17854-17854/com.ajapps.app e/androidruntime: @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1199)
if remove activityutilities.customiseactionbar
, works fine app won't receive message.
chatactivity:
public class chatactivity extends activity { private static final string tag = "chatactivity"; private static final int pick_image = 1; private static final int take_photo = 2; private static final int record_audio = 3; private static final int record_video = 4; private static final int choose_file = 5; private static final int drawing = 6; private static final int download_image = 100; private static final int delete_message = 101; private static final int download_file = 102; private static final int copy_text = 103; private static final int share_text = 104; private wifip2pmanager mmanager; private channel mchannel; private wifidirectbroadcastreceiver mreceiver; private intentfilter mintentfilter; private edittext edit; private static listview listview; private static list<message> listmessage; private static chatadapter chatadapter; private uri fileuri; private string fileurl; private arraylist<uri> tmpfilesuri; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_chat); mmanager = (wifip2pmanager) getsystemservice(context.wifi_p2p_service); mchannel = mmanager.initialize(this, getmainlooper(), null); mreceiver = wifidirectbroadcastreceiver.createinstance(); mreceiver.setmactivity(this); mintentfilter = new intentfilter(); mintentfilter.addaction(wifip2pmanager.wifi_p2p_state_changed_action); mintentfilter.addaction(wifip2pmanager.wifi_p2p_peers_changed_action); mintentfilter.addaction(wifip2pmanager.wifi_p2p_connection_changed_action); mintentfilter.addaction(wifip2pmanager.wifi_p2p_this_device_changed_action); //start service receive message startservice(new intent(this, messageservice.class)); //initialize adapter chat listview = (listview) findviewbyid(r.id.messagelist); listmessage = new arraylist<message>(); chatadapter = new chatadapter(this, listmessage); listview.setadapter(chatadapter); //initialize list of temporary files uri tmpfilesuri = new arraylist<uri>(); //send message addfloatingactionbutton button = (addfloatingactionbutton) findviewbyid(r.id.sendmessage); edit = (edittext) findviewbyid(r.id.editmessage); button.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { if(!edit.gettext().tostring().equals("")){ log.v(tag, "send message"); sendmessage(message.text_message); } else{ toast.maketext(chatactivity.this, "please enter not empty message", toast.length_short).show(); } } }); //register context menu list view (for pop menu) registerforcontextmenu(listview); } @override protected void onpostcreate(bundle savedinstancestate) { super.onpostcreate(savedinstancestate); activityutilities.customiseactionbar(this); } @override public void onresume() { super.onresume(); registerreceiver(mreceiver, mintentfilter); mmanager.discoverpeers(mchannel, new wifip2pmanager.actionlistener() { @override public void onsuccess() { log.v(tag, "discovery process succeeded"); } @override public void onfailure(int reason) { log.v(tag, "discovery process failed"); } }); savestateforeground(true); } @override public void onpause() { super.onpause(); unregisterreceiver(mreceiver); savestateforeground(false); } @override public void onbackpressed() { alertdialog.builder newdialog = new alertdialog.builder(this); newdialog.settitle("close chatroom"); newdialog.setmessage("are sure want close chatroom?\n" + "you no longer able receive messages, , " + "all unsaved media files deleted.\n" + "if host, other users disconnected well."); newdialog.setpositivebutton("yes", new dialoginterface.onclicklistener(){ @override public void onclick(dialoginterface dialog, int which) { cleartmpfiles(getexternalfilesdir(null)); if(login.server!=null){ login.server.interrupt(); } android.os.process.killprocess(android.os.process.mypid()); } }); newdialog.setnegativebutton("cancel", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { dialog.cancel(); } }); newdialog.show(); } @override protected void ondestroy() { super.onstop(); cleartmpfiles(getexternalfilesdir(null)); } // handle data sent 'for result' activities (pick/take image, record audio/video) @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); switch(requestcode){ case pick_image: if (resultcode == result_ok && data.getdata() != null) { fileuri = data.getdata(); sendmessage(message.image_message); } break; case take_photo: if (resultcode == result_ok && data.getdata() != null) { fileuri = data.getdata(); sendmessage(message.image_message); tmpfilesuri.add(fileuri); } break; case record_audio: if (resultcode == result_ok) { fileurl = (string) data.getstringextra("audiopath"); sendmessage(message.audio_message); } break; case record_video: if (resultcode == result_ok) { fileuri = data.getdata(); fileurl = mediafile.getrealpathfromuri(this, fileuri); sendmessage(message.video_message); } break; case choose_file: if (resultcode == result_ok) { fileurl = (string) data.getstringextra("filepath"); sendmessage(message.file_message); } break; case drawing: if(resultcode == result_ok){ fileurl = (string) data.getstringextra("drawingpath"); sendmessage(message.drawing_message); } break; } } // hydrate message object launch asynctasks send public void sendmessage(int type){ log.v(tag, "send message starts"); // message written in edittext sent message mes = new message(type, edit.gettext().tostring(), null, login.chatname); switch(type){ case message.image_message: image image = new image(this, fileuri); log.v(tag, "bitmap url ok"); mes.setbytearray(image.bitmaptobytearray(image.getbitmapfromuri())); mes.setfilename(image.getfilename()); mes.setfilesize(image.getfilesize()); log.v(tag, "set byte array image ok"); break; case message.audio_message: mediafile audiofile = new mediafile(this, fileurl, message.audio_message); mes.setbytearray(audiofile.filetobytearray()); mes.setfilename(audiofile.getfilename()); mes.setfilepath(audiofile.getfilepath()); break; case message.video_message: mediafile videofile = new mediafile(this, fileurl, message.audio_message); mes.setbytearray(videofile.filetobytearray()); mes.setfilename(videofile.getfilename()); mes.setfilepath(videofile.getfilepath()); tmpfilesuri.add(fileuri); break; case message.file_message: mediafile file = new mediafile(this, fileurl, message.file_message); mes.setbytearray(file.filetobytearray()); mes.setfilename(file.getfilename()); break; case message.drawing_message: mediafile drawingfile = new mediafile(this, fileurl, message.drawing_message); mes.setbytearray(drawingfile.filetobytearray()); mes.setfilename(drawingfile.getfilename()); mes.setfilepath(drawingfile.getfilepath()); break; } log.v(tag, "message object hydrated"); log.v(tag, "start asynctasks send message"); if(mreceiver.isgroupeowner() == wifidirectbroadcastreceiver.is_owner){ log.v(tag, "message hydrated, start sendmessageserver asynctask"); new sendmessageserver(chatactivity.this, true).executeonexecutor(asynctask.thread_pool_executor, mes); } else if(mreceiver.isgroupeowner() == wifidirectbroadcastreceiver.is_client){ log.v(tag, "message hydrated, start sendmessageclient asynctask"); new sendmessageclient(chatactivity.this, mreceiver.getowneraddr()).executeonexecutor(asynctask.thread_pool_executor, mes); } edit.settext(""); } // refresh message list public static void refreshlist(message message, boolean ismine){ log.v(tag, "refresh message list starts"); message.setmine(ismine); listmessage.add(message); chatadapter.notifydatasetchanged(); log.v(tag, "chat adapter notified of changes"); //scroll last element of list listview.setselection(listmessage.size() - 1); } // save app's state (foreground or background) sharedprefereces public void savestateforeground(boolean isforeground){ sharedpreferences prefs = preferencemanager.getdefaultsharedpreferences(this); editor edit = prefs.edit(); edit.putboolean("isforeground", isforeground); edit.commit(); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.chat, menu); return true; } // handle click on menu @override public boolean onoptionsitemselected(menuitem item) { int iditem = item.getitemid(); switch(iditem){ case r.id.send_image: showpopup(edit); return true; case r.id.send_audio: log.v(tag, "start activity record audio"); startactivityforresult(new intent(this, recordaudioactivity.class), record_audio); return true; case r.id.send_video: log.v(tag, "start activity record video"); intent takevideointent = new intent(mediastore.action_video_capture); takevideointent.putextra(mediastore.extra_video_quality, 0); if (takevideointent.resolveactivity(getpackagemanager()) != null) { startactivityforresult(takevideointent, record_video); } return true; case r.id.send_file: log.v(tag, "start activity choose file"); intent choosefileintent = new intent(this, filepickeractivity.class); startactivityforresult(choosefileintent, choose_file); return true; case r.id.send_drawing: log.v(tag, "start activity draw"); intent drawintent = new intent(this, drawingactivity.class); startactivityforresult(drawintent, drawing); return true; default: return super.onoptionsitemselected(item); } } //show popup menu public void showpopup(view v) { popupmenu popup = new popupmenu(this, v); popup.setonmenuitemclicklistener(new onmenuitemclicklistener() { @override public boolean onmenuitemclick(menuitem item) { switch(item.getitemid()){ case r.id.pick_image: log.v(tag, "pick image"); intent intent = new intent(intent.action_pick); intent.settype("image/*"); intent.setaction(intent.action_get_content); // prevent crash if no app can handle intent if (intent.resolveactivity(getpackagemanager()) != null) { startactivityforresult(intent, pick_image); } break; case r.id.take_photo: log.v(tag, "take photo"); intent intent2 = new intent(mediastore.action_image_capture); if (intent2.resolveactivity(getpackagemanager()) != null) { startactivityforresult(intent2, take_photo); } break; } return true; } }); popup.inflate(r.menu.send_image); popup.show(); } //create pop menu image download, delete message, etc... @override public void oncreatecontextmenu(contextmenu menu, view v, contextmenuinfo menuinfo) { super.oncreatecontextmenu(menu, v, menuinfo); menu.setheadertitle("options"); adaptercontextmenuinfo info = (adaptercontextmenuinfo) menuinfo; message mes = listmessage.get((int) info.position); //option delete message independently of type menu.add(0, delete_message, menu.none, "delete message"); if(!mes.getmtext().equals("")){ //option copy message's text clipboard menu.add(0, copy_text, menu.none, "copy message text"); //option share message's text menu.add(0, share_text, menu.none, "share message text"); } int type = mes.getmtype(); switch(type){ case message.image_message: menu.add(0, download_image, menu.none, "download image"); break; case message.file_message: menu.add(0, download_file, menu.none, "download file"); break; case message.audio_message: menu.add(0, download_file, menu.none, "download audio file"); break; case message.video_message: menu.add(0, download_file, menu.none, "download video file"); break; case message.drawing_message: menu.add(0, download_file, menu.none, "download drawing"); break; } } //handle click event on pop menu @override public boolean oncontextitemselected(menuitem item) { adaptercontextmenuinfo info = (adaptercontextmenuinfo) item.getmenuinfo(); switch (item.getitemid()) { case download_image: downloadimage(info.id); return true; case delete_message: deletemessage(info.id); return true; case download_file: downloadfile(info.id); return true; case copy_text: copytexttoclipboard(info.id); return true; case share_text: sharemedia(info.id, message.text_message); return true; default: return super.oncontextitemselected(item); } } //download image , save downloads public void downloadimage(long id){ message mes = listmessage.get((int) id); bitmap bm = mes.bytearraytobitmap(mes.getbytearray()); string path = environment.getexternalstoragepublicdirectory(environment.directory_downloads).getabsolutepath(); fileutilities.saveimagefrombitmap(this, bm, path, mes.getfilename()); fileutilities.refreshmedialibrary(this); } //download file , save downloads public void downloadfile(long id){ message mes = listmessage.get((int) id); string sourcepath = mes.getfilepath(); string destinationpath = environment.getexternalstoragepublicdirectory(environment.directory_downloads).getabsolutepath(); fileutilities.copyfile(this, sourcepath, destinationpath, mes.getfilename()); fileutilities.refreshmedialibrary(this); } //delete message message list (doesn't delete on other phones) public void deletemessage(long id){ listmessage.remove((int) id); chatadapter.notifydatasetchanged(); } private void cleartmpfiles(file dir){ file[] childdirs = dir.listfiles(); for(file child : childdirs){ if(child.isdirectory()){ cleartmpfiles(child); } else{ child.delete(); } } for(uri uri: tmpfilesuri){ getcontentresolver().delete(uri, null, null); } fileutilities.refreshmedialibrary(this); } public void talkto(string destination){ edit.settext("@" + destination + " : "); edit.setselection(edit.gettext().length()); } private void copytexttoclipboard(long id){ message mes = listmessage.get((int) id); clipboardmanager clipboard = (clipboardmanager) getsystemservice(clipboard_service); clipdata clip = clipdata.newplaintext("message", mes.getmtext()); clipboard.setprimaryclip(clip); toast.maketext(this, "message copied clipboard", toast.length_short).show(); } private void sharemedia(long id, int type){ message mes = listmessage.get((int) id); switch(type){ case message.text_message: intent sendintent = new intent(); sendintent.setaction(intent.action_send); sendintent.putextra(intent.extra_text, mes.getmtext()); sendintent.settype("text/plain"); startactivity(sendintent); } }
}
can try below code
getwindow().getdecorview().findviewbyid(titleid)
Comments
Post a Comment