我对Android应用程序开发有点新,所以请原谅我的菜鸟代码。我正在尝试根据运动编写游戏。当您旋转手机时,它会在屏幕上从右向左滑动一个对象(一个水桶)以捕捉掉落的物体。我在这个类中有一个Main Activity和一个私有的SurfaceView。我也有一个自定义线程运行。
这是错误: 致命异议:主要 处理:course.examples.databaseanimation,PID:17583 java.lang.NullPointerException:尝试调用虚方法' int android.graphics.Bitmap.getWidth()'在null对象引用上 在android.graphics.Bitmap.createBitmap(Bitmap.java:659) at course.examples.databaseanimation.BucketThread。(BucketThread.java:25) at course.examples.databaseanimation.MainActivity $ BucketView.surfaceCreated(MainActivity.java:102) 在android.view.SurfaceView.updateWindow(SurfaceView.java:582) 在android.view.SurfaceView $ 3.onPreDraw(SurfaceView.java:177) 在android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944) 在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2067) 在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1119) 在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6060) 在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:858) 在android.view.Choreographer.doCallbacks(Choreographer.java:670) 在android.view.Choreographer.doFrame(Choreographer.java:606) 在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:844) 在android.os.Handler.handleCallback(Handler.java:746) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:5443) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:728) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
这是我的主题:
public class BucketThread extends Thread {
private SurfaceHolder holder;
private boolean running = true;
private float shiftY;
private float vy;
private float x;
private Bitmap mBucket = Bitmap.createBitmap(BitmapFactory.decodeResource
(Resources.getSystem(), R.mipmap.ic_launcher));
public BucketThread(SurfaceHolder holder){
this.holder = holder;
x = 0;
}
public void run(){
while(running){
Canvas canvas = null;
try{
canvas = holder.lockCanvas();
synchronized (holder){
//update
vy += shiftY * 0.1;
x += vy;
//draw
Paint mPaint = new Paint();
canvas.drawBitmap(mBucket, x, 0, mPaint);
}
}
finally {
if(canvas != null){
holder.unlockCanvasAndPost(canvas);
}
}
}
}
public void setRunning(boolean r){
running = r;
}
public void setShift(float shiftY){
this.shiftY = shiftY;
}
}
这是我的主要活动:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout mainFrame = (RelativeLayout) findViewById
(R.id.main_frame);
BucketView bv = new BucketView(getApplicationContext());
mainFrame.addView(bv);
}
private class BucketView extends SurfaceView implements SurfaceHolder.Callback,
SensorEventListener {
//private final Bitmap mBucket;
//private DisplayMetrics mDisplay;
private SurfaceHolder mSurfaceHolder;
private BucketThread mBuckThread;
//private int mDisplayWidth;
//private float mY;
public BucketView(Context context) {
super(context);
mSurfaceHolder = getHolder();
mSurfaceHolder.addCallback(this);
SensorManager manager = (SensorManager) context.getSystemService
(Context.SENSOR_SERVICE);
if (manager.getSensorList(Sensor.TYPE_ACCELEROMETER).size() != 0) {
Sensor accelerometer = manager.getSensorList(Sensor
.TYPE_ACCELEROMETER).get(0);
manager.registerListener(this, accelerometer, SensorManager
.SENSOR_DELAY_GAME);
}
}
@Override
public void onSensorChanged(SensorEvent event) {
if (mBuckThread != null) {
mBuckThread.setShift(event.values[1]);
Log.i("Main Activity", getClass().getSimpleName() + "onSensorChanged");
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
mBuckThread = new BucketThread(holder);
mBuckThread.setRunning(true);
mBuckThread.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
mBuckThread.setRunning(false);
while (retry) {
try {
mBuckThread.join();
retry = false;
} catch (InterruptedException e) {
}
}
}
}
}
答案 0 :(得分:0)
更改
private Bitmap mBucket = Bitmap.createBitmap(BitmapFactory.decodeResource
(Resources.getSystem(), R.mipmap.ic_launcher));
到
private Bitmap mBucket = Bitmap.createBitmap(BitmapFactory.decodeResource
(getResources(), R.mipmap.ic_launcher));
Resources.getSystem()
不会工作,因为它用于访问系统资源而不是应用程序的资源。
所以请改用getResources()
。