0
votes

I am trying to create an audio capture activity so I copied the class from the android docs that looks like this

package com.android.audiorecordtest;

import android.app.Activity;
import android.widget.LinearLayout;
import android.os.Bundle;
import android.os.Environment;
import android.view.ViewGroup;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Context;
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 mFileName = null;

    private RecordButton mRecordButton = null;
    private MediaRecorder mRecorder = null;

    private PlayButton   mPlayButton = null;
    private MediaPlayer   mPlayer = null;

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

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

    private void startPlaying() {
        mPlayer = new MediaPlayer();
        try {
            mPlayer.setDataSource(mFileName);
            mPlayer.prepare();
            mPlayer.start();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }
    }

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

    private void startRecording() {
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setOutputFile(mFileName);
        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;
    }

    class RecordButton extends Button {
        boolean mStartRecording = true;

        OnClickListener clicker = new OnClickListener() {
            public void onClick(View v) {
                onRecord(mStartRecording);
                if (mStartRecording) {
                    setText("Stop recording");
                } else {
                    setText("Start recording");
                }
                mStartRecording = !mStartRecording;
            }
        };

        public RecordButton(Context ctx) {
            super(ctx);
            setText("Start recording");
            setOnClickListener(clicker);
        }
    }

    class PlayButton extends Button {
        boolean mStartPlaying = true;

        OnClickListener clicker = new OnClickListener() {
            public void onClick(View v) {
                onPlay(mStartPlaying);
                if (mStartPlaying) {
                    setText("Stop playing");
                } else {
                    setText("Start playing");
                }
                mStartPlaying = !mStartPlaying;
            }
        };

        public PlayButton(Context ctx) {
            super(ctx);
            setText("Start playing");
            setOnClickListener(clicker);
        }
    }

    public AudioRecordTest() {
        mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
        mFileName += "/audiorecordtest.3gp";
    }

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        LinearLayout ll = new LinearLayout(this);
        mRecordButton = new RecordButton(this);
        ll.addView(mRecordButton,
            new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                0));
        mPlayButton = new PlayButton(this);
        ll.addView(mPlayButton,
            new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                0));
        setContentView(ll);
    }

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

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

I renamed the activity to MicrophoneActivity and I placed it in a package called activity

Now I want to instead of programming in the layout I want to use an xml file.

So I edited the onCreate to look like the following

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

    mRecordButton = (RecordButton) findViewById(R.id.microphone);

}

And in my xml page I am trying to create a RecordButton with id microphone. Something like this

<com.heyprestoapp.heypresto.activity.MicrophoneActivity.RecordButton
    android:id="@+id/microphone"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="40dp"/>

Now when I try run the app crashes with the following output

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.heyprestoapp.heypresto, PID: 22471 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.heyprestoapp.heypresto/com.heyprestoapp.heypresto.activity.MicrophoneActivity}: android.view.InflateException: Binary XML file line #18: Binary XML file line #18: Error inflating class com.heyprestoapp.heypresto.activity.MicrophoneActivity.RecordButton at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3319) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415) at android.app.ActivityThread.access$1100(ActivityThread.java:229) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:7325) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) Caused by: android.view.InflateException: Binary XML file line #18: Binary XML file line #18: Error inflating class com.heyprestoapp.heypresto.activity.MicrophoneActivity.RecordButton at android.view.LayoutInflater.inflate(LayoutInflater.java:551) at android.view.LayoutInflater.inflate(LayoutInflater.java:429) at android.view.LayoutInflater.inflate(LayoutInflater.java:380) at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:479) at android.app.Activity.setContentView(Activity.java:2400) at com.heyprestoapp.heypresto.activity.MicrophoneActivity.onCreate(MicrophoneActivity.java:149) at android.app.Activity.performCreate(Activity.java:6904) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)  at android.app.ActivityThread.access$1100(ActivityThread.java:229)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:7325)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class com.heyprestoapp.heypresto.activity.MicrophoneActivity.RecordButton at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:788) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716) at android.view.LayoutInflater.rInflate(LayoutInflater.java:847) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810) at android.view.LayoutInflater.inflate(LayoutInflater.java:527) at android.view.LayoutInflater.inflate(LayoutInflater.java:429)  at android.view.LayoutInflater.inflate(LayoutInflater.java:380)  at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:479)  at android.app.Activity.setContentView(Activity.java:2400)  at com.heyprestoapp.heypresto.activity.MicrophoneActivity.onCreate(MicrophoneActivity.java:149)  at android.app.Activity.performCreate(Activity.java:6904)  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)  at android.app.ActivityThread.access$1100(ActivityThread.java:229)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:7325)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.heyprestoapp.heypresto.activity.MicrophoneActivity.RecordButton" on path: DexPathList[[zip file "/data/app/com.heyprestoapp.heypresto-2/base.apk"],nativeLibraryDirectories=[/data/app/com.heyprestoapp.heypresto-2/lib/arm, /vendor/lib, /system/lib]] at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56) at java.lang.ClassLoader.loadClass(ClassLoader.java:511) at java.lang.ClassLoader.loadClass(ClassLoader.java:469) at android.view.LayoutInflater.createView(LayoutInflater.java:595) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:776) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)  at android.view.LayoutInflater.rInflate(LayoutInflater.java:847)  at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)  at android.view.LayoutInflater.inflate(LayoutInflater.java:527)  at android.view.LayoutInflater.inflate(LayoutInflater.java:429)  at android.view.LayoutInflater.inflate(LayoutInflater.java:380)  at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:479)  at android.app.Activity.setContentView(Activity.java:2400)  at com.heyprestoapp.heypresto.activity.MicrophoneActivity.onCreate(MicrophoneActivity.java:149)  at android.app.Activity.performCreate(Activity.java:6904)  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)  at android.app.ActivityThread.access$1100(ActivityThread.java:229)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:7325)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
Suppressed: java.lang.ClassNotFoundException: Didn't find class "com.heyprestoapp.heypresto.activity.MicrophoneActivity.RecordButton" on path: DexPathList[[dex file "/data/data/com.heyprestoapp.heypresto/files/instant-run/dex/slice-support-annotations-24.1.1_f2d68394ace2648d246e9f3a6f79e1c800e15e57-classes.dex", dex file "/data/data/com.heyprestoapp.heypresto/files/instant-run/dex/slice-smack-tcp-4.1.0_831e36ba6f57de45ead9e83b8319b14f30ea801e-classes.dex", dex file "/data/data/com.heyprestoapp.heypresto/files/instant-run/dex/slice-smack-sasl-provided-4.1.3_396a290b83a0453d3a97b95e8c362f0a61ef9c13-classes.dex", dex file "/data/data/com.heyprestoapp.heypresto/files/instant-run/dex/slice-smack-resolver-minidns-4.1.3_427e3bfc74840e180f88b6f0e4b12b2c05808d12-classes.dex", dex file "/data/data/com.heyprestoapp.heypresto/files/instant-run/dex/slice-smack-im-4.1.3_7ebbb2471a6968d543236c3d4074f7a01c7a90a1-classes.dex", dex file "/data/data/com.heyprestoapp.hey

2
post the code for RecordButton.SaravInfern
Does this class exists in your package? <com.heyprestoapp.heypresto.activity.MicrophoneActivity.RecordButton because you are saying in your xml file that you need this object but you don't mention it / show it in your example.Greg
you should use this instead : com.android.audiorecordtest.RecordButton in your xmlGreg
RecordButton is there in the code? It is a class inside the other class Estoke: I renamed AudioRecordTest to MicrophoneActivityAdam Katz

2 Answers

0
votes

Suppressed: java.lang.ClassNotFoundException: Didn't find class "com.heyprestoapp.heypresto.activity.MicrophoneActivity.RecordButton"

Seems like your RecordButton class is not indexed, Clean and Rebuild your code, may fix you problem

0
votes

Okay the answer was pretty simple, I needed to make RecordButton public.