无法执行onClick

用户名

谢谢大家的帮助,事实证明,我在录制和播放按钮的文件路径中包含了一个不存在的目录。

好了,这是交易。

我将Google的示例代码用于他们的AudioCapture类,这里是我正在为一个类所做的项目。我正在创建一个音板应用程序,您应该可以在其中录制4种不同的声音并进行播放。

我的问题涉及我的pClick和rClick函数,我有理由相信这与我的Button tempButt行有关。

感谢您抽出宝贵的时间阅读本文档,并可能在尝试解决我的问题时为您提供帮助。

我收到的错误是这样的:

更新

    04-14 03:16:29.639 22570-22570/com.example.oreoman.failuredude D/OpenGLRenderer: Enabling debug mode 0
04-14 03:16:31.358 22570-22570/com.example.oreoman.failuredude E/AudioRecordTest: prepare() failed
04-14 03:16:31.358 22570-22570/com.example.oreoman.failuredude E/MediaRecorder: start called in an invalid state: 4
04-14 03:16:31.359 22570-22570/com.example.oreoman.failuredude D/AndroidRuntime: Shutting down VM
04-14 03:16:31.359 22570-22570/com.example.oreoman.failuredude W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41702d40)
04-14 03:16:31.367 22570-22570/com.example.oreoman.failuredude E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                 Process: com.example.oreoman.failuredude, PID: 22570
                                                                                 java.lang.IllegalStateException: Could not execute method of the activity
                                                                                     at android.view.View$1.onClick(View.java:3851)
                                                                                     at android.view.View.performClick(View.java:4466)
                                                                                     at android.view.View$PerformClick.run(View.java:18537)
                                                                                     at android.os.Handler.handleCallback(Handler.java:733)
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                     at android.os.Looper.loop(Looper.java:136)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5102)
                                                                                     at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                     at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                                                                                     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:515)
                                                                                     at android.view.View$1.onClick(View.java:3846)
                                                                                     at android.view.View.performClick(View.java:4466) 
                                                                                     at android.view.View$PerformClick.run(View.java:18537) 
                                                                                     at android.os.Handler.handleCallback(Handler.java:733) 
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                     at android.os.Looper.loop(Looper.java:136) 
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5102) 
                                                                                     at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                     at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
                                                                                     at dalvik.system.NativeStart.main(Native Method) 
                                                                                  Caused by: java.lang.IllegalStateException
                                                                                     at android.media.MediaRecorder.start(Native Method)
                                                                                     at com.example.oreoman.failuredude.AudioRecordTest.startRecording(AudioRecordTest.java:120)
                                                                                     at com.example.oreoman.failuredude.AudioRecordTest.onRecord(AudioRecordTest.java:35)
                                                                                     at com.example.oreoman.failuredude.AudioRecordTest.rClick(AudioRecordTest.java:131)
                                                                                     at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                     at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                                     at android.view.View$1.onClick(View.java:3846) 
                                                                                     at android.view.View.performClick(View.java:4466) 
                                                                                     at android.view.View$PerformClick.run(View.java:18537) 
                                                                                     at android.os.Handler.handleCallback(Handler.java:733) 
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                     at android.os.Looper.loop(Looper.java:136) 
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5102) 
                                                                                     at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                     at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
                                                                                     at dalvik.system.NativeStart.main(Native Method) 

这是我的Java文件:

package com.example.oreoman.failuredude;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Button;
import android.view.View;
import android.util.Log;
import android.media.MediaRecorder;
import android.media.MediaPlayer;

import java.io.IOException;


public class AudioRecordTest extends Activity
{
    private static final String LOG_TAG = "AudioRecordTest";

    private static String mFileA = null;
    private static String mFileB = null;
    private static String mFileC = null;
    private static String mFileD = null;

    boolean mStartPlaying = true;
    boolean mStartRecording = true;

    //private Button tempButt = null;

    private MediaRecorder mRecorder = null;

    private MediaPlayer   mPlayer = null;

    private void onRecord(boolean start, String theOne) {
        if (start) {
            startRecording(theOne);
        } else {
            stopRecording();
        }
    }

    private void onPlay(boolean start, String theOne) {
        if (start) {
            startPlaying(theOne);
        } else {
            stopPlaying();
        }
    }

    private void startPlaying(String theOne) {
        mFileA = Environment.getExternalStorageDirectory().getAbsolutePath();
        mPlayer = new MediaPlayer();
        try {
            switch(theOne){
                case "mPlayA":
                    mFileA += "/dakotaApp/audio1.mp4";
                    mPlayer.setDataSource(mFileA);
                    break;
                case "mPlayB":
                    mFileB = mFileA;
                    mFileB += "/dakotaApp/audio1.mp4";
                    mPlayer.setDataSource(mFileB);
                    break;
                case "mPlayC":
                    mFileC = mFileA;
                    mFileC += "/dakotaApp/audio1.mp4";
                    mPlayer.setDataSource(mFileC);
                    break;
                case "mPlayD":
                    mFileD = mFileA;
                    mFileD += "/dakotaApp/audio1.mp4";
                    mPlayer.setDataSource(mFileD);
                    break;
            }
            mPlayer.prepare();
            mPlayer.start();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }
    }

    private void stopPlaying() {
        mPlayer.release();
        mPlayer = null;
    }

    private void startRecording(String theOne) {
        mFileA = Environment.getExternalStorageDirectory().getAbsolutePath();
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        switch(theOne){
            case "mRecordA":
                mFileA += "/dakotaApp/audio1.mp4";
                mRecorder.setOutputFile(mFileA);
                break;
            case "mRecordB":
                mFileB = mFileA;
                mFileB += "/dakotaApp/audio1.mp4";
                mRecorder.setOutputFile(mFileB);
                break;
            case "mRecordC":
                mFileC = mFileA;
                mFileC += "/dakotaApp/audio1.mp4";
                mRecorder.setOutputFile(mFileC);
                break;
            case "mRecordD":
                mFileD = mFileA;
                mFileD += "/dakotaApp/audio1.mp4";
                mRecorder.setOutputFile(mFileD);
                break;
        }
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

        try {
            mRecorder.prepare();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }

        mRecorder.start();
    }

    private void stopRecording() {
        mRecorder.stop();
        mRecorder.release();
        mRecorder = null;
    }

    public void rClick(View v) {
        String theOne = v.getResources().getResourceEntryName(v.getId());
        onRecord(mStartRecording, theOne);
        switch(theOne) {
            case "mRecordA":
                Button tempButt = (Button)findViewById(R.id.mRecordA);
                if(mStartRecording) tempButt.setText("Stop Recording");
                else tempButt.setText("Start Recording");
                break;
            case "mRecordB":
                tempButt = (Button)findViewById(R.id.mRecordB);
                if(mStartRecording) tempButt.setText("Stop Recording");
                else tempButt.setText("Start Recording");
                break;
            case "mRecordC":
                tempButt = (Button)findViewById(R.id.mRecordC);
                if(mStartRecording) tempButt.setText("Stop Recording");
                else tempButt.setText("Start Recording");
                break;
            case "mRecordD":
                tempButt = (Button)findViewById(R.id.mRecordD);
                if(mStartRecording) tempButt.setText("Stop Recording");
                else tempButt.setText("Start Recording");
                break;
        }
        mStartRecording = !mStartRecording;
    }

    public void pClick(View v) {
        String theOne = v.getResources().getResourceEntryName(v.getId());
        onPlay(mStartPlaying, theOne);
        switch(theOne) {
            case "mPlayA":
                Button tempButt = (Button)findViewById(R.id.mPlayA);
                if(mStartRecording) tempButt.setText("Stop Playing");
                else tempButt.setText("Start Playing");
                break;
            case "mPlayB":
                tempButt = (Button)findViewById(R.id.mPlayB);
                if(mStartRecording) tempButt.setText("Stop Playing");
                else tempButt.setText("Start Playing");
                break;
            case "mPlayC":
                tempButt = (Button)findViewById(R.id.mPlayC);
                if(mStartRecording) tempButt.setText("Stop Playing");
                else tempButt.setText("Start Playing");
                break;
            case "mPlayD":
                tempButt = (Button)findViewById(R.id.mPlayD);
                if(mStartRecording) tempButt.setText("Stop Playing");
                else tempButt.setText("Start Playing");
                break;
        }
        mStartPlaying = !mStartPlaying;
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mRecorder != null) {
            mRecorder.release();
            mRecorder = null;
        }

        if (mPlayer != null) {
            mPlayer.release();
            mPlayer = null;
        }
    }

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

活动文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".AudioRecordTest">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Dakota&apos;s Awesome\nSuper Fun App of Sound!"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textAlignment="center" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start recording"
        android:id="@+id/mRecordA"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="70dp"
        android:onClick="rClick" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start playing"
        android:id="@+id/mPlayA"
        android:layout_alignTop="@+id/mRecordA"
        android:layout_marginLeft="220dp"
        android:onClick="pClick" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start recording"
        android:id="@+id/mRecordB"
        android:layout_below="@+id/mRecordA"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="50dp"
        android:onClick="rClick" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start playing"
        android:id="@+id/mPlayB"
        android:layout_alignTop="@+id/mRecordB"
        android:layout_marginLeft="220dp"
        android:onClick="pClick" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start recording"
        android:id="@+id/mRecordC"
        android:layout_below="@+id/mRecordB"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="50dp"
        android:onClick="rClick" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start playing"
        android:id="@+id/mPlayC"
        android:layout_alignTop="@+id/mRecordC"
        android:layout_marginLeft="220dp"
        android:onClick="pClick" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start recording"
        android:id="@+id/mRecordD"
        android:layout_below="@+id/mRecordC"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="50dp"
        android:onClick="rClick" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start playing"
        android:id="@+id/mPlayD"
        android:layout_alignTop="@+id/mRecordD"
        android:layout_marginLeft="220dp"
        android:onClick="pClick" />

</RelativeLayout>

最后,清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.oreoman.failuredude" >

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".AudioRecordTest" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
洪扎·穆西尔(Honza Musil)

这是您的例外,不是您提到的例外:

由以下原因引起:com.example.oreoman.failuredude.AudioRecordTest.startRecording(AudioRecordTest.java:120)上android.media.MediaRecorder.start(本机方法)上的java.lang.IllegalStateException

检查mRecorder.prepare(); 引发异常。如果是这样,请更正错误。

检查setOutputFile()中的路径是否指向现有文件。

检查清单中是否具有此权限:

<uses-permission android:name="android.permission.RECORD_AUDIO" />

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章