Your XML is looking for MainActivity
while your class is named "SoundRecorder
". The name should be the same.
Android FATAL EXCEPTION in main
-
28-06-2023 - |
Pergunta
I'm trying to develop android app that need user voice recording. i see many code but nothing work with me.
here is the code :
package com.example.record8;
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
public class SoundRecorder extends Activity {
MediaRecorder recorder;
File audiofile = null;
private static final String TAG = "SoundRecordingActivity";
private View startButton;
private View stopButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startButton = findViewById(R.id.btnStart);
stopButton = findViewById(R.id.btnStop);
}
public void startRecording(View view) throws IOException {
startButton.setEnabled(false);
stopButton.setEnabled(true);
File sampleDir = Environment.getExternalStorageDirectory();
try {
audiofile = File.createTempFile("sound", ".3gp", sampleDir);
} catch (IOException e) {
Log.e(TAG, "sdcard access error");
return;
}
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(audiofile.getAbsolutePath());
recorder.prepare();
recorder.start();
}
public void stopRecording(View view) {
startButton.setEnabled(true);
stopButton.setEnabled(false);
recorder.stop();
recorder.release();
addRecordingToMediaLibrary();
}
protected void addRecordingToMediaLibrary() {
ContentValues values = new ContentValues(4);
long current = System.currentTimeMillis();
values.put(MediaStore.Audio.Media.TITLE, "audio" + audiofile.getName());
values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));
values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp");
values.put(MediaStore.Audio.Media.DATA, audiofile.getAbsolutePath());
ContentResolver contentResolver = getContentResolver();
Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Uri newUri = contentResolver.insert(base, values);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
Toast.makeText(this, "Added File " + newUri, Toast.LENGTH_LONG).show();
}
}
i don't know the error i see this log
04-05 14:03:57.094: E/AndroidRuntime(9726): FATAL EXCEPTION: main
04-05 14:03:57.094: E/AndroidRuntime(9726): java.lang.RuntimeException:Unable to instantiate activity ComponentInfo{com.example.record8/com.example.record8.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.record8.MainActivity" on path: /data/app/com.example.record8-1.apk
04-05 14:03:57.094: E/AndroidRuntime(9726): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2219)
04-05 14:03:57.094: E/AndroidRuntime(9726): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
04-05 14:03:57.094: E/AndroidRuntime(9726): at android.app.ActivityThread.access$700(ActivityThread.java:159)
04-05 14:03:57.094: E/AndroidRuntime(9726): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
04-05 14:03:57.094: E/AndroidRuntime(9726): at android.os.Handler.dispatchMessage(Handler.java:99)
04-05 14:03:57.094: E/AndroidRuntime(9726): at android.os.Looper.loop(Looper.java:176)
04-05 14:03:57.094: E/AndroidRuntime(9726): at android.app.ActivityThread.main(ActivityThread.java:5419)
04-05 14:03:57.094: E/AndroidRuntime(9726): at java.lang.reflect.Method.invokeNative(Native Method)
04-05 14:03:57.094: E/AndroidRuntime(9726): at java.lang.reflect.Method.invoke(Method.java:525)
04-05 14:03:57.094: E/AndroidRuntime(9726): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
04-05 14:03:57.094: E/AndroidRuntime(9726): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
04-05 14:03:57.094: E/AndroidRuntime(9726): at dalvik.system.NativeStart.main(Native Method)
04-05 14:03:57.094: E/AndroidRuntime(9726): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.record8.MainActivity" on path: /data/app/com.example.record8-1.apk
04-05 14:03:57.094: E/AndroidRuntime(9726): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:64)
04-05 14:03:57.094: E/AndroidRuntime(9726): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
04-05 14:03:57.094: E/AndroidRuntime(9726): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
04-05 14:03:57.094: E/AndroidRuntime(9726): at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
04-05 14:03:57.094: E/AndroidRuntime(9726): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2210)
04-05 14:03:57.094: E/AndroidRuntime(9726): ... 11 more
here is the layout
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginLeft="45dp"
android:layout_marginTop="48dp"
android:text="@string/startrecording" />
<Button
android:id="@+id/btnStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btnStart"
android:layout_below="@+id/btnStart"
android:layout_marginTop="52dp"
android:text="@string/stoprecording" />
<Button
android:id="@+id/btnFormat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnStop"
android:layout_centerHorizontal="true"
android:layout_marginTop="75dp"
android:text="@string/audio_format" />
how can i solve this problem
Solução
Outras dicas
tools:context=".MainActivity"
This line says your activity name is MainActivity. But your Activity's actual name is SoundRecorder
. Changing it should solve the problem.
Clean you project, uninstall the application from you test device and reinstall.
Important: Use same name in your xml and mainactivity
logcat is saying that it can not find mainactivity because there isn't any
The stack trace says that Android can't create the Activity ".MainActivity". This point you to the Manifest (which you didn't post). Obviously you defined in your Mainfest the Activity with attribute name=".MainActivity" while your classes name is "SoundRecorder". Change either.