0

我在使用 android 交叉淡入淡出视图时遇到了一些问题。我正在尝试在两张图片之间来回淡入淡出。我可以加载图像,但是一旦我按下淡入淡出按钮,应用程序就会崩溃。我尝试遵循 android 网站上的代码并进行修改以满足我的需要。我不太确定我是否正确设置了 XML 文件来执行多个视图。我已经尝试过 FrameLayout 内部的 ImageViews 并且也没有 FrameLayout。不知道如何正确设置它。谢谢你的时间。我在下面包含了 java、xml 和 logcat:

MainActivity.java

public class MainActivity extends ActionBarActivity {

    private View imageView1;
    private View imageView2;
    private int mShortAnimationDuration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }

        imageView1 = findViewById(R.id.imageView1);
        imageView2 = findViewById(R.id.imageView2);

        // Initially hide the content view.
        imageView2.setVisibility(View.GONE);

        // Retrieve and cache the system's default "short" animation time.
        mShortAnimationDuration = getResources().getInteger(android.R.integer.config_shortAnimTime);
    }

    private void crossfade() {

        // Set the content view to 0% opacity but visible, so that it is visible
        // (but fully transparent) during the animation.
        imageView1.setAlpha(0f);
        imageView1.setVisibility(View.VISIBLE);

        // Animate the content view to 100% opacity, and clear any animation
        // listener set on the view.
        imageView1.animate()
                .alpha(1f)
                .setDuration(mShortAnimationDuration)
                .setListener(null);

        // Animate the loading view to 0% opacity. After the animation ends,
        // set its visibility to GONE as an optimization step (it won't
        // participate in layout passes, etc.)
        imageView2.animate()
                .alpha(0f)
                .setDuration(mShortAnimationDuration)
                .setListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        imageView2.setVisibility(View.GONE);
                    }
                });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            return rootView;
        }
    }

}

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="ca.mohawk.you.lab6a.MainActivity"
    tools:ignore="MergeRootFrame">

    <FrameLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:src="@drawable/desert" />
    </FrameLayout>

    <FrameLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:src="@drawable/jellyfish" />
    </FrameLayout>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="42dp"
        android:text="@string/button_click"
        android:onClick="crossfade" />

</RelativeLayout>

日志猫

04-07 23:25:25.770: D/dalvikvm(819): Not late-enabling CheckJNI (already on)
04-07 23:25:30.500: D/dalvikvm(819): GC_FOR_ALLOC freed 65K, 5% free 2900K/3048K, paused 47ms, total 49ms
04-07 23:25:30.500: I/dalvikvm-heap(819): Grow heap (frag case) to 9.659MB for 7077904-byte allocation
04-07 23:25:30.560: D/dalvikvm(819): GC_FOR_ALLOC freed 2K, 2% free 9810K/9964K, paused 53ms, total 53ms
04-07 23:25:31.860: D/dalvikvm(819): GC_FOR_ALLOC freed <1K, 2% free 9811K/9964K, paused 24ms, total 24ms
04-07 23:25:31.870: I/dalvikvm-heap(819): Grow heap (frag case) to 16.408MB for 7077904-byte allocation
04-07 23:25:31.950: D/dalvikvm(819): GC_FOR_ALLOC freed <1K, 1% free 16723K/16880K, paused 78ms, total 78ms
04-07 23:25:32.900: D/(819): HostConnection::get() New Host Connection established 0xb860aab0, tid 819
04-07 23:25:33.070: W/EGL_emulation(819): eglSurfaceAttrib not implemented
04-07 23:25:33.090: D/OpenGLRenderer(819): Enabling debug mode 0
04-07 23:26:37.049: D/dalvikvm(1073): GC_FOR_ALLOC freed 54K, 5% free 2900K/3040K, paused 34ms, total 38ms
04-07 23:26:37.049: I/dalvikvm-heap(1073): Grow heap (frag case) to 9.659MB for 7077904-byte allocation
04-07 23:26:37.089: D/dalvikvm(1073): GC_FOR_ALLOC freed 2K, 2% free 9810K/9956K, paused 34ms, total 34ms
04-07 23:26:38.059: D/dalvikvm(1073): GC_FOR_ALLOC freed <1K, 2% free 9811K/9956K, paused 22ms, total 23ms
04-07 23:26:38.059: I/dalvikvm-heap(1073): Grow heap (frag case) to 16.408MB for 7077904-byte allocation
04-07 23:26:38.109: D/dalvikvm(1073): GC_FOR_ALLOC freed <1K, 1% free 16723K/16872K, paused 40ms, total 40ms
04-07 23:26:38.859: D/(1073): HostConnection::get() New Host Connection established 0xb860aac8, tid 1073
04-07 23:26:39.019: W/EGL_emulation(1073): eglSurfaceAttrib not implemented
04-07 23:26:39.049: D/OpenGLRenderer(1073): Enabling debug mode 0
04-07 23:26:40.159: I/Choreographer(1073): Skipped 44 frames!  The application may be doing too much work on its main thread.
04-07 23:26:41.959: D/AndroidRuntime(1073): Shutting down VM
04-07 23:26:41.959: W/dalvikvm(1073): threadid=1: thread exiting with uncaught exception (group=0xb3a2dba8)
04-07 23:26:41.979: E/AndroidRuntime(1073): FATAL EXCEPTION: main
04-07 23:26:41.979: E/AndroidRuntime(1073): Process: ca.mohawk.you.lab6a, PID: 1073
04-07 23:26:41.979: E/AndroidRuntime(1073): java.lang.IllegalStateException: Could not find a method crossfade(View) in the activity class ca.mohawk.you.lab6a.MainActivity for onClick handler on view class android.widget.Button with id 'button'
04-07 23:26:41.979: E/AndroidRuntime(1073):     at android.view.View$1.onClick(View.java:3810)
04-07 23:26:41.979: E/AndroidRuntime(1073):     at android.view.View.performClick(View.java:4438)
04-07 23:26:41.979: E/AndroidRuntime(1073):     at android.view.View$PerformClick.run(View.java:18422)
04-07 23:26:41.979: E/AndroidRuntime(1073):     at android.os.Handler.handleCallback(Handler.java:733)
04-07 23:26:41.979: E/AndroidRuntime(1073):     at android.os.Handler.dispatchMessage(Handler.java:95)
04-07 23:26:41.979: E/AndroidRuntime(1073):     at android.os.Looper.loop(Looper.java:136)
04-07 23:26:41.979: E/AndroidRuntime(1073):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-07 23:26:41.979: E/AndroidRuntime(1073):     at java.lang.reflect.Method.invokeNative(Native Method)
04-07 23:26:41.979: E/AndroidRuntime(1073):     at java.lang.reflect.Method.invoke(Method.java:515)
04-07 23:26:41.979: E/AndroidRuntime(1073):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-07 23:26:41.979: E/AndroidRuntime(1073):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-07 23:26:41.979: E/AndroidRuntime(1073):     at dalvik.system.NativeStart.main(Native Method)
04-07 23:26:41.979: E/AndroidRuntime(1073): Caused by: java.lang.NoSuchMethodException: crossfade [class android.view.View]
04-07 23:26:41.979: E/AndroidRuntime(1073):     at java.lang.Class.getConstructorOrMethod(Class.java:472)
04-07 23:26:41.979: E/AndroidRuntime(1073):     at java.lang.Class.getMethod(Class.java:857)
04-07 23:26:41.979: E/AndroidRuntime(1073):     at android.view.View$1.onClick(View.java:3803)
04-07 23:26:41.979: E/AndroidRuntime(1073):     ... 11 more
04-07 23:26:46.359: I/Process(1073): Sending signal. PID: 1073 SIG: 9
04-07 23:28:55.859: D/dalvikvm(1126): GC_FOR_ALLOC freed 58K, 5% free 2900K/3044K, paused 36ms, total 38ms
04-07 23:28:55.859: I/dalvikvm-heap(1126): Grow heap (frag case) to 9.659MB for 7077904-byte allocation
04-07 23:28:55.909: D/dalvikvm(1126): GC_FOR_ALLOC freed 2K, 2% free 9810K/9960K, paused 43ms, total 43ms
04-07 23:28:56.779: D/dalvikvm(1126): GC_FOR_ALLOC freed <1K, 2% free 9811K/9960K, paused 22ms, total 22ms
04-07 23:28:56.779: I/dalvikvm-heap(1126): Grow heap (frag case) to 16.408MB for 7077904-byte allocation
04-07 23:28:56.839: D/dalvikvm(1126): GC_FOR_ALLOC freed <1K, 1% free 16723K/16876K, paused 50ms, total 50ms
04-07 23:28:57.539: D/(1126): HostConnection::get() New Host Connection established 0xb876ca90, tid 1126
04-07 23:28:57.629: W/EGL_emulation(1126): eglSurfaceAttrib not implemented
04-07 23:28:57.639: D/OpenGLRenderer(1126): Enabling debug mode 0
04-07 23:29:00.669: D/AndroidRuntime(1126): Shutting down VM
04-07 23:29:00.679: W/dalvikvm(1126): threadid=1: thread exiting with uncaught exception (group=0xb3a2dba8)
04-07 23:29:00.699: E/AndroidRuntime(1126): FATAL EXCEPTION: main
04-07 23:29:00.699: E/AndroidRuntime(1126): Process: ca.mohawk.you.lab6a, PID: 1126
04-07 23:29:00.699: E/AndroidRuntime(1126): java.lang.IllegalStateException: Could not find a method crossfade(View) in the activity class ca.mohawk.you.lab6a.MainActivity for onClick handler on view class android.widget.Button with id 'button'
04-07 23:29:00.699: E/AndroidRuntime(1126):     at android.view.View$1.onClick(View.java:3810)
04-07 23:29:00.699: E/AndroidRuntime(1126):     at android.view.View.performClick(View.java:4438)
04-07 23:29:00.699: E/AndroidRuntime(1126):     at android.view.View$PerformClick.run(View.java:18422)
04-07 23:29:00.699: E/AndroidRuntime(1126):     at android.os.Handler.handleCallback(Handler.java:733)
04-07 23:29:00.699: E/AndroidRuntime(1126):     at android.os.Handler.dispatchMessage(Handler.java:95)
04-07 23:29:00.699: E/AndroidRuntime(1126):     at android.os.Looper.loop(Looper.java:136)
04-07 23:29:00.699: E/AndroidRuntime(1126):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-07 23:29:00.699: E/AndroidRuntime(1126):     at java.lang.reflect.Method.invokeNative(Native Method)
04-07 23:29:00.699: E/AndroidRuntime(1126):     at java.lang.reflect.Method.invoke(Method.java:515)
04-07 23:29:00.699: E/AndroidRuntime(1126):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-07 23:29:00.699: E/AndroidRuntime(1126):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-07 23:29:00.699: E/AndroidRuntime(1126):     at dalvik.system.NativeStart.main(Native Method)
04-07 23:29:00.699: E/AndroidRuntime(1126): Caused by: java.lang.NoSuchMethodException: crossfade [class android.view.View]
04-07 23:29:00.699: E/AndroidRuntime(1126):     at java.lang.Class.getConstructorOrMethod(Class.java:472)
04-07 23:29:00.699: E/AndroidRuntime(1126):     at java.lang.Class.getMethod(Class.java:857)
04-07 23:29:00.699: E/AndroidRuntime(1126):     at android.view.View$1.onClick(View.java:3803)
04-07 23:29:00.699: E/AndroidRuntime(1126):     ... 11 more
04-07 23:34:46.919: D/dalvikvm(1179): GC_FOR_ALLOC freed 61K, 5% free 2902K/3044K, paused 35ms, total 37ms
04-07 23:34:46.919: I/dalvikvm-heap(1179): Grow heap (frag case) to 9.660MB for 7077904-byte allocation
04-07 23:34:46.959: D/dalvikvm(1179): GC_FOR_ALLOC freed 2K, 2% free 9811K/9960K, paused 31ms, total 31ms
04-07 23:34:47.839: D/dalvikvm(1179): GC_FOR_ALLOC freed <1K, 2% free 9814K/9960K, paused 21ms, total 22ms
04-07 23:34:47.839: I/dalvikvm-heap(1179): Grow heap (frag case) to 16.410MB for 7077904-byte allocation
04-07 23:34:47.889: D/dalvikvm(1179): GC_FOR_ALLOC freed <1K, 1% free 16725K/16876K, paused 40ms, total 41ms
04-07 23:34:48.579: D/(1179): HostConnection::get() New Host Connection established 0xb87602e8, tid 1179
04-07 23:34:48.689: W/EGL_emulation(1179): eglSurfaceAttrib not implemented
04-07 23:34:48.699: D/OpenGLRenderer(1179): Enabling debug mode 0
04-07 23:35:00.309: D/AndroidRuntime(1179): Shutting down VM
04-07 23:35:00.319: W/dalvikvm(1179): threadid=1: thread exiting with uncaught exception (group=0xb3a2dba8)
04-07 23:35:00.349: E/AndroidRuntime(1179): FATAL EXCEPTION: main
04-07 23:35:00.349: E/AndroidRuntime(1179): Process: ca.mohawk.you.lab6a, PID: 1179
04-07 23:35:00.349: E/AndroidRuntime(1179): java.lang.IllegalStateException: Could not find a method crossfade(View) in the activity class ca.mohawk.you.lab6a.MainActivity for onClick handler on view class android.widget.Button with id 'button'
04-07 23:35:00.349: E/AndroidRuntime(1179):     at android.view.View$1.onClick(View.java:3810)
04-07 23:35:00.349: E/AndroidRuntime(1179):     at android.view.View.performClick(View.java:4438)
04-07 23:35:00.349: E/AndroidRuntime(1179):     at android.view.View$PerformClick.run(View.java:18422)
04-07 23:35:00.349: E/AndroidRuntime(1179):     at android.os.Handler.handleCallback(Handler.java:733)
04-07 23:35:00.349: E/AndroidRuntime(1179):     at android.os.Handler.dispatchMessage(Handler.java:95)
04-07 23:35:00.349: E/AndroidRuntime(1179):     at android.os.Looper.loop(Looper.java:136)
04-07 23:35:00.349: E/AndroidRuntime(1179):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-07 23:35:00.349: E/AndroidRuntime(1179):     at java.lang.reflect.Method.invokeNative(Native Method)
04-07 23:35:00.349: E/AndroidRuntime(1179):     at java.lang.reflect.Method.invoke(Method.java:515)
04-07 23:35:00.349: E/AndroidRuntime(1179):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-07 23:35:00.349: E/AndroidRuntime(1179):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-07 23:35:00.349: E/AndroidRuntime(1179):     at dalvik.system.NativeStart.main(Native Method)
04-07 23:35:00.349: E/AndroidRuntime(1179): Caused by: java.lang.NoSuchMethodException: crossfade [class android.view.View]
04-07 23:35:00.349: E/AndroidRuntime(1179):     at java.lang.Class.getConstructorOrMethod(Class.java:472)
04-07 23:35:00.349: E/AndroidRuntime(1179):     at java.lang.Class.getMethod(Class.java:857)
04-07 23:35:00.349: E/AndroidRuntime(1179):     at android.view.View$1.onClick(View.java:3803)
04-07 23:35:00.349: E/AndroidRuntime(1179):     ... 11 more
04-07 23:35:03.109: I/Process(1179): Sending signal. PID: 1179 SIG: 9
04-07 23:35:36.759: D/dalvikvm(1221): GC_FOR_ALLOC freed 65K, 5% free 2902K/3048K, paused 38ms, total 40ms
04-07 23:35:36.769: I/dalvikvm-heap(1221): Grow heap (frag case) to 9.660MB for 7077904-byte allocation
04-07 23:35:36.839: D/dalvikvm(1221): GC_FOR_ALLOC freed 2K, 2% free 9811K/9964K, paused 68ms, total 68ms
04-07 23:35:37.799: D/dalvikvm(1221): GC_FOR_ALLOC freed <1K, 2% free 9814K/9964K, paused 23ms, total 23ms
04-07 23:35:37.799: I/dalvikvm-heap(1221): Grow heap (frag case) to 16.411MB for 7077904-byte allocation
04-07 23:35:37.859: D/dalvikvm(1221): GC_FOR_ALLOC freed <1K, 1% free 16725K/16880K, paused 47ms, total 47ms
04-07 23:35:38.599: D/(1221): HostConnection::get() New Host Connection established 0xb8738680, tid 1221
04-07 23:35:38.749: W/EGL_emulation(1221): eglSurfaceAttrib not implemented
04-07 23:35:38.769: D/OpenGLRenderer(1221): Enabling debug mode 0
04-07 23:35:42.339: D/AndroidRuntime(1221): Shutting down VM
04-07 23:35:42.339: W/dalvikvm(1221): threadid=1: thread exiting with uncaught exception (group=0xb3a2dba8)
04-07 23:35:42.409: E/AndroidRuntime(1221): FATAL EXCEPTION: main
04-07 23:35:42.409: E/AndroidRuntime(1221): Process: ca.mohawk.you.lab6a, PID: 1221
04-07 23:35:42.409: E/AndroidRuntime(1221): java.lang.IllegalStateException: Could not find a method crossfade(View) in the activity class ca.mohawk.you.lab6a.MainActivity for onClick handler on view class android.widget.Button with id 'button'
04-07 23:35:42.409: E/AndroidRuntime(1221):     at android.view.View$1.onClick(View.java:3810)
04-07 23:35:42.409: E/AndroidRuntime(1221):     at android.view.View.performClick(View.java:4438)
04-07 23:35:42.409: E/AndroidRuntime(1221):     at android.view.View$PerformClick.run(View.java:18422)
04-07 23:35:42.409: E/AndroidRuntime(1221):     at android.os.Handler.handleCallback(Handler.java:733)
04-07 23:35:42.409: E/AndroidRuntime(1221):     at android.os.Handler.dispatchMessage(Handler.java:95)
04-07 23:35:42.409: E/AndroidRuntime(1221):     at android.os.Looper.loop(Looper.java:136)
04-07 23:35:42.409: E/AndroidRuntime(1221):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-07 23:35:42.409: E/AndroidRuntime(1221):     at java.lang.reflect.Method.invokeNative(Native Method)
04-07 23:35:42.409: E/AndroidRuntime(1221):     at java.lang.reflect.Method.invoke(Method.java:515)
04-07 23:35:42.409: E/AndroidRuntime(1221):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-07 23:35:42.409: E/AndroidRuntime(1221):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-07 23:35:42.409: E/AndroidRuntime(1221):     at dalvik.system.NativeStart.main(Native Method)
04-07 23:35:42.409: E/AndroidRuntime(1221): Caused by: java.lang.NoSuchMethodException: crossfade [class android.view.View]
04-07 23:35:42.409: E/AndroidRuntime(1221):     at java.lang.Class.getConstructorOrMethod(Class.java:472)
04-07 23:35:42.409: E/AndroidRuntime(1221):     at java.lang.Class.getMethod(Class.java:857)
04-07 23:35:42.409: E/AndroidRuntime(1221):     at android.view.View$1.onClick(View.java:3803)
04-07 23:35:42.409: E/AndroidRuntime(1221):     ... 11 more
04-07 23:35:44.729: I/Process(1221): Sending signal. PID: 1221 SIG: 9
4

1 回答 1

2

在xml中声明onClick属性时,Java代码中对应的方法需要有一定的签名。改变:

private void crossfade()

至:

public void crossfade(View v)
于 2014-04-08T03:54:20.487 回答