java - Android MediaRecorder NullPointerException

标签 java android

我刚开始使用 Java 开发,正在摆弄 Android 媒体记录器。但是我得到一个 NullPointerException,我使用以下源代码单击记录按钮:

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
//import android.net.Uri;
import android.view.Menu;
import android.view.MenuItem;
import android.os.Environment;
import android.view.View;
import java.io.File;
import java.io.IOException;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import java.lang.String;


public class MainActivity extends ActionBarActivity {
   String outputFile = "test.3gp";
    private MediaRecorder myAudioRecorder;
    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MediaRecorder myAudioRecorder = new MediaRecorder();
        myAudioRecorder = new MediaRecorder();
       // Uri target = Uri.parse(outputFile.getAbsolutePath());
        myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
        myAudioRecorder.setOutputFile(outputFile);

    }

    public void Start_Record(View v)
    {
        try {
            myAudioRecorder.prepare();
            myAudioRecorder.start();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        myAudioRecorder.stop();
        myAudioRecorder.release();

    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

我意识到这可能是由于填充指针被设置为 NULL,但是我正在努力了解如何初始化文件引用以使其消失。我在 mediaRecorderPrepare() 上遇到异常

这是抛出的异常:

03-09 22:53:56.548  31836-31836/uk.co.spectralgear.spectralboost E/AndroidRuntime﹕ FATAL EXCEPTION: main

    java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:4007)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4002)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaRecorder.prepare()' on a null object reference
            at uk.co..MainActivity.Start_Record(MainActivity.java:38)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4002)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

任何帮助将不胜感激:)

最佳答案

问题: 当“Start_Record()”尝试使用它时,您的类变量“myAudioRecord”为空:

Caused by: java.lang.NullPointerException: 
Attempt to invoke virtual method 'void Android.media.MediaRecorder.prepare()' on a null object reference

解决方案:从“onCreate()”方法中删除这一行:

public class MainActivity extends ActionBarActivity {
    String outputFile = "test.3gp";
    private MediaRecorder myAudioRecorder;  // Your class variable: OK...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        // MediaRecorder myAudioRecorder = new MediaRecorder();  // DELETE THIS LINE!!!
        myAudioRecorder = new MediaRecorder();  // OK...
        ...

onCreate() 中的“myAudioRecorder”声明隐藏 外部声明。因此,外部(类级)“myAudioRecorder”永远不会被初始化……当“Start_Record()”尝试访问它时,您的程序会因 NPE 而死。

附言:

请考虑将方法名称更改为“start_Record()”。或者更好的是,“startRecord()”。这更符合 Java 命名约定。

关于java - Android MediaRecorder NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28953798/

相关文章:

java - 以编程方式跟踪由 eclipse cleanup 完成的文件更改

java.io.NotSerializableException : -> No constructor for deserialization found for class java. sql.时间戳

java - 在 @Configuration bean 的 SpEL 表达式中引用 ConfigurationProperties Beans

java - 将 JSON 输出到文件,然后将其输入回

java - 如何检查我的线程是否正在运行 Java

java - 为每个给定的数组大小在数组中添加数字 - Java

android - 动画围绕 ImageView 的圆圈

java - 在 Android 中发送 HTML 格式的电子邮件

Android:MapActivity 给出 Nullpointerexception

android - 如何从 Android 中的 AlertDialog 获取文本