1

我看到这个错误出现了近一百万次,人们得到了帮助。作为一个初学者,我想为我正在开发的简单媒体记录器寻求一些帮助。请帮忙。

下面提到的是代码 -

add_company.java

    public class add_company extends Activity{

        //Adding for voice record
        ImageButton play,record,stop;
        private String s,voicerec;
        MediaPlayer mp;
        MediaRecorder mr;

        @Override
        public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            setContentView(R.layout.add_company);

            //Adding voice record
            play = (ImageButton)findViewById(R.id.Play);
            record = (ImageButton)findViewById(R.id.Record);
            stop = (ImageButton)findViewById(R.id.Stop);

            File audioFile = null;
            try {
                audioFile = File.createTempFile(null, ".amr");
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            mr = new MediaRecorder();

            s = audioFile.getAbsolutePath();
            voicerec=s;
            System.out.println("Path: "+s);

            mr.setAudioSource(MediaRecorder.AudioSource.MIC);
            mr.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
            mr.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            mr.setOutputFile(s);
            try{
                mr.prepare();
            }
            catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }

        }

        public void record(View v){
            Toast.makeText(add_company.this, "Recording", Toast.LENGTH_LONG).show();
            mr.start();
        }
        public void stop(View v){
            mr.stop();  //this is stop recording

            Toast.makeText(add_company.this, "Stopped", Toast.LENGTH_LONG).show();
            mr.reset();     // this is required to avoid the error:  Fatal signal 11 (SIGSEGV) at 0x00000010 (code=1)
            mr.release();
        }
        public void play(View v){
            File f = new File(voicerec);
            Uri u = Uri.fromFile(f);
            mp = MediaPlayer.create(add_company.this, u);
            mp.setLooping(false);
            mp.start();
        }

    }

LogCat 详细信息:

    06-18 10:31:53.281: E/AndroidRuntime(2740): FATAL EXCEPTION: main
    06-18 10:31:53.281: E/AndroidRuntime(2740): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tg_db1/com.example.tg_db1.add_company}: java.lang.NullPointerException
    06-18 10:31:53.281: E/AndroidRuntime(2740):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
    06-18 10:31:53.281: E/AndroidRuntime(2740):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    06-18 10:31:53.281: E/AndroidRuntime(2740):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
    06-18 10:31:53.281: E/AndroidRuntime(2740):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    06-18 10:31:53.281: E/AndroidRuntime(2740):     at android.os.Handler.dispatchMessage(Handler.java:99)
    06-18 10:31:53.281: E/AndroidRuntime(2740):     at android.os.Looper.loop(Looper.java:137)
    06-18 10:31:53.281: E/AndroidRuntime(2740):     at android.app.ActivityThread.main(ActivityThread.java:5039)
    06-18 10:31:53.281: E/AndroidRuntime(2740):     at java.lang.reflect.Method.invokeNative(Native Method)
    06-18 10:31:53.281: E/AndroidRuntime(2740):     at java.lang.reflect.Method.invoke(Method.java:511)
    06-18 10:31:53.281: E/AndroidRuntime(2740):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    06-18 10:31:53.281: E/AndroidRuntime(2740):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    06-18 10:31:53.281: E/AndroidRuntime(2740):     at dalvik.system.NativeStart.main(Native Method)
    06-18 10:31:53.281: E/AndroidRuntime(2740): Caused by: java.lang.NullPointerException
    06-18 10:31:53.281: E/AndroidRuntime(2740):     at java.io.File.createTempFile(File.java:999)
    06-18 10:31:53.281: E/AndroidRuntime(2740):     at java.io.File.createTempFile(File.java:970)
    06-18 10:31:53.281: E/AndroidRuntime(2740):     at com.example.tg_db1.add_company.onCreate(add_company.java:64)
    06-18 10:31:53.281: E/AndroidRuntime(2740):     at android.app.Activity.performCreate(Activity.java:5104)
    06-18 10:31:53.281: E/AndroidRuntime(2740):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
    06-18 10:31:53.281: E/AndroidRuntime(2740):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
    06-18 10:31:53.281: E/AndroidRuntime(2740):     ... 11 more
4

1 回答 1

5

File.createTempFile(null, ".amr");

前缀不能为空。

源代码

    public static File createTempFile(String prefix, String suffix, File directory) throws IOException
    {
         if (prefix == null) throw new NullPointerException();
         /***/
     }
于 2013-06-18T10:40:09.487 回答