1

我正在尝试制作基本的应用程序,将一个图像交叉淡化为另一个图像,然后再返回。但是,当我单击第一张图片时,该应用程序不断崩溃。

交叉淡入淡出方法fade独立工作。自从我添加了第二种将图像转换回来的方法以来,它刚刚开始崩溃。

我看到了InvocationTargetExceptionNullPointerException但我不明白这是如何/为什么会发生的。还有onClick设置为正确的方法。

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);
    }
}

活动主要 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>

错误:

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) 
4

1 回答 1

1

您正在初始化视图obama,并且will仅在fade(). If在和的值fadeBack()之前被调用,两者都为空。这就是为什么你得到一个空指针异常。fade()obamawill

将此代码从 移动fade()onCreate(),

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

这样它就会在视图创建本身时启动。

MainActivity 应如下所示:

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);

        ......

    }

    ......
}
于 2015-12-29T18:02:58.303 回答