Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我们如何修复这个错误?java.lang.IllegalStateException_Java_Android - Fatal编程技术网

我们如何修复这个错误?java.lang.IllegalStateException

我们如何修复这个错误?java.lang.IllegalStateException,java,android,Java,Android,我们应该如何着手修复此代码。在过去的24小时里,我们一直在尝试在Android中保存音频 我们疲惫的眼睛将非常感谢您的帮助 我们正在尝试创建一个音频分析应用程序,用于分析麦克风输入 package com.example.opus2; import android.content.ContentValues; import android.media.MediaRecorder; import android.os.Bundle; import android.provider.MediaSt

我们应该如何着手修复此代码。在过去的24小时里,我们一直在尝试在Android中保存音频

我们疲惫的眼睛将非常感谢您的帮助

我们正在尝试创建一个音频分析应用程序,用于分析麦克风输入

package com.example.opus2;

import android.content.ContentValues;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.IOException;



public class audioTest extends MainActivity{

    public int saveMarker=0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.save_test);
    }


    public void onClick(View view){
        switch (view.getId()){

            case R.id.saveBttn: {

                Button saveButton= (Button) findViewById(R.id.saveBttn);
                if(saveMarker==0){
                    TextView done = (TextView) findViewById(R.id.done_text);
                    done.setText("Done2");
                    saveButton.setText("Saved?");
                    saveMarker=1;
                    String save_file = "viola";
                    try {
                        recordAudio(save_file);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    break;
                }
                else{
                    saveButton.setText("Save");
                    saveMarker=0;

                    break;
                }

            }
        }
    }

    public void recordAudio(String fileName) throws IOException {
        TextView done = (TextView) findViewById(R.id.done_text);
        done.setText("Done1");

        final MediaRecorder recorder = new MediaRecorder();
        ContentValues values = new ContentValues(3);
        values.put(MediaStore.MediaColumns.TITLE, fileName);
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
        recorder.setOutputFile("/sdcard/sound/" + fileName+".mp4");

        try {
            recorder.prepare();
        } catch (Exception e){
            e.printStackTrace();
        }

        /* final ProgressDialog mProgressDialog = new ProgressDialog(audioTest.this);
        mProgressDialog.setTitle(R.string.lbl_recording);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        mProgressDialog.setButton("Stop recording", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                mProgressDialog.dismiss();
                recorder.stop();
                recorder.release();
            }
        });

        mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            public void onCancel(DialogInterface p1) {
                recorder.stop();
                recorder.release();
            }
        });
        mProgressDialog.show();
        */

        recorder.start();
        done = (TextView) findViewById(R.id.done_text);
        done.setText("Done");
        }
}
----错误------


如果您的媒体录制器已准备就绪,请尝试检查其状态。如果没有:在调用开始记录之前,等待几毫秒,直到它准备好

另外,请查看android开发者参考资料,了解有关不同记录器状态的更多信息:

您可以发布异常吗?您可以发布logcat堆栈跟踪第一条编程规则:读取错误消息。您可以删除开关中案例的
{}
案例R.id.saveBttn:{
我觉得那里有些奇怪。谢谢你提醒我注意到你让我注意到mediarecorder在模拟器上无法工作。但遗憾的是,这并没有解决问题
11-02 22:29:18.416    3256-3256/com.example.opus2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:3599)
            at android.view.View.performClick(View.java:4204)
            at android.view.View$PerformClick.run(View.java:17355)
            at android.os.Handler.handleCallback(Handler.java:725)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5041)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at android.view.View$1.onClick(View.java:3594)
            at android.view.View.performClick(View.java:4204)
            at android.view.View$PerformClick.run(View.java:17355)
            at android.os.Handler.handleCallback(Handler.java:725)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5041)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalStateException
            at android.media.MediaRecorder.start(Native Method)
            at com.example.opus2.audioTest.recordAudio(audioTest.java:96)
            at com.example.opus2.audioTest.onClick(audioTest.java:40)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at android.view.View$1.onClick(View.java:3594)
            at android.view.View.performClick(View.java:4204)
            at android.view.View$PerformClick.run(View.java:17355)
            at android.os.Handler.handleCallback(Handler.java:725)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5041)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)