javaandroidcross-fade

Android studio crossfade drawables crashes app


I am trying to make basic app that crossfades one image for another and then back again. The app however keeps crashing when I click the first image.

The crossfade method fade works on it's own. It has just begun crashing since I added the second method to transition the images back.

I see the InvocationTargetException and the NullPointerException but I don't understand how/why this is happening. Also the onClick within the content setup is set to the correct methods.

package com.example.richardcurteis.layoutdemo;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    ImageView obama;
    ImageView will;

    public void fade(View view) {

        obama = (ImageView) findViewById(R.id.obama);
        will = (ImageView) findViewById(R.id.will);

        obama.animate().alpha(0f).setDuration(2000);
        will.animate().alpha(1f).setDuration(2000);

    }

    public void fadeBack(View view) {
        will.animate().alpha(1f).setDuration(2000);
        obama.animate().alpha(0f).setDuration(2000);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Activity Main xml:

<?xml version="1.0" encoding="utf-8"?>
    <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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="com.example.richardcurteis.layoutdemo.MainActivity"
        tools:showIn="@layout/activity_main"
        android:clickable="false">

         <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/obama"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true"
             android:src="@drawable/obama"
             android:onClick="fade" />

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/will"
            android:layout_alignTop="@+id/obama"
            android:layout_alignParentStart="true"
            android:src="@drawable/will"
            android:alpha="0"
            android:onClick="fadeBack" />
    </RelativeLayout>

Errors:

12-29 17:17:32.003 3368-3368/com.example.richardcurteis.layoutdemo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.richardcurteis.layoutdemo, PID: 3368
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:275)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
at android.view.View.performClick(View.java:5198) 
at android.view.View$PerformClick.run(View.java:21147) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewPropertyAnimator android.widget.ImageView.animate()' on a null object reference
at com.example.richardcurteis.layoutdemo.MainActivity.fadeBack(MainActivity.java:29)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270) 
at android.view.View.performClick(View.java:5198) 
at android.view.View$PerformClick.run(View.java:21147) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

Solution

  • You are initializing views obama and will only in fade(). If fadeBack() is called before fade() values of obama and will are both null. That's why you are getting a null pointer exception.

    Move this code from fade() to onCreate(),

    obama = (ImageView) findViewById(R.id.obama);
    will = (ImageView) findViewById(R.id.will);
    

    So that it will get initiated at the time of view creation itself.

    MainActivity should look like this :

    public class MainActivity extends AppCompatActivity {
        ImageView obama;
        ImageView will;
    
        public void fade(View view) {
            obama.animate().alpha(0f).setDuration(2000);
            will.animate().alpha(1f).setDuration(2000);
    
        }
    
        public void fadeBack(View view) {
            will.animate().alpha(1f).setDuration(2000);
            obama.animate().alpha(0f).setDuration(2000);
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            obama = (ImageView) findViewById(R.id.obama);
            will = (ImageView) findViewById(R.id.will);
    
            ......
    
        }
    
        ......
    }