在服务中使用WindowManager时,自定义视图不会膨胀

时间:2018-07-12 04:32:58

标签: android service android-custom-view android-windowmanager

我正在尝试在服务中使用WindowManager添加自定义的KnockView。这是KnockView类。

    public class KnowView extends LinearLayout {
    public KnowView(Context context) {
        super(context);
        initView();
    }

    public KnowView(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.KnowView);
        try {
            isFromChangeKnock = typedArray.getBoolean(R.styleable.KnowView_changeKnock, false);
        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            typedArray.recycle();
        }
        initView();
    }

    public KnowView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }
     private void initView() {
        mainLayout = inflate(getContext(), R.layout.knock_code, this);
    }

}

因此,基本上,在KnowView类中,我们将另一种布局(knock_code)夸大并将其添加到KnowView中。之后,我在另一个布局中使用此自定义Knockview。 main_view.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/parent_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <production.kado.lock.views.KnowView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:paddingBottom="20dp"
        app:changeKnock="true" />
    </RelativeLayout>

并在这样的WindowManager中使main_view.xml投入使用。

WindowManager windowManager = ((WindowManager) 
this.getSystemService(WINDOW_SERVICE));

View view1 = LayoutInflater.from(this).inflate(R.layout.main_view, null);
windowManager.addView(mainView, getViewParam(true));

但是运行此服务时,我遇到了这些异常。

07-12 09:16:34.087 12020-12020/production.kado.lock E/AndroidRuntime: FATAL EXCEPTION: main
Process: production.kado.lock, PID: 12020
java.lang.RuntimeException: Unable to create service production.kado.lock.services.LockService: android.view.InflateException: Binary XML file line #47: Binary XML file line #47: Error inflating class <unknown>
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3252)
    at android.app.ActivityThread.-wrap5(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1594)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6247)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
 Caused by: android.view.InflateException: Binary XML file line #47: Binary XML file line #47: Error inflating class <unknown>
 Caused by: android.view.InflateException: Binary XML file line #47: Error inflating class <unknown>
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.newInstance0(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
    at android.view.LayoutInflater.createView(LayoutInflater.java:645)
    at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:717)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:785)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:861)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:861)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
    at android.view.View.inflate(View.java:21008)
    at production.kado.lock.views.KnowView.findAllViews(KnowView.java:115)
    at production.kado.lock.views.KnowView.initView(KnowView.java:99)
    at production.kado.lock.views.KnowView.<init>(KnowView.java:46)
    at production.kado.lock.services.LockService.addLockView(LockService.java:87)
    at production.kado.lock.services.LockService.onCreate(LockService.java:69)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3242)
    at android.app.ActivityThread.-wrap5(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1594)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6247)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
 Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 35: TypedValue{t=0x2/d=0x7f02012d a=-1}

我还在活动中使用了KnockView,但是在活动中,它工作正常。唯一的问题是与WindowManager一起用于服务中。

1 个答案:

答案 0 :(得分:0)

我发现了我正在使用的问题

android:background="?selectableItemBackground"
删除此按钮后,

我的布局按钮中的属性正常工作