-1

login.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/my_bg_anim"
android:id="@+id/myLayout"
tools:context="com.example.elibuser14.assignment2.login">

<EditText
    android:id="@+id/etName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/etPassword"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"

    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="144dp"
    android:ems="10"
    android:hint="User Name"
    android:inputType="textPersonName"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.287" />

<EditText
    android:id="@+id/etPassword"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/etName"
    android:layout_alignParentBottom="true"
    android:layout_alignStart="@+id/etName"

    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:ems="10"
    android:hint="Password"
    android:inputType="textPersonName"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.503"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.639" />

<Button
    android:id="@+id/btLogin"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_above="@+id/tvInfo"
    android:layout_alignEnd="@+id/tvInfo"
    android:layout_alignRight="@+id/tvInfo"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="364dp"
    android:text="LOGIN"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/tvInfo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/etPassword"
    android:layout_alignParentBottom="true"
    android:layout_alignStart="@+id/etPassword"
    android:text="No of attempts remaining"
    tools:layout_editor_absoluteX="97dp"
    tools:layout_editor_absoluteY="521dp" />

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:theme="@style/AppTheme.AppBarOverlay"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="395dp"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

</android.support.constraint.ConstraintLayout>

login java file

public class login extends AppCompatActivity {

ConstraintLayout myLayout;
AnimationDrawable animationDrawable;
@Override
    protected void onCreate(Bundle savedInstanceState) {
        myLayout = (ConstraintLayout) findViewById(R.id.myLayout);

        animationDrawable = (AnimationDrawable) myLayout.getBackground();
        animationDrawable.setEnterFadeDuration(4500);
        animationDrawable.setExitFadeDuration(4500);
        animationDrawable.start();
}

The logcat shows

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.drawable.Drawable android.support.constraint.ConstraintLayout.getBackground()' on a null object reference
at com.example.elibuser14.assignment2.login.onCreate(login.java:71)

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

In onCreate, you need to setContentView(R.layout.login.xml) before assigning and accessing any views via findViewById().

terencey
  • 3,282
  • 4
  • 32
  • 40