java - Unable to change text of TextView inside of a Fragment from same activity -
i trying change textview give status of bluetooth connection within fragment, nothing seems happen when msgreceived.settext(string)
called. how go doing this? here java file fragment:
package dleedesign.dubcommunicationstestapp; import android.app.fragment; import android.bluetooth.bluetoothadapter; import android.os.bundle; import android.os.handler; import android.os.message; import android.support.annotation.nullable; import android.view.layoutinflater; import android.util.log; import android.view.view; import android.view.viewgroup; import android.widget.button; import android.widget.textview; public class secondfragment extends fragment implements view.onclicklistener { view myview; public final string tag = "main"; private bluetooth bt; public button sendcommand; public textview msgreceived; @nullable @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { myview = inflater.inflate(r.layout.second_layout, container, false); sendcommand = (button) inflater.inflate(r.layout.second_layout, container, false).findviewbyid(r.id.sendcommand); msgreceived = (textview) inflater.inflate(r.layout.second_layout, container, false).findviewbyid(r.id.msgreceived); msgreceived.settext("ready connect"); bt = new bluetooth(new mainactivity(), mhandler); connectservice(); return myview; } @override public void onclick(view v) { connectservice(); } public void connectservice() { try { msgreceived.settext("connecting..."); bluetoothadapter btadapter = bluetoothadapter.getdefaultadapter(); if(btadapter.isenabled()) { bt.start(); bt.connectdevice("hc-06"); log.d(tag, "btservice started- listening"); msgreceived.settext("connected!"); } else { log.w(tag, "btservice started - bluetooth not enabled"); msgreceived.settext("bluetooth not enabled"); } } catch (exception e) { log.e(tag, "unable start bluetooth", e); msgreceived.settext("unable connect: " + e); } } private final handler mhandler = new handler() { @override public void handlemessage(message msg) { switch (msg.what) { case bluetooth.message_state_change: log.d(tag, "message_state_change: " + msg.arg1); break; case bluetooth.message_write: log.d(tag, "message_write"); break; case bluetooth.message_read: log.d(tag, "message_read"); break; case bluetooth.message_device_name: log.d(tag, "message_device_name " + msg); break; case bluetooth.message_toast: log.d(tag, "message_toast " + msg); break; } } }; }
edit: here logcat message spits out when select secondfragment:
dleedesign.dubcommunicationstestapp d/bluetoothservice: bounded device 40:ef:4c:c2:a9:32 dleedesign.dubcommunicationstestapp d/bluetoothservice: bounded device 98:d3:31:70:80:c5 dleedesign.dubcommunicationstestapp d/bluetoothservice: start dleedesign.dubcommunicationstestapp d/bluetoothservice: setstate() 0 -> 1 dleedesign.dubcommunicationstestapp w/bluetoothadapter: getbluetoothservice() called no bluetoothmanagercallback dleedesign.dubcommunicationstestapp d/bluetoothservice: socket type: nullbegin macceptthreadthread[thread-20627,5,main] dleedesign.dubcommunicationstestapp d/bluetoothservice: connect to: 98:d3:31:70:80:c5 dleedesign.dubcommunicationstestapp d/bluetoothservice: setstate() 1 -> 2 dleedesign.dubcommunicationstestapp d/main: btservice started- listening dleedesign.dubcommunicationstestapp i/bluetoothservice: begin mconnectthread sockettype:null dleedesign.dubcommunicationstestapp w/bluetoothadapter: getbluetoothservice() called no bluetoothmanagercallback dleedesign.dubcommunicationstestapp d/main: message_state_change: 1 dleedesign.dubcommunicationstestapp d/main: message_state_change: 2 dleedesign.dubcommunicationstestapp e/bluetoothservice: unable connect socket java.io.ioexception: read failed, socket might closed or timeout, read ret: -1 @ android.bluetooth.bluetoothsocket.readall(bluetoothsocket.java:517) @ android.bluetooth.bluetoothsocket.readint(bluetoothsocket.java:528) @ android.bluetooth.bluetoothsocket.connect(bluetoothsocket.java:320) @ dleedesign.dubcommunicationstestapp.bluetooth$connectthread.run(bluetooth.java:408) dleedesign.dubcommunicationstestapp d/bluetoothservice: start dleedesign.dubcommunicationstestapp d/bluetoothservice: setstate() 2 -> 1 dleedesign.dubcommunicationstestapp d/main: message_toast { when=-15ms what=5 target=dleedesign.dubcommunicationstestapp.secondfragment$1 } dleedesign.dubcommunicationstestapp d/main: message_state_change: 1
whats happening you're trying inflate view given layout each time findviewbyid
, casting textview. calling findviewbyid
on different view you're returning. should first inflate layout did in first line
myview = inflater.inflate....
and then,
msgreceived = (textview) myview.findviewbyid(r.id.msgreceived);
(same other textview, button etc trying use)
Comments
Post a Comment