0

カメラ アプリケーションでホーム ボタンを押すと、「残念ながら、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();
}
}

ログキャット

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)
4

2 に答える 2

0

エラーは releaseCamera メソッドにあります:

private void releaseCamera(){
    if (myCamera != null){
        myCamera.stopPreview();
        myCamera.release();        // release the camera for other applications
        myCamera = null;
        myCamera.setPreviewCallback(null);
    }
}

null で setPreviewCallback を呼び出しています。

解決策: 上に移動myCamera.setPreviewCallback(null);します。

于 2015-09-14T08:59:02.600 に答える