2

When I'm trying to run the app I get the exception which is shown in the Title.

This is my xml File:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.example.ruben.fileapp.MainActivity.PlayButton
            android:id="@+id/play_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <com.example.ruben.fileapp.MainActivity.RecordButton
            android:id="@+id/record_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

</LinearLayout>

This is my MainActivity.java:

package com.example.ruben.fileapp;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {


    private static final String LOG_TAG = "AudioRecordTest";
    private static final int REQUEST_RECORD_AUDIO_PERMISSION = 200;
    private static String mFileName = null;

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

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

    // Requesting permission to RECORD_AUDIO
    private boolean permissionToRecordAccepted = false;
    private String [] permissions = {Manifest.permission.RECORD_AUDIO};



    //region permissionMethod
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode){
            case REQUEST_RECORD_AUDIO_PERMISSION:
                permissionToRecordAccepted  = grantResults[0] == PackageManager.PERMISSION_GRANTED;
                break;
        }

    if (!permissionToRecordAccepted ) finish();

}
//endregion

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;
}
//region RecordButton
public class RecordButton extends AppCompatButton {
    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);
    }
}
//endregion

//region PlayButton
public class PlayButton extends AppCompatButton {
    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);
    }
}
//endregion

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


    // Record to the external cache directory for visibility
    mFileName = getExternalCacheDir().getAbsolutePath();
    mFileName += "/audiorecordtest.3gp";

    ActivityCompat.requestPermissions(this, permissions, REQUEST_RECORD_AUDIO_PERMISSION);

    LinearLayout ll = new LinearLayout(this);

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

    mPlayButton = (PlayButton) findViewById(R.id.play_button);
    /*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 onStop() {
    super.onStop();
    if (mRecorder != null) {
        mRecorder.release();
        mRecorder = null;
    }

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

}

This is my full logcat:

09-04 12:36:45.898 6198-6198/? I/art: Late-enabling -Xcheck:jni
    Reinit property: dalvik.vm.checkjni= false
09-04 12:36:46.143 6198-6198/com.example.ruben.fileapp W/System: ClassLoader referenced unknown path: /data/app/com.example.ruben.fileapp-1/lib/arm64
09-04 12:36:46.156 6198-6198/com.example.ruben.fileapp I/InstantRun: starting instant run server: is main process
09-04 12:36:46.185 6198-6198/com.example.ruben.fileapp I/HwCust: Constructor found for class android.app.HwCustActivityImpl
09-04 12:36:46.210 6198-6198/com.example.ruben.fileapp I/HwCust: Constructor found for class android.app.HwCustHwWallpaperManagerImpl
09-04 12:36:46.235 6198-6198/com.example.ruben.fileapp W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
09-04 12:36:46.318 6198-6198/com.example.ruben.fileapp E/HW-JPEG-DEC: [HME_JPEG_DEC_Delete](3321): HME_JPEG_DEC_Delete: decoder_ctx=null
09-04 12:36:46.344 6198-6198/com.example.ruben.fileapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.ruben.fileapp, PID: 6198
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ruben.fileapp/com.example.ruben.fileapp.MainActivity}: android.view.InflateException: Binary XML file line #14: Binary XML file line #14: Error inflating class com.example.ruben.fileapp.MainActivity.PlayButton
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2793)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:156)
        at android.app.ActivityThread.main(ActivityThread.java:6523)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831)
     Caused by: android.view.InflateException: Binary XML file line #14: Binary XML file line #14: Error inflating class com.example.ruben.fileapp.MainActivity.PlayButton
     Caused by: android.view.InflateException: Binary XML file line #14: Error inflating class com.example.ruben.fileapp.MainActivity.PlayButton
     Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.ruben.fileapp.MainActivity.PlayButton" on path: DexPathList[[zip file "/data/app/com.example.ruben.fileapp-1/base.apk", zip file "/data/app/com.example.ruben.fileapp-1/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.ruben.fileapp-1/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.ruben.fileapp-1/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.ruben.fileapp-1/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.ruben.fileapp-1/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.ruben.fileapp-1/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.ruben.fileapp-1/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.ruben.fileapp-1/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.ruben.fileapp-1/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.ruben.fileapp-1/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.ruben.fileapp-1/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.ruben.fileapp-1/lib/arm64, /system/lib64, /vendor/lib64, /system/vendor/lib64, /product/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at android.view.LayoutInflater.createView(LayoutInflater.java:616)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:798)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:738)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:869)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:832)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:872)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:832)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
        at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
        at com.example.ruben.fileapp.MainActivity.onCreate(MainActivity.java:158)
        at android.app.Activity.performCreate(Activity.java:6910)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2746)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:156)
        at android.app.ActivityThread.main(ActivityThread.java:6523)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831)
09-04 12:36:46.353 6198-6198/com.example.ruben.fileapp I/Process: Sending signal. PID: 6198 SIG: 9

I would appreciate any kind of help.

Thank you

P.S.: This is my first question posted on stackoverflow, if something is bad in the way I'm asking, feel free to tell me.

1
  • 1
    You could give a quick overview of the purpose of each class involved (here just MainActivity). Just explain what your code does before posting the code itself. It is usually appreciated to do so because the reader will not start from scratch when reading the code, instead the reader has an idea of what you are actually trying to accomplish. Commented Sep 4, 2018 at 10:57

4 Answers 4

1

You're calling MainActivity.RecordButton but the RecordButton isn't a subclass of MainActivity.

Your RecordButton and PlayButton are public classes, which means they are in their own files, probaly at the same level as the MainActivity.

try

<com.example.ruben.fileapp.PlayButton
        android:id="@+id/play_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
Sign up to request clarification or add additional context in comments.

3 Comments

The Mainactivity.RecordButton is an inner Class of the MainActivity Class. I've tried but it still don't work :/ ... When I use com.example.ruben.fileapp.MainActivity.Playbutton, I can see the button View in the xml preview. Without "Mainactivity" the view does not show properly.
but in the code you showed here it was shown as public. When you prepend public to a class definition it should be in it's own file.
I have taken away the public parameter and tried it with both pathes (...fileapp.MainActivity.Playbutton & fileapp.Playbutton), it still doesn't work. Is it possible to inflate these classes without putting them into their own files ?
0

Make separate class for custom button instead of mainactivity

   com.example.ruben.fileapp.MainActivity.PlayButton

move your PlayButton class into other location like

   com.example.ruben.fileapp.PlayButton

1 Comment

my custom button class is located in an inner class of my MainActivity. This path is the same shown in my xml file
0

Please make sure that android.enableJetifier=true exists in the gradle.properties it's not there by default anymore and you will face ambiguous exceptions when some library inside your code still uses the support libraries rather than AndroidX's.

Comments

0

For some reason, static inner classes don't work. Moving the class to its own file solved the problem for me.

3 Comments

In the code you posted, the inner classes are not static - that would be a key problem.
@dominicoder I didn't post any code :D. I just had the same problem, and in my case, the inner class was indeed static, but I had the same exception. I thought I should clarify that inner classes, even static ones, DON'T work.
Sorry, skimmed too fast, I thought you were the OP answering your own question. Disregard.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.