如何为ImageView分配drawable

Jas*_*ngh 3 java android cyanogenmod android-resources

我正在努力将光环添加到cyanogenmod 11中.为此,我使用的方法可以使光晕处于活动状态,然后相应地为其分配图像.

首先,这是我最近尝试的代码(我已多次尝试完成此任务) - >

protected void updateHalo() {
//mHaloActive is a boolean. mHaloButton is an ImageView
    mHaloActive = Settings.System.getInt(mContext.getContentResolver(),
            Settings.System.HALO_ACTIVE, 0) == 1;
    int resID;
    String mDrawableName;
    if (mHaloActive) {
        mDrawableName = "ic_notify_halo_pressed";
        resID = getResources().getIdentifier(mDrawableName, "drawable", getPackageName());
        mHaloButton.setImageResource(resID);
    } else {
        mDrawableName = "ic_notify_halo_normal";
        resID = getResources().getIdentifier(mDrawableName, "drawable", getPackageName());
        mHaloButton.setImageResource(resID);
    }
    if (mHaloActive) {
        if (mHalo == null) {
            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mHalo = (Halo) inflater.inflate(R.layout.halo_trigger, null);
            mHalo.setLayerType(View.LAYER_TYPE_HARDWARE, null);
            WindowManager.LayoutParams params = mHalo.getWMParams();
            mWindowManager.addView(mHalo, params);
            mHalo.setStatusBar(this);
        }
    } else {
        if (mHalo != null) {
            mHalo.cleanUp();
            mWindowManager.removeView(mHalo);
            mHalo = null;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,它说 cannot find symbol: method getPackageName()

早些时候我曾多次尝试使用setImageResource(R.drawable.ic_notify_halo_pressed); 但是这给了我NPE.

然后我也尝试使用:

Resources resources = getResources();        mHaloButton.setImageDrawable(resources.getDrawable(R.drawable.ic_notify_halo_pressed));
Run Code Online (Sandbox Code Playgroud)

但这给了错误 cannot find symbol: method getResources()

我也尝试了,mHaloButton.setImageDrawable(R.drawable.ic_notify_halo_pressed);但它给出了错误:setImageDrawable(android.graphics.drawable.Drawable) in android.widget.ImageView cannot be applied to (int)

所以我知道它需要资源ID,但我该怎么做呢?

请注意:我没有构建应用程序,这不是Android工作室项目.我试图将光环移植到cyanogenmod 11.

Kar*_*rim 5

替换为Resources resources = getResources();,Resources resources = mContext.getResources();因为您已经引用了Context.

同样的getPackageName().替换为mContext.getPackageName().