在我的相机应用程序中,当我按下主页按钮时,它会显示消息“不幸的是,test(myappname) 已停止”。我一直在寻找一段时间,但永远无法摆脱这个错误。任何帮助表示赞赏,我已经坚持了很长一段时间:(。如果我遗漏了什么,请问。谢谢
public class MainActivity extends Activity implements OnInitListener, OnUtteranceCompletedListener{
private Camera myCamera;
private MyCameraSurfaceView myCameraSurfaceView;
Button myButton;
Button speakButton;
boolean recording;
Button Speak;
TextView TextBox;
private TextToSpeech mTts;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
Button.OnClickListener speakButtonOnClickListener = new Button.OnClickListener() {
@Override
public void onClick(View v) {
//when Button Speak is clicked
//grab text from the EditText Speech
String TALK = TextBox.getText().toString(); //Talk now has text from EditText
mTts.speak(TALK, TextToSpeech.QUEUE_ADD, null);
}};
Button.OnClickListener myButtonOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(recording){
// stop recording and release camera
myButton.setText("START");
recording = false;
}else{
recording = true;
myButton.setText("STOP");
}
}};
private Camera getCameraInstance(){
// TODO Auto-generated method stub
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
c.setDisplayOrientation(90);
}
catch (Exception e){
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
@Override
protected void onPause() {
super.onPause();
releaseCamera();
// release the camera immediately on pause event
}
@Override
protected void onResume() {
super.onResume();
if (myCamera == null) {
getWindow().setFormat(PixelFormat.UNKNOWN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
recording = false;
setContentView(R.layout.activity_main);
Speak = (Button)this.findViewById(R.id.button1);
TextBox = (TextView)this.findViewById(R.id.textView1);
//initialise TTS
mTts= new TextToSpeech(this, this);
//Get Camera for preview
myCamera = getCameraInstance();
if(myCamera == null){
Toast.makeText(MainActivity.this,
"Fail to get Camera",
Toast.LENGTH_LONG).show();
}
myCameraSurfaceView = new MyCameraSurfaceView(this, myCamera);
FrameLayout myCameraPreview = (FrameLayout)findViewById(R.id.videoview);
myCameraPreview.addView(myCameraSurfaceView);
speakButton = (Button) findViewById(R.id.button1);
speakButton.setOnClickListener(speakButtonOnClickListener);
}
}
@Override
protected void onStop() {
super.onStop();
// release the camera immediately on pause event
}
@Override
protected void onDestroy() {
super.onDestroy();
// release the camera immediately on pause event
releaseCamera();
}
private void releaseCamera(){
if (myCamera != null){
myCamera.stopPreview();
myCamera.release(); // release the camera for other applications
myCamera = null;
myCamera.setPreviewCallback(null);
}
}
@SuppressWarnings("deprecation")
@Override
public void onInit(int status) {
mTts.setOnUtteranceCompletedListener(this);
}
@Override
public void onUtteranceCompleted(String utteranceId) {
mTts.shutdown();
}
}
LOGCAT
10-04 16:00:23.330: E/AndroidRuntime(2871): FATAL EXCEPTION: main
10-04 16:00:23.330: E/AndroidRuntime(2871): java.lang.RuntimeException: Unable to pause activity {com.example.test/com.example.test.MainActivity}: java.lang.NullPointerException
10-04 16:00:23.330: E/AndroidRuntime(2871): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3064)
10-04 16:00:23.330: E/AndroidRuntime(2871): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3019)
10-04 16:00:23.330: E/AndroidRuntime(2871): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2997)
10-04 16:00:23.330: E/AndroidRuntime(2871): at android.app.ActivityThread.access$800(ActivityThread.java:141)
10-04 16:00:23.330: E/AndroidRuntime(2871): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1267)
10-04 16:00:23.330: E/AndroidRuntime(2871): at android.os.Handler.dispatchMessage(Handler.java:99)
10-04 16:00:23.330: E/AndroidRuntime(2871): at android.os.Looper.loop(Looper.java:137)
10-04 16:00:23.330: E/AndroidRuntime(2871): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-04 16:00:23.330: E/AndroidRuntime(2871): at java.lang.reflect.Method.invokeNative(Native Method)
10-04 16:00:23.330: E/AndroidRuntime(2871): at java.lang.reflect.Method.invoke(Method.java:525)
10-04 16:00:23.330: E/AndroidRuntime(2871): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)