我一直在做这个副项目,并且已经在这部分上挂了一段时间。我正在尝试将与船图像视图相关的尽可能多的代码分离到船类中。我没有收到任何错误,一切看起来都对我来说是正确的,但是当我尝试运行它时,该应用程序不断崩溃。这是船类:
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;
}
}