gpt4 book ai didi

java - 为什么我的代码会抛出 ClassNotFoundException?

转载 作者:行者123 更新时间:2023-12-01 05:45:22 25 4
gpt4 key购买 nike

我试图简单地将图像 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);
}
}

实例化新的DotBase时会发生ClassNotFoundException,但我不知道为什么。

附注这是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。

最佳答案

问题确实出在这一行:

private Bitmap dotBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.dotpowder);

这段代码是在调用构造函数之前调用的,这里 mContext 肯定为 null。只需将初始化代码放在构造函数中就可以了。希望这会有所帮助。

关于java - 为什么我的代码会抛出 ClassNotFoundException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6079722/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com