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>
登录java文件
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();
}
logcat显示
引起:java.lang.NullPointerException:尝试调用虚拟 方法'android.graphics.drawable.Drawable android.support.constraint.ConstraintLayout.getBackground()'上的null 对象参考
在com.example.elibuser14.assignment2.login.onCreate(login.java:71)
答案 0 :(得分:0)
在onCreate
中,您需要setContentView(R.layout.login.xml)
才能通过findViewById()
分配和访问任何观看次数。