Android FATAL EXCEPTION主要

时间:2014-04-05 11:23:36

标签: android android-manifest

我正在尝试开发需要用户录音的Android应用程序。 我看到很多代码,但没有任何关系。

这是代码:

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();
}
  }

我不知道我看到这个日志的错误

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

这是布局

<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" />

我该如何解决这个问题

4 个答案:

答案 0 :(得分:0)

tools:context=".MainActivity"

此行表示您的活动名称为MainActivity。但您的活动的实际名称是SoundRecorder。改变它应该可以解决问题。

答案 1 :(得分:0)

当您的班级名为“MainActivity”时,您的XML正在寻找SoundRecorder。名称应该相同。

答案 2 :(得分:0)

清理项目,从测试设备卸载应用程序并重新安装。

重要:在xml和mainactivity中使用相同的名称

logcat说无法找到主要活动,因为没有任何

答案 3 :(得分:0)

堆栈跟踪显示Android无法创建活动&#34;。主要活动&#34;。这指向你的Manifest(你没有发布)。显然,您在Mainfest中定义了具有属性名称=&#34; .MainActivity&#34;而你的课程名称是&#34; SoundRecorder&#34;。改变。