发生空指针异常错误

时间:2013-09-21 22:13:54

标签: android nullpointerexception

我正在尝试制作一款使用麦克风移动图片的应用。我知道这个错误。下面是类和LogCat。谁能帮我?谢谢! .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. ..........

这是MoveBalloon类

package com.example.prova1;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MoveBalloon extends Activity {
 Bitmap balloon;
 DrawBalloon myView;
 float x,y,sensorX, sensorY;
 Microphone mic;



public class DrawBalloon extends SurfaceView implements Runnable {

 SurfaceHolder ourHolder ;
 Thread ourThread = null;
 boolean isRunning=true;

 public DrawBalloon(Context context) {
  super(context);
  ourHolder= getHolder();
 }

 public void pause() {
  isRunning=false;
  while(true){
   try{
    ourThread.join();
   } catch (InterruptedException e){
    e.printStackTrace();
   }
   break;
  }
  ourThread=null;
 }

 public void resume(){
  isRunning=true;
  ourThread = new Thread(this);
  ourThread.start();


 }
 @Override
 public void run (){
  while(isRunning){
   if(ourHolder.getSurface().isValid())
    continue;
            Canvas canvas = ourHolder.lockCanvas();

   canvas.drawColor(Color.WHITE);
   updateMic();

   canvas.drawBitmap(balloon, sensorX, sensorY,null);
   ourHolder.unlockCanvasAndPost(canvas);
  }
 }
}



@Override
protected void onCreate(Bundle savedInstanceState) {
 // TODO Auto-generated method stub
 super.onCreate(savedInstanceState);
 mic = new Microphone();
balloon = BitmapFactory.decodeResource(getResources(), R.drawable.images);
sensorX=150;
sensorY=350;
myView= new DrawBalloon (this);
myView.resume();
}


public void updateMic(){
 int level = mic.getLevel();
 sensorX=0;
 sensorY=level;
}



}

logcat在这里:

09-21 23:57:10.909: D/ActivityThread(30461): setTargetHeapUtilization:0.25
09-21 23:57:10.909: D/ActivityThread(30461): setTargetHeapIdealFree:8388608
09-21 23:57:10.919: D/ActivityThread(30461): setTargetHeapConcurrentStart:2097152
09-21 23:57:11.109: W/SurfaceView(30461): CHECK surface infomation creating=true formatChanged=true sizeChanged=true visible=true visibleChanged=true surfaceChanged=false realSizeChanged=true redrawNeeded=true left=false top=false
09-21 23:57:11.519: I/System.out(30461): 0
09-21 23:57:11.519: W/dalvikvm(30461): threadid=12: thread exiting with uncaught exception (group=0x41bb5438)
09-21 23:57:11.519: E/AndroidRuntime(30461): FATAL EXCEPTION: Thread-2034
09-21 23:57:11.519: E/AndroidRuntime(30461): java.lang.NullPointerException
09-21 23:57:11.519: E/AndroidRuntime(30461):  at com.example.prova1.MoveBalloon$DrawBalloon.run(MoveBalloon.java:64)
09-21 23:57:11.519: E/AndroidRuntime(30461):  at java.lang.Thread.run(Thread.java:856)
09-21 23:57:11.589: W/SurfaceView(30461): CHECK surface infomation creating=false formatChanged=false sizeChanged=false visible=false visibleChanged=true surfaceChanged=false realSizeChanged=false redrawNeeded=false left=false top=false
09-21 23:57:11.619: D/libEGL(30461): loaded /system/lib/egl/libEGL_adreno200.so
09-21 23:57:11.619: D/libEGL(30461): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
09-21 23:57:11.629: D/libEGL(30461): loaded /system/lib/egl/libGLESv2_adreno200.so
09-21 23:57:11.639: I/Adreno200-EGL(30461): <qeglDrvAPI_eglInitialize:299>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB_REL_2.0.3.1_RB1.04.01.01.45.000_msm8625_JB_REL_2.0.3.1_Merge_release_AU (Merge)
09-21 23:57:11.639: I/Adreno200-EGL(30461): Build Date: 01/11/13 Fri
09-21 23:57:11.639: I/Adreno200-EGL(30461): Local Branch: 
09-21 23:57:11.639: I/Adreno200-EGL(30461): Remote Branch: m/jb_rel_2.0.3.1
09-21 23:57:11.639: I/Adreno200-EGL(30461): Local Patches: NONE
09-21 23:57:11.639: I/Adreno200-EGL(30461): Reconstruct Branch: NOTHING
09-21 23:57:11.999: D/OpenGLRenderer(30461): Enabling debug mode 0
09-21 23:57:19.349: I/Choreographer(30461): Skipped 439 frames!  The application may be doing too much work on its main thread.
09-21 23:57:20.539: I/Process(30461): Sending signal. PID: 30461 SIG: 9

1 个答案:

答案 0 :(得分:0)

Frm的android文档

  

开始编辑曲面中的像素。返回的Canvas可用于绘制到曲面的位图。如果尚未创建表面或无法编辑表面,则返回 null 。您通常需要实现 Callback.surfaceCreated 以找出Surface可供使用的时间。

因此,只有在创建曲面后才需要执行此过程。您需要为此实现SurfaceHolder.CallbackFurther Read..