0

我正在尝试制作一个使用麦克风来移动图像的应用程序。但是我得到了这个错误。下面是类和 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;
}



}

日志在这里:

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
4

1 回答 1

0

Frm 安卓文档

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

因此,您必须仅在创建曲面后执行该过程。您需要为此实施SurfaceHolder.Callback进一步阅读..

于 2013-09-21T23:02:14.017 回答