Getting the error expected resource of type attr

1k views Asked by At

I am trying to use the Home Key Locker https://github.com/shaobin0604/Android-HomeKey-Locker I want to be able to detect and prevent homescreen button click on the lock screen. Majority of the answers mentioned that it cannot be disabled.

This is the HomeKeyLocker Class :

 package com.example.harshilshah.screenonoff;

  import android.app.Activity;
  import android.app.AlertDialog;
  import android.os.Bundle;
  import android.view.Gravity;
  import android.view.MotionEvent;
  import android.view.WindowManager;
  import android.widget.FrameLayout;

  import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
  import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
  import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;


public class HomeKeyLocker {
private OverlayDialog mOverlayDialog;

public void lock(Activity activity) {
    if (mOverlayDialog == null) {
        mOverlayDialog = new OverlayDialog(activity);
        mOverlayDialog.show();
    }
}

public void unlock() {
    if (mOverlayDialog != null) {
        mOverlayDialog.dismiss();
        mOverlayDialog = null;
    }
}

  public static class OverlayDialog extends AlertDialog {

    public OverlayDialog(Activity activity) {
        super(activity, R.style.OverlayDialog);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.type = TYPE_SYSTEM_ERROR;
        params.dimAmount = 0.0F; // transparent
        params.width = 0;
        params.height = 0;
        params.gravity = Gravity.BOTTOM;
        getWindow().setAttributes(params);
        getWindow().setFlags(FLAG_SHOW_WHEN_LOCKED | FLAG_NOT_TOUCH_MODAL, 0xffffff);
        setOwnerActivity(activity);
        setCancelable(false);
    }

    public final boolean dispatchTouchEvent(MotionEvent motionevent) {
        return true;
    }

    protected final void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        FrameLayout framelayout = new FrameLayout(getContext());
        framelayout.setBackgroundColor(0);
        setContentView(framelayout);
    }
  }
}

This is the styles.xml

 <resources>

 <!-- Base application theme. -->
 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="windowActionBar">false</item>
    <item name="android:windowNoTitle">true</item>
 </style>

<style name="OverlayDialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowFrame">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowIsTranslucent">false</item>
    <item name="android:backgroundDimEnabled">false</item>
</style>

</resources>

I am getting an error Expected resource of type Attr in the following lines

 public OverlayDialog(Activity activity) {
        super(activity, R.style.OverlayDialog);//ERROR

How do i solve this?

1

There are 1 answers

0
rjr-apps On

I just had this issue. What caused it for me was that I imported the android.R class, instead of the R class already defined in my app. So it wasn't using the styles.xml defined in my app's values, where the OverlayDialog was implemented, but the standard one defined in the Android system that obviously didn't have it. Once I deleted import android.R and added import com.example.myapp.R it worked just fine.