1

When trying to inflate View like this:

val lay: LayoutInflater = getSystemService(LAYOUT_INFLATER_SERVICE) as (LayoutInflater)
mView = lay.inflate(R.layout.dialog_new_order, null)

I see a inflating exception:

Binary XML file line #0: Failed to resolve attribute at index 1: TypedValue{t=0x2/d=0x7f030006 a=-1}

But when write the same lines in activity it's working normally !

View XML :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:background="@color/white"
    android:layout_margin="10dp"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary"
        android:drawableStart="@mipmap/ic_launcher"
        android:gravity="center_vertical"
        android:drawablePadding="20dp"
        android:text="@string/new_order"
        android:textColor="@color/white"
        android:textSize="@dimen/text_size_large" />

    <ImageView
        android:id="@+id/order_image"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp" />

    <TextView
        android:id="@+id/order_rest_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:textColor="@color/black"
        android:textSize="@dimen/text_size_xlarge" />

    <TextView
        android:id="@+id/order_customer_info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"
        android:textColor="@color/black"
        android:textSize="@dimen/text_size_normal" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="40dp"
        android:layout_marginRight="40dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/bg_btns_border"
        android:orientation="horizontal"
        android:weightSum="1">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:padding="10dp"
            android:orientation="vertical">

            <TextView
                android:id="@+id/order_price"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/black"
                android:gravity="center"
                android:textSize="@dimen/text_size_large" />

            <TextView
                android:id="@+id/order_pay_method"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/black"
                android:gravity="center" />


        </LinearLayout>

        <View
            android:layout_width="0.5dp"
            android:layout_height="match_parent"
            android:background="@color/gray_lite" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:padding="10dp"

            android:orientation="vertical">

            <TextView
                android:id="@+id/order_delivery_time"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="0 Min"
                android:singleLine="true"
                android:ellipsize="end"
                android:textColor="@color/black"
                android:textSize="@dimen/text_size_large" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textColor="@color/black"
                android:text="@string/to_restaurant" />
        </LinearLayout>
    </LinearLayout>

    <Button
        android:id="@+id/order_accept_btn"
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:layout_margin="10dp"
        android:background="@drawable/bg_btns_rounded_green"
        android:text="@string/order_status_start_pickup"
        android:textAllCaps="true"
        android:textSize="@dimen/text_size_large"
        android:textColor="@color/white" />

</LinearLayout>
Mark Adler
  • 101,978
  • 13
  • 118
  • 158
Hani Hussein
  • 159
  • 4
  • 5
    Service is an activity without a UI. What are you trying to accomplish here? What are you expecting to do with the layout you want to inflate from a Service? – Alessio Nov 29 '17 at 04:33

1 Answers1

1

as per say's Alessio that is right Service has no UI so you can't use inflater on it if you want to show view on Activity from your service then pass your Activity Context and Show View

LayoutInflater lay=(LayoutInflater)Context.getSystemService(LAYOUT_INFLATER_SERVICE)

mView = lay.inflate(R.layout.dialog_new_order, null)
Divyesh Kalotra
  • 204
  • 1
  • 2
  • 13