我正在尝试简单地将图片dotpowder.png
放置在触摸屏幕的位置。很简单吧?显然不是,因为图像是一个类,具有属性所以我可以将其视为动态对象而不是ImageView。以下是DotCanvas.java
的代码:
package com.dhg.dots;
import android.content.Context;
import android.view.View;
import android.view.MotionEvent;
import android.graphics.Canvas;
import android.graphics.Point;
public class DotCanvas extends View {
private Context mContext;
public DotCanvas(Context context) {
super(context);
setFocusable(true);
mContext = context;
}
private DotBase dot;
@Override protected void onDraw(Canvas canvas) {
for (int i = 0; i < DotHandler.dotList.size(); i++) {
dot = DotHandler.dotList.get(i);
dot.setY(dot.getY() + 1);
canvas.drawBitmap(dot.getBitmap(), dot.getX(), dot.getY(), null);
}
}
public boolean onTouchEvent(MotionEvent event) {
Point dotPoint = new Point();
dotPoint.x = (int)event.getX();
dotPoint.y = (int)event.getY();
new DotPowder(mContext, dotPoint.x, dotPoint.y);
invalidate();
return true;
}
}
这是已更新 DotBase.java
:
package com.dhg.dots;
import android.graphics.Bitmap;
import android.content.Context;
import android.graphics.BitmapFactory;
public class DotBase {
private Context mContext;
private Bitmap dotBitmap;
public DotBase(Context context, float x, float y) {
DotHandler.addDotToArray(this);
mContext = context;
Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.dotpowder);
dotBitmap = bitmap;
xCoord = x;
yCoord = y;
}
private float xCoord;
private float yCoord;
public void setX(float x) {
xCoord = x;
}
public void setY(float y) {
yCoord = y;
}
public float getX() {
return xCoord;
}
public float getY() {
return yCoord;
}
public void setGraphic(int id) {
dotBitmap = BitmapFactory.decodeResource(mContext.getResources(), id);
}
public Bitmap getBitmap() {
return dotBitmap;
}
}
最后,这里是DotPowder.java
:
package com.dhg.dots;
import android.content.Context;
public class DotPowder extends DotBase {
public DotPowder(Context context, float x, float y) {
super(context, x, y);
setGraphic(R.drawable.dotpowder);
}
}
ClassNotFoundException
在实例化新DotBase
时发生,但我不明白为什么。
P.S。这是DotHandler.java,主要的Activity:
package com.dhg.dots;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
public class DotHandler extends Activity {
/** Called when the activity is first created. */
public static List<DotBase> dotList = new ArrayList<DotBase>() {
private static final long serialVersionUID = -946727550332680940L;
};
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new DotCanvas(this));
}
public static void addDotToArray(DotBase dot) {
if (dotList.size() == 0) {
dotList.add(0, dot);
}
else {
dotList.add(dotList.size() + 1, dot);
}
}
}
这是堆栈跟踪:
java.lang.NullPointerException
at com.dhg.dots.DotBase.<init>(DotBase.java:17)
at com.dhg.dots.DotPowder.<init>(DotPowder.java:7)
at com.dhg.dots.DotCanvas.onTouchEvent(DotCanvas.java:32)
at android.view.View.dispatchTouchEvent(View.java:3778)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:885)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:885)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:885)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1716)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1124)
at android.app.Activity.dispatchTouchEvent(Activity.java:2125)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1700)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1802)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:5061)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
java.lang.NullPointerException
at com.dhg.dots.DotBase.<init>(DotBase.java:17)
at com.dhg.dots.DotPowder.<init>(DotPowder.java:7)
at com.dhg.dots.DotCanvas.onTouchEvent(DotCanvas.java:32)
at android.view.View.dispatchTouchEvent(View.java:3778)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:958)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:958)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:958)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1716)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1124)
at android.app.Activity.dispatchTouchEvent(Activity.java:2125)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1700)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1802)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:5061)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
java.lang.NullPointerException
at com.dhg.dots.DotBase.<init>(DotBase.java:17)
at com.dhg.dots.DotPowder.<init>(DotPowder.java:7)
at com.dhg.dots.DotCanvas.onTouchEvent(DotCanvas.java:32)
at android.view.View.dispatchTouchEvent(View.java:3778)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:958)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:958)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:958)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1716)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1124)
at android.app.Activity.dispatchTouchEvent(Activity.java:2125)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1700)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1802)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:5061)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
另外,正如您从堆栈跟踪中可以看出的那样,它也开始抛出NullPointerExceptions。
答案 0 :(得分:0)
DotBase.java第17行的方法调用“mContext.getResources()”可能引发NullPointerException。 DotBase #mContext的起源是DotCanvas类的构造函数中的参数“mContext”。
请检查通过DotCanvas构造函数传递的对象。
答案 1 :(得分:0)
问题确实在这一行:
private Bitmap dotBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.dotpowder);
在调用构造函数之前调用此代码,当然mContext为null。只需将初始化代码放在构造函数中,就可以了。希望这会有所帮助。