0

这是我需要制作动画的船的课程:

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

public class Boat extends ImageView {

    ImageView boatimage;



    public Boat(Context context, AttributeSet attrs) {
        super(context, attrs);

    }


    OnClickListener boatForwardListener = new OnClickListener() {

        @Override
        public void onClick(View v) {

            TranslateAnimation animation = new TranslateAnimation(0, 0, 0, -290);
            animation.setDuration(250);
            animation.setFillAfter(true);
            boatimage.startAnimation(animation);

        }

    };



}

这是我的主要活动:

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View.OnClickListener;



public class MainActivity extends Activity {


    OnClickListener boatForwardListener;

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

        Boat boat = new Boat(this, null);
        boat = (Boat)findViewById(R.id.imageView1);
        boat.setOnClickListener(boatForwardListener);   


    }

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


}

这是我的xml布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:src="@drawable/redsquare" />

    <ImageButton
        android:id="@+id/imageButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/imageButton2"
        android:layout_toRightOf="@+id/imageButton2"
        android:src="@drawable/redsquare" />

    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/imageButton1"
        android:layout_toRightOf="@+id/imageButton1"
        android:src="@drawable/redsquare" />

    <ImageButton
        android:id="@+id/imageButton4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/imageButton3"
        android:layout_toRightOf="@+id/imageButton3"
        android:src="@drawable/greensquare" />

    <ImageButton
        android:id="@+id/imageButton5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/imageButton4"
        android:layout_toRightOf="@+id/imageButton4"
        android:src="@drawable/greensquare" />

    <ImageButton
        android:id="@+id/imageButton6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/imageButton5"
        android:layout_toRightOf="@+id/imageButton5"
        android:src="@drawable/greensquare" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/imageButton4"
        android:layout_alignRight="@+id/imageButton5"
        android:layout_marginBottom="102dp"
        android:layout_marginRight="14dp"
        android:src="@drawable/boat" />

</RelativeLayout>

最后是堆栈跟踪:

**09-20 06:46:55.957: E/Trace(6309): error opening trace file: No such file or directory (2)
09-20 06:46:57.107: D/dalvikvm(6309): GC_FOR_ALLOC freed 57K, 7% free 2556K/2732K, paused 42ms, total 47ms
09-20 06:46:57.127: I/dalvikvm-heap(6309): Grow heap (frag case) to 5.245MB for 2764816-byte allocation
09-20 06:46:57.178: D/dalvikvm(6309): GC_FOR_ALLOC freed 2K, 4% free 5254K/5436K, paused 46ms, total 46ms
09-20 06:46:57.229: D/dalvikvm(6309): GC_CONCURRENT freed <1K, 4% free 5270K/5436K, paused 4ms+4ms, total 58ms
09-20 06:46:57.288: D/AndroidRuntime(6309): Shutting down VM
09-20 06:46:57.288: W/dalvikvm(6309): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
09-20 06:46:57.308: E/AndroidRuntime(6309): FATAL EXCEPTION: main
09-20 06:46:57.308: E/AndroidRuntime(6309): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cannibal_photographer/com.cannibal_photographer.MainActivity}: java.lang.NullPointerException
09-20 06:46:57.308: E/AndroidRuntime(6309):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
09-20 06:46:57.308: E/AndroidRuntime(6309):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-20 06:46:57.308: E/AndroidRuntime(6309):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-20 06:46:57.308: E/AndroidRuntime(6309):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-20 06:46:57.308: E/AndroidRuntime(6309):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-20 06:46:57.308: E/AndroidRuntime(6309):     at android.os.Looper.loop(Looper.java:137)
09-20 06:46:57.308: E/AndroidRuntime(6309):     at android.app.ActivityThread.main(ActivityThread.java:5041)
09-20 06:46:57.308: E/AndroidRuntime(6309):     at java.lang.reflect.Method.invokeNative(Native Method)
09-20 06:46:57.308: E/AndroidRuntime(6309):     at java.lang.reflect.Method.invoke(Method.java:511)
09-20 06:46:57.308: E/AndroidRuntime(6309):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-20 06:46:57.308: E/AndroidRuntime(6309):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-20 06:46:57.308: E/AndroidRuntime(6309):     at dalvik.system.NativeStart.main(Native Method)
09-20 06:46:57.308: E/AndroidRuntime(6309): Caused by: java.lang.NullPointerException
09-20 06:46:57.308: E/AndroidRuntime(6309):     at com.cannibal_photographer.Boat.<init>(Boat.java:20)
09-20 06:46:57.308: E/AndroidRuntime(6309):     at com.cannibal_photographer.MainActivity.onCreate(MainActivity.java:21)
09-20 06:46:57.308: E/AndroidRuntime(6309):     at android.app.Activity.performCreate(Activity.java:5104)
09-20 06:46:57.308: E/AndroidRuntime(6309):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-20 06:46:57.308: E/AndroidRuntime(6309):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
09-20 06:46:57.308: E/AndroidRuntime(6309):     ... 11 more
09-20 06:46:59.757: I/Process(6309): Sending signal. PID: 6309 SIG: 9
09-20 06:47:15.908: E/Trace(6357): error opening trace file: No such file or directory (2)
09-20 06:47:17.088: D/dalvikvm(6357): GC_FOR_ALLOC freed 61K, 7% free 2556K/2736K, paused 183ms, total 185ms
09-20 06:47:17.258: I/dalvikvm-heap(6357): Grow heap (frag case) to 5.245MB for 2764816-byte allocation
09-20 06:47:17.307: D/dalvikvm(6357): GC_FOR_ALLOC freed 2K, 4% free 5254K/5440K, paused 43ms, total 43ms
09-20 06:47:17.357: D/dalvikvm(6357): GC_CONCURRENT freed <1K, 4% free 5271K/5440K, paused 5ms+4ms, total 58ms
09-20 06:47:17.668: D/gralloc_goldfish(6357): Emulator without GPU emulation detected.
09-20 07:07:17.318: D/dalvikvm(6461): GC_FOR_ALLOC freed 64K, 7% free 2556K/2740K, paused 142ms, total 146ms
09-20 07:07:17.338: I/dalvikvm-heap(6461): Grow heap (frag case) to 5.245MB for 2764816-byte allocation
09-20 07:07:17.378: D/dalvikvm(6461): GC_FOR_ALLOC freed 2K, 4% free 5254K/5444K, paused 37ms, total 37ms
09-20 07:07:17.428: D/dalvikvm(6461): GC_CONCURRENT freed <1K, 4% free 5254K/5444K, paused 6ms+4ms, total 49ms
09-20 07:07:17.497: D/AndroidRuntime(6461): Shutting down VM
09-20 07:07:17.497: W/dalvikvm(6461): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
09-20 07:07:17.507: E/AndroidRuntime(6461): FATAL EXCEPTION: main
09-20 07:07:17.507: E/AndroidRuntime(6461): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cannibal_photographer/com.cannibal_photographer.MainActivity}: java.lang.ClassCastException: android.widget.ImageView cannot be cast to com.cannibal_photographer.Boat
09-20 07:07:17.507: E/AndroidRuntime(6461):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
09-20 07:07:17.507: E/AndroidRuntime(6461):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-20 07:07:17.507: E/AndroidRuntime(6461):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-20 07:07:17.507: E/AndroidRuntime(6461):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-20 07:07:17.507: E/AndroidRuntime(6461):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-20 07:07:17.507: E/AndroidRuntime(6461):     at android.os.Looper.loop(Looper.java:137)
09-20 07:07:17.507: E/AndroidRuntime(6461):     at android.app.ActivityThread.main(ActivityThread.java:5041)
09-20 07:07:17.507: E/AndroidRuntime(6461):     at java.lang.reflect.Method.invokeNative(Native Method)
09-20 07:07:17.507: E/AndroidRuntime(6461):     at java.lang.reflect.Method.invoke(Method.java:511)
09-20 07:07:17.507: E/AndroidRuntime(6461):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-20 07:07:17.507: E/AndroidRuntime(6461):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-20 07:07:17.507: E/AndroidRuntime(6461):     at dalvik.system.NativeStart.main(Native Method)
09-20 07:07:17.507: E/AndroidRuntime(6461): Caused by: java.lang.ClassCastException: android.widget.ImageView cannot be cast to com.cannibal_photographer.Boat
09-20 07:07:17.507: E/AndroidRuntime(6461):     at com.cannibal_photographer.MainActivity.onCreate(MainActivity.java:22)
09-20 07:07:17.507: E/AndroidRuntime(6461):     at android.app.Activity.performCreate(Activity.java:5104)
09-20 07:07:17.507: E/AndroidRuntime(6461):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-20 07:07:17.507: E/AndroidRuntime(6461):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
09-20 07:07:17.507: E/AndroidRuntime(6461):     ... 11 more
09-20 07:07:22.547: I/Process(6461): Sending signal. PID: 6461 SIG: 9
09-20 07:09:21.168: D/dalvikvm(6510): GC_FOR_ALLOC freed 68K, 7% free 2556K/2744K, paused 29ms, total 31ms
09-20 07:09:21.189: I/dalvikvm-heap(6510): Grow heap (frag case) to 5.245MB for 2764816-byte allocation
09-20 07:09:21.228: D/dalvikvm(6510): GC_FOR_ALLOC freed 2K, 4% free 5254K/5448K, paused 36ms, total 36ms
09-20 07:09:21.278: D/dalvikvm(6510): GC_CONCURRENT freed <1K, 4% free 5254K/5448K, paused 4ms+5ms, total 57ms
09-20 07:09:21.348: D/AndroidRuntime(6510): Shutting down VM
09-20 07:09:21.348: W/dalvikvm(6510): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
09-20 07:09:21.358: E/AndroidRuntime(6510): FATAL EXCEPTION: main
09-20 07:09:21.358: E/AndroidRuntime(6510): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cannibal_photographer/com.cannibal_photographer.MainActivity}: java.lang.ClassCastException: android.widget.ImageView cannot be cast to com.cannibal_photographer.Boat
09-20 07:09:21.358: E/AndroidRuntime(6510):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
09-20 07:09:21.358: E/AndroidRuntime(6510):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-20 07:09:21.358: E/AndroidRuntime(6510):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-20 07:09:21.358: E/AndroidRuntime(6510):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-20 07:09:21.358: E/AndroidRuntime(6510):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-20 07:09:21.358: E/AndroidRuntime(6510):     at android.os.Looper.loop(Looper.java:137)
09-20 07:09:21.358: E/AndroidRuntime(6510):     at android.app.ActivityThread.main(ActivityThread.java:5041)
09-20 07:09:21.358: E/AndroidRuntime(6510):     at java.lang.reflect.Method.invokeNative(Native Method)
09-20 07:09:21.358: E/AndroidRuntime(6510):     at java.lang.reflect.Method.invoke(Method.java:511)
09-20 07:09:21.358: E/AndroidRuntime(6510):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-20 07:09:21.358: E/AndroidRuntime(6510):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-20 07:09:21.358: E/AndroidRuntime(6510):     at dalvik.system.NativeStart.main(Native Method)
09-20 07:09:21.358: E/AndroidRuntime(6510): Caused by: java.lang.ClassCastException: android.widget.ImageView cannot be cast to com.cannibal_photographer.Boat
09-20 07:09:21.358: E/AndroidRuntime(6510):     at com.cannibal_photographer.MainActivity.onCreate(MainActivity.java:22)
09-20 07:09:21.358: E/AndroidRuntime(6510):     at android.app.Activity.performCreate(Activity.java:5104)
09-20 07:09:21.358: E/AndroidRuntime(6510):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-20 07:09:21.358: E/AndroidRuntime(6510):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
09-20 07:09:21.358: E/AndroidRuntime(6510):     ... 11 more
09-20 07:09:24.178: I/Process(6510): Sending signal. PID: 6510 SIG: 9
09-20 07:20:12.277: E/Trace(6590): error opening trace file: No such file or directory (2)
09-20 07:20:13.257: D/dalvikvm(6590): GC_FOR_ALLOC freed 71K, 7% free 2556K/2748K, paused 29ms, total 31ms
09-20 07:20:13.287: I/dalvikvm-heap(6590): Grow heap (frag case) to 5.245MB for 2764816-byte allocation
09-20 07:20:13.337: D/dalvikvm(6590): GC_FOR_ALLOC freed 2K, 4% free 5254K/5452K, paused 48ms, total 48ms
09-20 07:20:13.387: D/dalvikvm(6590): GC_CONCURRENT freed <1K, 4% free 5257K/5452K, paused 5ms+14ms, total 51ms
09-20 07:20:13.437: D/AndroidRuntime(6590): Shutting down VM
09-20 07:20:13.437: W/dalvikvm(6590): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
09-20 07:20:13.447: E/AndroidRuntime(6590): FATAL EXCEPTION: main
09-20 07:20:13.447: E/AndroidRuntime(6590): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cannibal_photographer/com.cannibal_photographer.MainActivity}: java.lang.ClassCastException: android.widget.ImageView cannot be cast to com.cannibal_photographer.Boat
09-20 07:20:13.447: E/AndroidRuntime(6590):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
09-20 07:20:13.447: E/AndroidRuntime(6590):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-20 07:20:13.447: E/AndroidRuntime(6590):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-20 07:20:13.447: E/AndroidRuntime(6590):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-20 07:20:13.447: E/AndroidRuntime(6590):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-20 07:20:13.447: E/AndroidRuntime(6590):     at android.os.Looper.loop(Looper.java:137)
09-20 07:20:13.447: E/AndroidRuntime(6590):     at android.app.ActivityThread.main(ActivityThread.java:5041)
09-20 07:20:13.447: E/AndroidRuntime(6590):     at java.lang.reflect.Method.invokeNative(Native Method)
09-20 07:20:13.447: E/AndroidRuntime(6590):     at java.lang.reflect.Method.invoke(Method.java:511)
09-20 07:20:13.447: E/AndroidRuntime(6590):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-20 07:20:13.447: E/AndroidRuntime(6590):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-20 07:20:13.447: E/AndroidRuntime(6590):     at dalvik.system.NativeStart.main(Native Method)
09-20 07:20:13.447: E/AndroidRuntime(6590): Caused by: java.lang.ClassCastException: android.widget.ImageView cannot be cast to com.cannibal_photographer.Boat
09-20 07:20:13.447: E/AndroidRuntime(6590):     at com.cannibal_photographer.MainActivity.onCreate(MainActivity.java:21)
09-20 07:20:13.447: E/AndroidRuntime(6590):     at android.app.Activity.performCreate(Activity.java:5104)
09-20 07:20:13.447: E/AndroidRuntime(6590):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-20 07:20:13.447: E/AndroidRuntime(6590):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
09-20 07:20:13.447: E/AndroidRuntime(6590):     ... 11 more
09-20 07:24:42.428: D/dalvikvm(6643): GC_FOR_ALLOC freed 37K, 6% free 2556K/2712K, paused 174ms, total 176ms
09-20 07:24:42.448: I/dalvikvm-heap(6643): Grow heap (frag case) to 5.245MB for 2764816-byte allocation
09-20 07:24:42.508: D/dalvikvm(6643): GC_FOR_ALLOC freed 2K, 3% free 5254K/5416K, paused 54ms, total 54ms
09-20 07:24:42.598: D/dalvikvm(6643): GC_CONCURRENT freed <1K, 3% free 5285K/5448K, paused 7ms+21ms, total 91ms
09-20 07:24:42.687: D/AndroidRuntime(6643): Shutting down VM
09-20 07:24:42.687: W/dalvikvm(6643): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
09-20 07:24:42.707: E/AndroidRuntime(6643): FATAL EXCEPTION: main
09-20 07:24:42.707: E/AndroidRuntime(6643): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cannibal_photographer/com.cannibal_photographer.MainActivity}: java.lang.ClassCastException: android.widget.ImageView cannot be cast to com.cannibal_photographer.Boat
09-20 07:24:42.707: E/AndroidRuntime(6643):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
09-20 07:24:42.707: E/AndroidRuntime(6643):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-20 07:24:42.707: E/AndroidRuntime(6643):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-20 07:24:42.707: E/AndroidRuntime(6643):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-20 07:24:42.707: E/AndroidRuntime(6643):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-20 07:24:42.707: E/AndroidRuntime(6643):     at android.os.Looper.loop(Looper.java:137)
09-20 07:24:42.707: E/AndroidRuntime(6643):     at android.app.ActivityThread.main(ActivityThread.java:5041)
09-20 07:24:42.707: E/AndroidRuntime(6643):     at java.lang.reflect.Method.invokeNative(Native Method)
09-20 07:24:42.707: E/AndroidRuntime(6643):     at java.lang.reflect.Method.invoke(Method.java:511)
09-20 07:24:42.707: E/AndroidRuntime(6643):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-20 07:24:42.707: E/AndroidRuntime(6643):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-20 07:24:42.707: E/AndroidRuntime(6643):     at dalvik.system.NativeStart.main(Native Method)
09-20 07:24:42.707: E/AndroidRuntime(6643): Caused by: java.lang.ClassCastException: android.widget.ImageView cannot be cast to com.cannibal_photographer.Boat
09-20 07:24:42.707: E/AndroidRuntime(6643):     at com.cannibal_photographer.MainActivity.onCreate(MainActivity.java:21)
09-20 07:24:42.707: E/AndroidRuntime(6643):     at android.app.Activity.performCreate(Activity.java:5104)
09-20 07:24:42.707: E/AndroidRuntime(6643):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-20 07:24:42.707: E/AndroidRuntime(6643):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
09-20 07:24:42.707: E/AndroidRuntime(6643):     ... 11 more**

我不太确定为什么它不起作用,我正在关注 sdk 附带的示例,但是在修补了几个小时之后,我就是无法弄清楚它为什么会崩溃。任何正确方向的帮助将不胜感激。

4

2 回答 2

1

因为,在你的..中OnClickListener boatForwardListenerNULLMainActivity

并且在 中boatimageNULLBoat.java

于 2013-09-20T07:33:25.730 回答
0

我猜user370305是对的。

OnClickListener boatForwardListener; is Null

你可以用,

new OnClickListener({
    @Override
    public void OnClick(View v){
        //YOUR IMPLEMENTATIONS HERE
    }
}); 
于 2013-09-20T07:40:39.827 回答