0

I am using TextInputLayout as a container to hold country code and phone number. I have a custom style for this container.

<android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/signin_inputlayout_mobile"
        style="@style/UserAuthField">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Spinner
                android:layout_width="0dp"
                android:layout_weight="22"
                android:layout_height="wrap_content"
                style="@style/region_spinner"
                android:id="@+id/spinner"
                android:prompt="@string/Auth_DialCode">
            </Spinner>

            <EditText
                android:layout_width="0dp"
                android:layout_weight="78"
                style="@style/UserAuthField"
                android:inputType="number"
                android:id="@+id/usr_mobile_uname_login"
                android:hint="@string/Auth_Phone"/>

        </LinearLayout></android.support.design.widget.TextInputLayout>

The following is the style:

<style name="UserAuthField">
    <item name="android:background">@drawable/etborder</item>
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textColor">#333333</item>
    <item name="android:textColorHint">#757575</item>
    <item name="android:paddingBottom">4dp</item>
    <item name="android:paddingTop">8dp</item>
</style>

Here's the layer-list:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:bottom="1dp" android:left="-3dp" android:right="-3dp" android:top="-3dp">
    <shape android:shape="rectangle">
        <stroke android:width="1dp" android:color="#00FFFFFF"/>
    </shape>
</item>
</layer-list>

When I try to setError on this, I get the following error:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setBackgroundTintList(android.content.res.ColorStateList)' on a null object reference

FYI, the same strategy worked earlier, but in this instance, something seems a miss. Can't figure out why?!

Pallav Bajjuri
  • 65
  • 1
  • 1
  • 4

1 Answers1

0

The big mistake that I did was in the definition of the root element of the layout:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ripple="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
**tools:context="com.viven.android.kahaniya.ActivitySetup"**
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

As I understand from this answer, for styles to work, they have to be defined with local context and not application level context.

Now, I still don't fully understand what that means. I will read up a little more on it, but if you have additional info, pls do share.

Community
  • 1
  • 1
Pallav Bajjuri
  • 65
  • 1
  • 1
  • 4