[英]Error inflating class android.view.InflateException, Didn't find class java.lang.ClassNotFoundException
When I'm trying to run the app I get the exception which is shown in the Title.当我尝试运行该应用程序时,我得到了标题中显示的异常。
This is my xml File:这是我的 xml 文件:
<?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:这是我的 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谢谢
PS: This is my first question posted on stackoverflow, if something is bad in the way I'm asking, feel free to tell me. PS:这是我在stackoverflow上发布的第一个问题,如果我问的方式不好,请随时告诉我。
You're calling MainActivity.RecordButton but the RecordButton isn't a subclass of MainActivity.您正在调用 MainActivity.RecordButton 但 RecordButton 不是 MainActivity 的子类。
Your RecordButton and PlayButton are public classes, which means they are in their own files, probaly at the same level as the MainActivity.您的 RecordButton 和 PlayButton 是公共类,这意味着它们在自己的文件中,可能与 MainActivity 处于同一级别。
try尝试
<com.example.ruben.fileapp.PlayButton
android:id="@+id/play_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Make separate class for custom button instead of mainactivity为自定义按钮创建单独的类而不是 mainactivity
com.example.ruben.fileapp.MainActivity.PlayButton
move your PlayButton class into other location like将您的 PlayButton 类移动到其他位置,例如
com.example.ruben.fileapp.PlayButton
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.请确保 gradle.properties 中存在gradle.properties
android.enableJetifier=true
默认情况下不再存在,并且当您的代码中的某些库仍然使用支持库而不是 AndroidX 时,您将面临模棱两可的异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.