开发者

Why does my code throw a ClassNotFoundException?

开发者 https://www.devze.com 2023-03-07 16:26 出处:网络
I\'m trying to simply place an image, dotpowder.png, where you touch the screen. Simple enough, right? Apparently not, as the image is to be a class, with attributes so I can treat it as a dynamic obj

I'm trying to simply place an image, dotpowder.png, where you touch the screen. Simple enough, right? Apparently not, as the image is to be a class, with attributes so I can treat it as a dynamic object instead of an ImageView. Here is the code for the 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;
    }
}

And this is the updated 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;
    }
}

And finally, here is 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);
    }
}

The ClassNotFoundException occurs when the new DotBase is instantiated, but I have no idea why.

P.S. Here is DotHandler.java, the main 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. */
    p开发者_运维技巧ublic 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);
        }
    }
}

And here is the stack trace:

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)

Also, as you can tell from the stack trace, it has started throwing NullPointerExceptions as well.


The problem is indeed in this line:

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

This code is called before you call the constructor, and surely mContext is null here. Just place the initialization code inside your constructor and you'll be OK. Hope this helps.


The method call "mContext.getResources()" at the line 17 in DotBase.java may raise NullPointerException. The origin of DotBase#mContext is the parameter "mContext" in the constructor of DotCanvas class.

Please check what object is passed through the DotCanvas constructor.

0

精彩评论

暂无评论...
验证码 换一张
取 消