尝试让imageview在点击时来回移动,Eclipse中没有错误显示,但应用程序崩溃

时间:2013-10-07 01:17:24

标签: android eclipse

我一直在研究这个方面的项目,并且暂时搁置了这一部分。我试图尽可能多地将与船只imageview相关的代码分成船只类。我没有收到任何错误,一切看起来都对我,但当我尝试运行时,应用程序一直崩溃。这是船类:

package com.cannibal_photographer;



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

public class Boat extends ImageView {

ImageView boatimage = (ImageView)findViewById(R.id.imageView1);
boolean state = true;


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

    boatimage.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick (View v) {
            if (state) {
                moveBoatForward(-290);
            } else {
                moveBoatReverse(290);
            }
        }
        });
    }


public void moveBoatForward(int amount){
        boatimage.offsetTopAndBottom(amount);
        state = !state;
}

    public void moveBoatReverse(int amount) {
        boatimage.offsetTopAndBottom(290);
        state = !state;
}

}

以下是主要活动类:

package com.cannibal_photographer;

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



public class MainActivity extends Activity {



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


    Boat boatobject = new Boat(this, null);

    }


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


} 

我确信有一些根本就是我做错了,但我不知道它是什么。非常感谢任何正确方向的帮助。

修改

这是layout.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" />


<ImageView
    android:id="@+id/imageView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="invisible"
    android:layout_alignLeft="@+id/imageView1"
    android:layout_alignTop="@+id/imageView2"
    android:layout_marginTop="135dp"
    android:src="@drawable/boat" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/imageButton2"
    android:layout_alignTop="@+id/imageView2"
    android:layout_marginRight="14dp"
    android:layout_marginTop="16dp"
    android:text="TextView" />

</RelativeLayout>

以下是 LogCat:

10-07 04:01:53.424: D/dalvikvm(2049): GC_FOR_ALLOC freed 37K, 6% free 2556K/2712K, paused 26ms, total 29ms
10-07 04:01:53.444: I/dalvikvm-heap(2049): Grow heap (frag case) to 5.245MB for 2764816-byte allocation
10-07 04:01:53.494: D/dalvikvm(2049): GC_FOR_ALLOC freed 2K, 3% free 5254K/5416K, paused 41ms, total 41ms
10-07 04:01:53.554: D/dalvikvm(2049): GC_CONCURRENT freed <1K, 3% free 5285K/5448K, paused 3ms+22ms, total 62ms
10-07 04:01:53.604: D/AndroidRuntime(2049): Shutting down VM
10-07 04:01:53.604: W/dalvikvm(2049): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
10-07 04:01:53.614: E/AndroidRuntime(2049): FATAL EXCEPTION: main
10-07 04:01:53.614: E/AndroidRuntime(2049): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cannibal_photographer/com.cannibal_photographer.MainActivity}: java.lang.NullPointerException
10-07 04:01:53.614: E/AndroidRuntime(2049):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
10-07 04:01:53.614: E/AndroidRuntime(2049):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
10-07 04:01:53.614: E/AndroidRuntime(2049):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-07 04:01:53.614: E/AndroidRuntime(2049):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
10-07 04:01:53.614: E/AndroidRuntime(2049):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-07 04:01:53.614: E/AndroidRuntime(2049):     at android.os.Looper.loop(Looper.java:137)
10-07 04:01:53.614: E/AndroidRuntime(2049):     at android.app.ActivityThread.main(ActivityThread.java:5041)
10-07 04:01:53.614: E/AndroidRuntime(2049):     at java.lang.reflect.Method.invokeNative(Native Method)
10-07 04:01:53.614: E/AndroidRuntime(2049):     at java.lang.reflect.Method.invoke(Method.java:511)
10-07 04:01:53.614: E/AndroidRuntime(2049):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-07 04:01:53.614: E/AndroidRuntime(2049):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-07 04:01:53.614: E/AndroidRuntime(2049):     at dalvik.system.NativeStart.main(Native Method)
10-07 04:01:53.614: E/AndroidRuntime(2049): Caused by: java.lang.NullPointerException
10-07 04:01:53.614: E/AndroidRuntime(2049):     at com.cannibal_photographer.Boat.init(Boat.java:22)
10-07 04:01:53.614: E/AndroidRuntime(2049):     at com.cannibal_photographer.Boat.<init>(Boat.java:18)
10-07 04:01:53.614: E/AndroidRuntime(2049):     at com.cannibal_photographer.MainActivity.onCreate(MainActivity.java:19)
10-07 04:01:53.614: E/AndroidRuntime(2049):     at android.app.Activity.performCreate(Activity.java:5104)
10-07 04:01:53.614: E/AndroidRuntime(2049):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-07 04:01:53.614: E/AndroidRuntime(2049):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
10-07 04:01:53.614: E/AndroidRuntime(2049):     ... 11 more
10-07 04:01:56.825: I/Process(2049): Sending signal. PID: 2049 SIG: 9
10-07 04:15:22.295: D/dalvikvm(2136): GC_FOR_ALLOC freed 40K, 6% free 2556K/2716K, paused 37ms, total 40ms
10-07 04:15:22.315: I/dalvikvm-heap(2136): Grow heap (frag case) to 5.245MB for 2764816-byte allocation
10-07 04:15:22.365: D/dalvikvm(2136): GC_FOR_ALLOC freed 2K, 4% free 5254K/5420K, paused 44ms, total 44ms
10-07 04:15:22.415: D/dalvikvm(2136): GC_CONCURRENT freed <1K, 4% free 5271K/5436K, paused 9ms+4ms, total 59ms
10-07 04:15:22.455: D/AndroidRuntime(2136): Shutting down VM
10-07 04:15:22.455: W/dalvikvm(2136): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
10-07 04:15:22.475: E/AndroidRuntime(2136): FATAL EXCEPTION: main
10-07 04:15:22.475: E/AndroidRuntime(2136): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cannibal_photographer/com.cannibal_photographer.MainActivity}: android.view.InflateException: Binary XML file line #60: Error inflating class com.cannibal_photographer.Boat
10-07 04:15:22.475: E/AndroidRuntime(2136):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
10-07 04:15:22.475: E/AndroidRuntime(2136):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
10-07 04:15:22.475: E/AndroidRuntime(2136):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-07 04:15:22.475: E/AndroidRuntime(2136):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
10-07 04:15:22.475: E/AndroidRuntime(2136):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-07 04:15:22.475: E/AndroidRuntime(2136):     at android.os.Looper.loop(Looper.java:137)
10-07 04:15:22.475: E/AndroidRuntime(2136):     at android.app.ActivityThread.main(ActivityThread.java:5041)
10-07 04:15:22.475: E/AndroidRuntime(2136):     at java.lang.reflect.Method.invokeNative(Native Method)
10-07 04:15:22.475: E/AndroidRuntime(2136):     at java.lang.reflect.Method.invoke(Method.java:511)
10-07 04:15:22.475: E/AndroidRuntime(2136):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-07 04:15:22.475: E/AndroidRuntime(2136):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-07 04:15:22.475: E/AndroidRuntime(2136):     at dalvik.system.NativeStart.main(Native Method)
10-07 04:15:22.475: E/AndroidRuntime(2136): Caused by: android.view.InflateException: Binary XML file line #60: Error inflating class com.cannibal_photographer.Boat
10-07 04:15:22.475: E/AndroidRuntime(2136):     at android.view.LayoutInflater.createView(LayoutInflater.java:596)
10-07 04:15:22.475: E/AndroidRuntime(2136):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
10-07 04:15:22.475: E/AndroidRuntime(2136):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
10-07 04:15:22.475: E/AndroidRuntime(2136):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
10-07 04:15:22.475: E/AndroidRuntime(2136):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
10-07 04:15:22.475: E/AndroidRuntime(2136):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
10-07 04:15:22.475: E/AndroidRuntime(2136):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
10-07 04:15:22.475: E/AndroidRuntime(2136):     at android.app.Activity.setContentView(Activity.java:1881)
10-07 04:15:22.475: E/AndroidRuntime(2136):     at com.cannibal_photographer.MainActivity.onCreate(MainActivity.java:16)
10-07 04:15:22.475: E/AndroidRuntime(2136):     at android.app.Activity.performCreate(Activity.java:5104)
10-07 04:15:22.475: E/AndroidRuntime(2136):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-07 04:15:22.475: E/AndroidRuntime(2136):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
10-07 04:15:22.475: E/AndroidRuntime(2136):     ... 11 more
10-07 04:15:22.475: E/AndroidRuntime(2136): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
10-07 04:15:22.475: E/AndroidRuntime(2136):     at java.lang.Class.getConstructorOrMethod(Class.java:460)
10-07 04:15:22.475: E/AndroidRuntime(2136):     at java.lang.Class.getConstructor(Class.java:431)
10-07 04:15:22.475: E/AndroidRuntime(2136):     at android.view.LayoutInflater.createView(LayoutInflater.java:561)
10-07 04:15:22.475: E/AndroidRuntime(2136):     ... 22 more
10-07 04:15:27.784: I/Process(2136): Sending signal. PID: 2136 SIG: 9
10-07 04:21:26.554: D/dalvikvm(2205): GC_FOR_ALLOC freed 44K, 7% free 2556K/2720K, paused 29ms, total 31ms
10-07 04:21:26.564: I/dalvikvm-heap(2205): Grow heap (frag case) to 5.245MB for 2764816-byte allocation
10-07 04:21:26.624: D/dalvikvm(2205): GC_FOR_ALLOC freed 2K, 4% free 5254K/5424K, paused 51ms, total 51ms
10-07 04:21:26.674: D/dalvikvm(2205): GC_CONCURRENT freed <1K, 4% free 5257K/5424K, paused 3ms+3ms, total 54ms
10-07 04:21:26.714: D/AndroidRuntime(2205): Shutting down VM
10-07 04:21:26.714: W/dalvikvm(2205): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
10-07 04:21:26.734: E/AndroidRuntime(2205): FATAL EXCEPTION: main
10-07 04:21:26.734: E/AndroidRuntime(2205): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cannibal_photographer/com.cannibal_photographer.MainActivity}: android.view.InflateException: Binary XML file line #60: Error inflating class com.cannibal_photographer.Boat
10-07 04:21:26.734: E/AndroidRuntime(2205):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
10-07 04:21:26.734: E/AndroidRuntime(2205):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
10-07 04:21:26.734: E/AndroidRuntime(2205):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-07 04:21:26.734: E/AndroidRuntime(2205):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
10-07 04:21:26.734: E/AndroidRuntime(2205):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-07 04:21:26.734: E/AndroidRuntime(2205):     at android.os.Looper.loop(Looper.java:137)
10-07 04:21:26.734: E/AndroidRuntime(2205):     at android.app.ActivityThread.main(ActivityThread.java:5041)
10-07 04:21:26.734: E/AndroidRuntime(2205):     at java.lang.reflect.Method.invokeNative(Native Method)
10-07 04:21:26.734: E/AndroidRuntime(2205):     at java.lang.reflect.Method.invoke(Method.java:511)
10-07 04:21:26.734: E/AndroidRuntime(2205):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-07 04:21:26.734: E/AndroidRuntime(2205):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-07 04:21:26.734: E/AndroidRuntime(2205):     at dalvik.system.NativeStart.main(Native Method)
10-07 04:21:26.734: E/AndroidRuntime(2205): Caused by: android.view.InflateException: Binary XML file line #60: Error inflating class com.cannibal_photographer.Boat
10-07 04:21:26.734: E/AndroidRuntime(2205):     at android.view.LayoutInflater.createView(LayoutInflater.java:596)
10-07 04:21:26.734: E/AndroidRuntime(2205):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
10-07 04:21:26.734: E/AndroidRuntime(2205):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
10-07 04:21:26.734: E/AndroidRuntime(2205):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
10-07 04:21:26.734: E/AndroidRuntime(2205):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
10-07 04:21:26.734: E/AndroidRuntime(2205):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
10-07 04:21:26.734: E/AndroidRuntime(2205):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
10-07 04:21:26.734: E/AndroidRuntime(2205):     at android.app.Activity.setContentView(Activity.java:1881)
10-07 04:21:26.734: E/AndroidRuntime(2205):     at com.cannibal_photographer.MainActivity.onCreate(MainActivity.java:16)
10-07 04:21:26.734: E/AndroidRuntime(2205):     at android.app.Activity.performCreate(Activity.java:5104)
10-07 04:21:26.734: E/AndroidRuntime(2205):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-07 04:21:26.734: E/AndroidRuntime(2205):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
10-07 04:21:26.734: E/AndroidRuntime(2205):     ... 11 more
10-07 04:21:26.734: E/AndroidRuntime(2205): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
10-07 04:21:26.734: E/AndroidRuntime(2205):     at java.lang.Class.getConstructorOrMethod(Class.java:460)
10-07 04:21:26.734: E/AndroidRuntime(2205):     at java.lang.Class.getConstructor(Class.java:431)
10-07 04:21:26.734: E/AndroidRuntime(2205):     at android.view.LayoutInflater.createView(LayoutInflater.java:561)
10-07 04:21:26.734: E/AndroidRuntime(2205):     ... 22 more

以下是Boat类的更新版本:

package com.cannibal_photographer;



import android.content.Context;
import android.view.View;
import android.widget.ImageView;

public class Boat extends ImageView {


boolean state = true;


public Boat(Context context) {
    super(context);
    init();
}
private void init()
{
    this.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick (View v) {
            if (state) {
                moveBoatForward(-290);
            } else {
                moveBoatReverse(290);
            }
        }
    });
}



public void moveBoatForward(int amount){
    this.offsetTopAndBottom(amount);
    state = !state;
}

public void moveBoatReverse(int amount) {
    this.offsetTopAndBottom(amount);
    state = !state;
}

}

1 个答案:

答案 0 :(得分:1)

这里有一些想法:

在Boat视图中,覆盖所有3个构造函数:

代码示例:

public Boat(Context context)
    {
        super(context);
        init();
    }

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

    public Boat(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        init();
    }

private void init()
{
boatimage.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick (View v) {
            if (state) {
                moveBoatForward(-290);
            } else {
                moveBoatReverse(290);
            }
        }
        });
    }
}

在扩展视图时,我发现这是一个很好的做法。

  1. 最好将您的Boat视图包含在layout.xml文件中,这样您的通胀过程就可以处理您的Boat视图的即时消息

  2. 如果您必须通过代码立即启动您的船,请使用1st Constructor。我相信你将null作为AttributeSet发送是导致崩溃的原因。

  3. 与您的问题无关,但您有两种方法:moveBoatForward&amp; moveBoatReverse实际上做同样的事情。这是重复的代码,在编码时被认为是非常糟糕的做法。试着想出一种方法将这两种方法合并为一种,然后调用相同的方法,一次使用正数,一次使用负数。

  4. 编辑:

    这就是你的崩溃发生的原因:

    ImageView boatimage =(ImageView)findViewById(R.id.imageView1);

    您正在尝试在视图获得其上下文之前查找视图。 要了解如何实现您正在寻找的行为,最好发布layout.xml文件。

    编辑:

    考虑到您的布局,以下是您应该进行的更改:

    将ImageView变成船:

    <com.cannibal_photographer.Boat
        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" />
    

    在您的活动中,按照以下方式即时启动船只:

    Boat boatobject = (Boat)findViewById(R.id.imageView1);
    

    希望这会有所帮助:)