闹钟code:mContext变量不能被解决 [英] Alarm clock code: mContext variable cant be resolved

查看:163
本文介绍了闹钟code:mContext变量不能被解决的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立在我的文件复制粘贴后的闹钟源$ C ​​$ C
虽然编译,我得到的错误,mContext无法得到解决。
这里是链接到这片code的:<一href=\"http://www.netmite.com/android/mydroid/2.0/packages/apps/AlarmClock/src/com/android/alarmclock/DigitalClock.java\" rel=\"nofollow\">http://www.netmite.com/android/mydroid/2.0/packages/apps/AlarmClock/src/com/android/alarmclock/DigitalClock.java

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

和我都复制粘贴它采用mContext低于

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);
}

要解决这个编译错误,我就把这句话在我的code

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.

能否有人请告诉我如何使用此背景下东西还是笏shud我写作为替代?

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

推荐答案

您需要启动mContext。有一些不同的方式来做到这一点。在活动中,你可以这样做:

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

Context mContext = this;

或一般为:

Context mContext = getContext();

这篇关于闹钟code:mContext变量不能被解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆