When I use this code:
@Override
public void onReceive(Context context, Intent intent) {
seconds += 0.1;
if (seconds > 59.9) {
seconds=0;
minutes++;
}
TextView tv = (TextView)((MainActivity)context).findViewById(R.id.txtChron);
tv.setText (String.valueOf(minutes) + ":" + String.valueOf(seconds));
}
I get an error when trying to cast the context to MainActivity
.
Here is the error / stack trace:
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.jordanbleu.lab8chronservjordanbleu, PID: 22693 java.lang.RuntimeException: Error receiving broadcast Intent { act=com.example.jordanbleu.lab8chronservjordanbleu.MY_TIME_TICK flg=0x10 } in com.example.jordanbleu.lab8chronservjordanbleu.ChronometerReceiver@d9365fe at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:1132) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to com.example.jordanbleu.lab8chronservjordanbleu.MainActivity at com.example.jordanbleu.lab8chronservjordanbleu.ChronometerReceiver.onReceive(ChronometerReceiver.java:41) at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:1122) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
So I have a MainActivity
, which starts a service. The service creates a new thread, and this thread constantly created implicit intents which are handled by a BroadcastReceiver
. This broadcast receiver will update a clock, and update a TextView
in the layout with the updated time. But when I try to get access to the findViewByID
via the context parameter, I get the error above.