0

I came across with this exeption:

java.lang.ClassCastException: android.app.Application cannot be cast to app.Main.MainActivity

Here is the code where the problem should be:

private Context mContext;

public Chronometer(Context context) {
        this.mContext = context;
    }

     ((Main)mContext).updaterTimerText(String.format("%02d:%02d:%02d:%03d" , hours , minutes , seconds , millis));

I am making a Chronometer-app, if that helps...

Any Ideas?

VVB
  • 7,363
  • 7
  • 49
  • 83
  • @Ironman but the thing is that I read all those questions/answers and they didn't help me in my case – Matthew Anderson Oct 21 '16 at 05:45
  • Please share your more Error log here – Farmer Oct 21 '16 at 05:47
  • 2
    You try to cast an Application object "mCcontext" to an Activity object "Main", which causes the ClassCastException. So show us the Code where you instanciate the Chronometer(Context context) class. – Vall0n Oct 21 '16 at 05:47
  • @Vall0n There is all the code that has mContext in it – Matthew Anderson Oct 21 '16 at 05:48
  • @ShaileshLimbadiya Here is my error log: java.lang.ClassCastException: android.app.Application cannot be cast to app.Main.MainActivity at app.Main.Chronometer.run(Chronometer.java:46) at java.lang.Thread.run(Thread.java:818) – Matthew Anderson Oct 21 '16 at 05:51
  • @MatthewAnderson And I want to know where you use the Chronometer(Context context) constructor. The Problem is propably that the context you pass in the constructor Chronometer(Context context) is the application context and can't be casted to an Activity. – Vall0n Oct 21 '16 at 05:54
  • @Vall0n said that please check you Base case context which you passed in Chronometer(Context context) else share some code.and another thing is if you use Application class then you should declare in Android Menifest.xml file – Farmer Oct 21 '16 at 05:57
  • make globle context ,assign its value in your very first actvity after splash screen , use that globle context here rather then using it from constructor . – Radhey Oct 21 '16 at 05:59

2 Answers2

0

it seems like you are creating Chronometer class passing Application then Activity. Check when you instantiate the object

0

You may want to check where you're instantiating your Chronometer object and if you want it to always take a Main object in the first place, it might be better to pass in one of those in explicitly instead of a Context. It could be that you're grabbing the application context accidentally instead of the activity context and it might help to obviate the issue and help figure out where it's happening.