java - Interface object not working when called from another fragment -
ok, have android app login node.js server. file structure consist of: mainactivity.java, firstfragment.java, middleman.java, , mysocket.java.
mainactivity.java
package ""; import android.app.activity; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.widget.edittext; import java.net.urisyntaxexception; public class mainactivity extends activity implements middleman { string server_address = ""; mysocket my_socket; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } public void onconnectbuttonpressed(edittext edittext_id){ system.out.println("received connect request first fragment."); my_socket = new mysocket(); try { my_socket.establishconnection(server_address, edittext_id); } catch (urisyntaxexception e) { e.printstacktrace(); } } public void connectionestablished() { system.out.println("received established connection mysocket.java."); } }
firstfragment.java
package ""; import android.app.activity; import android.app.fragment; import android.os.bundle; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.button; import android.widget.edittext; import android.widget.textview; public class firstfragment extends fragment{ middleman mcallback; @override public void onattach(activity activity){ super.onattach(activity); try { mcallback = (middleman) activity; } catch (classcastexception e) { throw new classcastexception(activity.tostring() + " must implement reqestconnect"); } } private static textview textview_state; private static edittext edittext_id; button button_connect; button button_disconnect; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate){ view view = inflater.inflate(r.layout.first_fragment_root,container,false); textview_state = (textview) view.findviewbyid(r.id.textview_state); edittext_id = (edittext) view.findviewbyid(r.id.edittext_id); button_connect = (button) view.findviewbyid(r.id.button_connect); button_connect.setonclicklistener(new view.onclicklistener(){ public void onclick(view v){ buttonconnectclicked(); } }); button button_disconnect = (button) view.findviewbyid(r.id.button_disconnect); return view; } private void buttonconnectclicked() { system.out.println("connect button pressed "); mcallback.onconnectbuttonpressed(edittext_id); } }
middleman.java
package ""; import android.widget.edittext; public interface middleman { void onconnectbuttonpressed(edittext edittext_id); void connectionestablished(); }
mysocket.java
package ""; import android.app.activity; import android.app.fragment; import android.widget.edittext; import com.github.nkzawa.emitter.emitter; import com.github.nkzawa.socketio.client.io; import com.github.nkzawa.socketio.client.socket; import java.net.urisyntaxexception; public class mysocket extends fragment{ public socket lobby; middleman mcallback; @override public void onattach(activity activity){ super.onattach(activity); try { mcallback = (middleman) activity; } catch (classcastexception e) { throw new classcastexception(activity.tostring() + " must implement reqestconnect"); } } public string getuserid(edittext edittext_id) { string string = edittext_id.gettext().tostring(); if (string.length() > 0) { return string; } else { return null; } } public void establishconnection(string server_address,edittext edittext_id) throws urisyntaxexception { lobby = io.socket(server_address); final string client = getuserid(edittext_id); system.out.println("client: " + client); lobby.on(socket.event_connect, new emitter.listener() { @override public void call(object... args) { system.out.println("connection established"); mcallback.connectionestablished(); } }).on("event", new emitter.listener() { @override public void call(object... args) { } }).on(socket.event_disconnect, new emitter.listener() { @override public void call(object... args) { system.out.println("connection terminated."); } }); lobby.connect(); } }
there activity_main.xml, first_fragment_root.xml (that eats of display) , mysocket.java has no associated layout.
so far happens app boots displays edittext, textview, , 2 buttons: connect , disconnect. entering id , clicking connect button executes bit of code in firstfragment.java:
private void buttonconnectclicked() { system.out.println("connect button pressed "); mcallback.onconnectbuttonpressed(edittext_id); }
which moves on over middleman.java have mainactivity.java execute bit:
public void onconnectbuttonpressed(edittext edittext_id){ system.out.println("received connect request first fragment."); my_socket = new mysocket(); try { my_socket.establishconnection(server_address, edittext_id); } catch (urisyntaxexception e) { e.printstacktrace(); } }
which sends mysocket.java's establishconnection:
public void establishconnection(string server_address,edittext edittext_id) throws urisyntaxexception { lobby = io.socket(server_address); final string client = getuserid(edittext_id); system.out.println("client: " + client); lobby.on(socket.event_connect, new emitter.listener() { @override public void call(object... args) { system.out.println("connection established"); mcallback.connectionestablished(); } }).on("event", new emitter.listener() { @override public void call(object... args) { } }).on(socket.event_disconnect, new emitter.listener() { @override public void call(object... args) { system.out.println("connection terminated."); } }); lobby.connect(); }
everything worked fine, until added wee bit mysocket.java having own object middle man tell mainactivity.java finished. instead of running mcallback.connectionestablished(); program breaks , tells me attempt invoke interface method 'void middleman.connectionestablished()' on null object reference
i have no idea doing wrong, assume onattach method in mysocket.java not getting called mcallback here null, why? reading.
conclusion
i've decided post working version of people might have same or similar issue.
mainactivity.java
package ""; import android.app.activity; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.widget.edittext; import java.net.urisyntaxexception; public class mainactivity extends activity implements middleman { string server_address = ""; mysocket my_socket; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } public void onconnectbuttonpressed(edittext edittext_id){ if (my_socket==null) { system.out.println("received connect request first fragment."); my_socket = new mysocket(); my_socket.setlistener(this); try { my_socket.establishconnection(server_address, edittext_id); } catch (urisyntaxexception e) { e.printstacktrace(); } } } public void connectionestablished() { system.out.println("received established connection mysocket.java."); system.out.println("attempting establish room client."); my_socket.requestroom(); runonuithread(new runnable() { @override public void run() { firstfragment f = (firstfragment) getfragmentmanager().findfragmentbyid(r.id.first_fragment_root); f.updatetextviewstate("connected"); } }); } @override public void ondisconnectbuttonpressed() { if (my_socket!=null) { system.out.println("received disconnect request first fragment."); my_socket.requestdisconnect(); } } @override public void disconnectestablished() { system.out.println("received disconnected mysocket.java."); my_socket = null; runonuithread(new runnable() { @override public void run() { firstfragment f = (firstfragment) getfragmentmanager().findfragmentbyid(r.id.first_fragment_root); f.updatetextviewstate("disconnected"); } }); } }
firstfragment.java
package ""; import android.app.activity; import android.app.fragment; import android.os.bundle; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.button; import android.widget.edittext; import android.widget.textview; public class firstfragment extends fragment{ middleman mcallback; @override public void onattach(activity activity){ super.onattach(activity); try { mcallback = (middleman) activity; } catch (classcastexception e) { throw new classcastexception(activity.tostring() + " must implement reqestconnect"); } } private static textview textview_state; private edittext edittext_id; button button_connect; button button_disconnect; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate){ view view = inflater.inflate(r.layout.first_fragment_root,container,false); textview_state = (textview) view.findviewbyid(r.id.textview_state); edittext_id = (edittext) view.findviewbyid(r.id.edittext_id); button_connect = (button) view.findviewbyid(r.id.button_connect); button_connect.setonclicklistener(new view.onclicklistener(){ @override public void onclick(view v){ buttonconnectclicked(); } }); button_disconnect = (button) view.findviewbyid(r.id.button_disconnect); button_disconnect.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { buttondisconnectclicked(); } }); return view; } private void buttondisconnectclicked() { system.out.println("disconnect button pressed "); mcallback.ondisconnectbuttonpressed(); } private void buttonconnectclicked() { system.out.println("connect button pressed "); if (this.edittext_id.gettext().tostring()!=null && !this.edittext_id.gettext().tostring().isempty()) { mcallback.onconnectbuttonpressed(edittext_id); } } public void updatetextviewstate(string s){ textview_state.settext(s); } }
mysocket.java
package ""; import android.app.fragment; import android.content.context; import android.widget.edittext; import com.github.nkzawa.emitter.emitter; import com.github.nkzawa.socketio.client.io; import com.github.nkzawa.socketio.client.socket; import java.net.urisyntaxexception; public class mysocket extends fragment{ public socket lobby; string client; middleman mcallback; public void setlistener(context callback) { try { mcallback = (middleman) callback; } catch (classcastexception e) { throw new classcastexception(callback.tostring() + " must implement reqestconnect"); } } public string getuserid(edittext edittext_id) { string string = edittext_id.gettext().tostring(); if (string.length() > 0) { return string; } else { return null; } } public void establishconnection(string server_address,edittext edittext_id) throws urisyntaxexception { lobby = io.socket(server_address); final string client = getuserid(edittext_id); this.client = client; system.out.println("client: " + this.client); lobby.on(socket.event_connect, new emitter.listener() { @override public void call(object... args) { system.out.println("connection established"); mcallback.connectionestablished(); } }).on("event", new emitter.listener() { @override public void call(object... args) { string s = (string) args[0]; system.out.println(s); } }).on(socket.event_disconnect, new emitter.listener() { @override public void call(object... args) { system.out.println("connection terminated."); if (lobby!=null) { disconnect(); } } }).on("room_created", new emitter.listener() { @override public void call(object... args) { string s = (string) args[0]; system.out.println(s); } }); lobby.connect(); } public void requestroom() { lobby.emit("request_user_room", this.client); } public void requestdisconnect() { if (lobby!=null) { if (lobby.connected()) { lobby.disconnect(); } } } private void disconnect() { lobby.off(socket.event_connect); lobby.off("event"); lobby.off(socket.event_disconnect); lobby.off("room_created"); lobby = null; mcallback.disconnectestablished(); } }
middleman.java
package ""; import android.widget.edittext; public interface middleman { void onconnectbuttonpressed(edittext edittext_id); void connectionestablished(); void ondisconnectbuttonpressed(); void disconnectestablished(); }
activity_main.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" tools:context=".mainactivity"> <fragment android:layout_width="match_parent" android:layout_height="match_parent" android:name="<<path_to_>>.firstfragment" android:id="@+id/first_fragment_root" tools:layout="@layout/first_fragment_root"/> </relativelayout>
first_fragment_root.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancemedium" android:text="@string/textview_id" android:id="@+id/textview_id" android:layout_alignparenttop="true" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_margintop="23dp" /> <edittext android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputtype="phone" android:ems="10" android:id="@+id/edittext_id" android:hint="@string/edittext_id" android:layout_marginleft="23dp" android:layout_marginstart="23dp" android:layout_alignbottom="@+id/textview_id" android:layout_torightof="@+id/textview_id" android:layout_toendof="@+id/textview_id" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancemedium" android:text="@string/textview_status" android:id="@+id/textview_status" android:layout_alignparentbottom="true" android:layout_alignparentleft="true" android:layout_alignparentstart="true" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancemedium" android:text="@string/textview_state" android:id="@+id/textview_state" android:layout_alignparentbottom="true" android:layout_torightof="@+id/textview_status" android:layout_marginleft="23dp" android:layout_marginstart="23dp" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_connect" android:id="@+id/button_connect" android:layout_below="@+id/textview_id" android:layout_alignleft="@+id/edittext_id" android:layout_alignstart="@+id/edittext_id" android:layout_margintop="25dp" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_disconnect" android:id="@+id/button_disconnect" android:layout_alignbottom="@+id/button_connect" android:layout_alignright="@+id/edittext_id" android:layout_alignend="@+id/edittext_id" /> </relativelayout>
from android developers:
onattach(context context) called when fragment first attached context.
it doesn't attaching context, rather, using fragment if regular java class. can useful, however, onattach isn't called.
maybe try this. instead of code:
@override public void onattach(activity activity){ super.onattach(activity); try { mcallback = (middleman) activity; } catch (classcastexception e) { throw new classcastexception(activity.tostring() + " must implement reqestconnect"); } }
call setlistener(reqestconnect callback) this:
public void setlistener(reqestconnect callback) { mcallback = callback; }
then, when create socket fragment call setlistener method. this:
public void onconnectbuttonpressed(edittext edittext_id){ system.out.println("received connect request first fragment."); my_socket = new mysocket(); my_socket.setlistener(this); try { my_socket.establishconnection(server_address, edittext_id); } catch (urisyntaxexception e) { e.printstacktrace(); } }
Comments
Post a Comment