Mettre à jour un TextView depuis une autre classe
Bonjour.
J'ai un petit problème dans mon code et je n'arrive pas à le solutionner.
Je reçois des donnés (string) via une connexion bluetooth (de la classe bluetooth) et j'aimerais suite à cette réception, mettre à jour le TextView d'une activité.
Le problème vient de la ligne
context.logview.setText("coucou");
(dans l'exemple, je n'envois pas la donnée reçut mais "coucou" juste pour tester.)
Voila la partie du code qui reçoit les donnés :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
String data = new String(rawdata);
Log.d("debug", "new string");
Log.d("debug", "data");
//a faire dans le Thread UI
Log.d("debug", "envoie.affiche "+ data);
context = (MainActivity) context;
Log.d("debug", "context cree");
//a faire dans le Thread UI
runOnUiThread(new Runnable() {
public void run() {
Log.d("debug", "in Run on UI");
context.logview.setText("coucou");
}
}
);
Log.d("debug", data); |
Et j'obtiens l'erreur suivante :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
07-22 01:41:07.411: D/debug(32038): new string
07-22 01:41:07.411: D/debug(32038): data
07-22 01:41:07.411: D/debug(32038): envoie.affiche f
07-22 01:41:07.411: D/debug(32038): context cree
07-22 01:41:07.421: D/debug(32038): in Run on UI
07-22 01:41:07.421: D/AndroidRuntime(32038): Shutting down VM
07-22 01:41:07.421: W/dalvikvm(32038): threadid=1: thread exiting with uncaught exception (group=0x40ab6228)
07-22 01:41:07.421: E/AndroidRuntime(32038): FATAL EXCEPTION: main
07-22 01:41:07.421: E/AndroidRuntime(32038): java.lang.NullPointerException
07-22 01:41:07.421: E/AndroidRuntime(32038): at com.example.service_ap.BtClass$1$1.run(BtClass.java:307)
07-22 01:41:07.421: E/AndroidRuntime(32038): at android.os.Handler.handleCallback(Handler.java:608)
07-22 01:41:07.421: E/AndroidRuntime(32038): at android.os.Handler.dispatchMessage(Handler.java:92)
07-22 01:41:07.421: E/AndroidRuntime(32038): at android.os.Looper.loop(Looper.java:156)
07-22 01:41:07.421: E/AndroidRuntime(32038): at android.app.ActivityThread.main(ActivityThread.java:4987)
07-22 01:41:07.421: E/AndroidRuntime(32038): at java.lang.reflect.Method.invokeNative(Native Method)
07-22 01:41:07.421: E/AndroidRuntime(32038): at java.lang.reflect.Method.invoke(Method.java:511)
07-22 01:41:07.421: E/AndroidRuntime(32038): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-22 01:41:07.421: E/AndroidRuntime(32038): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-22 01:41:07.421: E/AndroidRuntime(32038): at dalvik.system.NativeStart.main(Native Method)
07-22 01:41:07.431: D/debug(32038): f
07-22 01:44:20.059: D/Process(32038): killProcess, pid=32038 |
Avec un TextView a priori bien déclaré :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| public class MainActivity extends Activity implements OnClickListener {
public TextView logview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("debug","Commencement");
MyApplication appState = ((MyApplication)getApplicationContext());
Log.d("debug","Context de MyApplication recuperé");
bt = appState.CreateBtDevice();
Log.d("debug","bt recuperé");
logview = (TextView)findViewById(R.id.logview); |
J’espère que vous pourrez m'aider.
Merci d'avance.