1

I am trying to build the alarmclock source code after copy pasting in my files While compilation, I get the error, mContext cannot be resolved. Here is the link to this piece of code: http://www.netmite.com/android/mydroid/2.0/packages/apps/AlarmClock/src/com/android/alarmclock/DigitalClock.java

And I have copy pasted some part of the code which uses mContext below

protected void onAttachedToWindow() {
    super.onAttachedToWindow();

    if (Log.LOGV) Log.v("onAttachedToWindow " + this);

    if (mAttached) return;
    mAttached = true;

    if (mAnimate) {
        setBackgroundResource(R.drawable.animate_circle);
        /* Start the animation (looped playback by default). */
        ((AnimationDrawable) getBackground()).start();
    }

    if (mLive) {
        /* monitor time ticks, time changed, timezone */
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_TIME_TICK);
        filter.addAction(Intent.ACTION_TIME_CHANGED);
        filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
        mContext.registerReceiver(mIntentReceiver, filter, null, mHandler);

    }

    /* monitor 12/24-hour display preference */
    mFormatChangeObserver = new FormatChangeObserver();

    mContext.getContentResolver().registerContentObserver(

            Settings.System.CONTENT_URI, true, mFormatChangeObserver); 

    updateTime();
}

private void setDateFormat() {      

    mFormat = Alarms.get24HourMode(mContext) ? Alarms.M24 : M12;
    mAmPm.setShowAmPm(mFormat == M12);
}

To solve this compilation error, I put this statement in my code

Context mContext;

But though the compilation errors are solved, on launch in the emulator, the application throws an exception and exits without launching.

Can some one please tell me how to use this context thing or wat shud i write as an alternative?

dave.c
  • 10,910
  • 5
  • 39
  • 62
rafia_sultana
  • 91
  • 3
  • 6

4 Answers4

3

You need to initiate mContext. There are some different ways to do this. In the activity you could do:

Context mContext = this;

or generally:

Context mContext = getContext();
Heinrisch
  • 5,835
  • 4
  • 33
  • 43
2

Instead of mContext, use the getContext() method to get hold of a context. The sample code there perhaps missed this part.

Kumar Bibek
  • 9,016
  • 2
  • 39
  • 68
2

instead of mContext use getApplicationContext() .Hope it Will Works

Anil Jadhav
  • 2,128
  • 1
  • 17
  • 31
1

here you need to add context of your activity try my code

mFormat = Alarms.get24HourMode(this) ? Alarms.M24 : M12;
        mAmPm.setShowAmPm(mFormat == M12)

;

need more of your code

but my suggestion is where you are getting your context in this view or activity intialize your mContext variable there and it will work

vipin
  • 2,851
  • 4
  • 18
  • 32