我正在制作一个简单的安卓应用程序。当我按下主活动中的按钮时,我调用了 MoveBalloon 活动。此活动使用麦克风根据麦克风检测到的声音在屏幕上移动图像。我按下后退按钮并返回主要活动。一切都很顺利,直到我第二次按下按钮再次开始 MoveBalloon 活动。此时应用程序崩溃。我认为问题出在 MoveBallon.class 中,但我不知道出了什么问题。下面我发布 logcat 和 MoveBalloon.class。请帮我 !先感谢您!日志猫:
09-24 14:52:20.346: E/AndroidRuntime(23075): FATAL EXCEPTION: main
09-24 14:52:20.346: E/AndroidRuntime(23075): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.prova1/com.example.prova1.MoveBalloon}: java.lang.IllegalStateException
09-24 14:52:20.346: E/AndroidRuntime(23075): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
09-24 14:52:20.346: E/AndroidRuntime(23075): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-24 14:52:20.346: E/AndroidRuntime(23075): at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-24 14:52:20.346: E/AndroidRuntime(23075): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-24 14:52:20.346: E/AndroidRuntime(23075): at android.os.Handler.dispatchMessage(Handler.java:99)
09-24 14:52:20.346: E/AndroidRuntime(23075): at android.os.Looper.loop(Looper.java:137)
09-24 14:52:20.346: E/AndroidRuntime(23075): at android.app.ActivityThread.main(ActivityThread.java:5103)
09-24 14:52:20.346: E/AndroidRuntime(23075): at java.lang.reflect.Method.invokeNative(Native Method)
09-24 14:52:20.346: E/AndroidRuntime(23075): at java.lang.reflect.Method.invoke(Method.java:525)
09-24 14:52:20.346: E/AndroidRuntime(23075): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-24 14:52:20.346: E/AndroidRuntime(23075): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-24 14:52:20.346: E/AndroidRuntime(23075): at dalvik.system.NativeStart.main(Native Method)
09-24 14:52:20.346: E/AndroidRuntime(23075): Caused by: java.lang.IllegalStateException
09-24 14:52:20.346: E/AndroidRuntime(23075): at android.media.MediaRecorder.start(Native Method)
09-24 14:52:20.346: E/AndroidRuntime(23075): at com.example.prova1.Microphone.<init>(Microphone.java:29)
09-24 14:52:20.346: E/AndroidRuntime(23075): at com.example.prova1.MoveBalloon.onCreate(MoveBalloon.java:106)
09-24 14:52:20.346: E/AndroidRuntime(23075): at android.app.Activity.performCreate(Activity.java:5133)
09-24 14:52:20.346: E/AndroidRuntime(23075): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-24 14:52:20.346: E/AndroidRuntime(23075): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
09-24 14:52:20.346: E/AndroidRuntime(23075): ... 11 more
移动气球类:
package com.example.prova1;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.hardware.SensorManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class MoveBalloon extends Activity {
Bitmap balloon;
Bitmap background;
Bitmap _background;
int i,j,k;
private MediaPlayer mp;
Bitmap foto;
DrawBalloon myView;
float x,y,sensorX, sensorY;
SensorManager sm;
Microphone mic;
int width, height;
int count=0;
public class DrawBalloon extends SurfaceView implements Runnable {
SurfaceHolder ourHolder ;
Thread ourThread = null;
boolean isRunning=true;
public DrawBalloon(Context context) {
super(context);
mp = MediaPlayer.create(context, R.raw.yeehaaa);
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();
if(canvas!=null){
updateMic();
canvas.drawBitmap(_background,0,0,null);
canvas.drawBitmap(balloon, sensorX, sensorY,null);
checkCollisions(canvas);
ourHolder.unlockCanvasAndPost(canvas);
}
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
/*sm= (SensorManager) getSystemService(Context.SENSOR_SERVICE);
if(sm.getSensorList(Sensor.TYPE_ACCELEROMETER).size()!=0){
Sensor s = sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
sm.registerListener(this,s ,SensorManager.SENSOR_DELAY_NORMAL);
}*/
Intent intent= getIntent();
i=intent.getIntExtra("b1", 0);
j=intent.getIntExtra("b2", 0);
k=intent.getIntExtra("b3", 0);
mic = new Microphone();
balloon = BitmapFactory.decodeResource(getResources(), i);
background = BitmapFactory.decodeResource(getResources(), j);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
height = displaymetrics.heightPixels;
width = displaymetrics.widthPixels;
sensorX=(width-balloon.getWidth())/2;
sensorY=height-balloon.getHeight()-20;
//x=y=sensorX=sensorY=0;
_background = getResizedBitmap(background,height,width);
myView= new DrawBalloon (this);
myView.resume();
setContentView(myView);
}
public void updateMic(){
double level = mic.getAmplitudeEMA();
sensorY-=level;
}
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// Create a matrix for the manipulation
Matrix matrix = new Matrix();
// Resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// Recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
return resizedBitmap;
}
private void checkCollisions(Canvas canvas){
foto=BitmapFactory.decodeResource(getResources(), k);
if(sensorY<=50&&sensorY>=40){
canvas.drawBitmap(foto, (width-foto.getWidth())/2, 30, null);
mp.start();
}else{
if (sensorY<40){
canvas.drawBitmap(foto,(width-foto.getWidth())/2, 30, null);
}
}
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
mp.stop();
finish();
super.onBackPressed();
}
}
这是 Microphone.class :
package com.example.prova1;
import java.io.IOException;
import android.media.MediaRecorder;
public class Microphone {
MediaRecorder recorder;
int level;
static final private double EMA_FILTER = 0.6;
private double mEMA = 0;
public Microphone(){
// Audio recorder
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile("/dev/null");
mEMA = 0;
try {
recorder.prepare();
recorder.start();
}
catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void stopMic(){
recorder.release();
}
public double getLevel(){
level = recorder.getMaxAmplitude();
level = level/2700;
return level;
}
public double getAmplitudeEMA() {
double amp = getLevel();
mEMA = EMA_FILTER * amp + (1.0 - EMA_FILTER) * mEMA;
return mEMA;
}
}
新的 LOGCAT:
09-24 16:31:44.140: E/AndroidRuntime(31331): FATAL EXCEPTION: Thread-1680
09-24 16:31:44.140: E/AndroidRuntime(31331): java.lang.NullPointerException
09-24 16:31:44.140: E/AndroidRuntime(31331): at com.example.prova1.Microphone.getLevel(Microphone.java:43)
09-24 16:31:44.140: E/AndroidRuntime(31331): at com.example.prova1.Microphone.getAmplitudeEMA(Microphone.java:48)
09-24 16:31:44.140: E/AndroidRuntime(31331): at com.example.prova1.MoveBalloon.updateMic(MoveBalloon.java:157)
09-24 16:31:44.140: E/AndroidRuntime(31331): at com.example.prova1.MoveBalloon$DrawBalloon.run(MoveBalloon.java:84)