Question

I am working on a Media Recorder application using android API 17. However, I get Fatal exception:main and Null Pointer Exception error during run time: i.e, while pressing record button get unfortunately stopped working error. What am I doing wrong?

Here is my mainfest file:

<uses-sdk
    android:minSdkVersion="16"
    android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="co.example.projectplay.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

This is my .java file

protected void onCreate(Bundle savedInstanceState){

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FILE=Environment.getExternalStorageState()+"/tempRecord.3gpp";

    botn1=(Button)findViewById(R.id.botn1);
    botn1.setOnClickListener(new View.OnClickListener() 
    {

        @Override
        public void onClick(View v)
        {

            play= MediaPlayer.create(MainActivity.this,R.raw.a);
            play.start();
            play.setOnCompletionListener(new OnCompletionListener()
            {
                public void onCompletion(MediaPlayer play)
                {
                    play.release();
                }
            }
            );
        }
    });


    botn2=(Button)findViewById(R.id.botn2);
    botn2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            play= MediaPlayer.create(MainActivity.this,R.raw.b);
            play.start();
            play.setOnCompletionListener(new OnCompletionListener()
            {
                public void onCompletion(MediaPlayer play)
                {
                    play.release();
                }
            });
        }
    });
    botn3=(Button)findViewById(R.id.botn3);
    botn3.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            play= MediaPlayer.create(MainActivity.this,R.raw.c);
            play.start();
            play.setOnCompletionListener(new OnCompletionListener()
            {
                public void onCompletion(MediaPlayer play)
                {
                    play.release();
                }
            });
        }
    });

    botnRecord=(Button)findViewById(R.id.botnRecord);
    botnRecord.setOnClickListener(new View.OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            if (botnRecord.getText().toString().equals("Record"))
             {
                try 
                {
                    startRecord();
                } 
                catch (Exception e) 

                {
                    e.printStackTrace();
                }

                txtButton.setText("Recording...");
                botnRecord.setText("End");
             }
            else if(botnRecord.getText().toString().equals("End"))
             {
                stopRecord();
                txtButton.setText("");
                botnRecord.setText("Play");
             }
            else if(botnRecord.getText().toString().equals("Play"))
            {
                try {
                    startPlayback();
                } 
                catch (Exception e) 
                {

                    e.printStackTrace();

                }
                botnRecord.setText("Stop");
            }

            else
            {
                stopPlayback();
                botnRecord.setText("Record");
            }
        }   
    });
}  

public void startRecord()throws Exception
{
    if(record!=null)
    {
        //record.release();
        record.reset();
    }

    File fileOut=new File(FILE);

    if(fileOut!=null)
    {
        fileOut.delete();
    }
    record=new MediaRecorder();
    record.setAudioSource(MediaRecorder.AudioSource.MIC);
    record.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    record.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    record.setOutputFile(FILE);
    record.prepare();
    record.start();

}
public void stopRecord()
{
    record.stop();
    record.release();
}
public void startPlayback()throws Exception
{
    if(play!=null)
    {
        play.stop();
        play.release();
    }
    play=new MediaPlayer();
    play.setDataSource(FILE);
    play.prepare();
    play.start();
    play.setOnCompletionListener(new OnCompletionListener()
    {
        public void onCompletion(MediaPlayer play)
        {
            play.release();
        }
    }
    );
}
public void stopPlayback()
{
    play.stop();
    play.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.main, menu);
    return true;
}

}

This is my log cat error

  • 03-11 05:13:04.803: E/AndroidRuntime(814): FATAL EXCEPTION: main
  • 03-11 05:13:04.803: E/AndroidRuntime(814): java.lang.NullPointerException
  • 03-11 05:13:04.803: E/AndroidRuntime(814): at co.example.projectplay.MainActivity$4.onClick(MainActivity.java:106)
  • 03-11 05:13:04.803: E/AndroidRuntime(814): at android.view.View.performClick(View.java:4204)
  • 03-11 05:13:04.803: E/AndroidRuntime(814): at android.view.View$PerformClick.run(View.java:17355)
  • 03-11 05:13:04.803: E/AndroidRuntime(814): at android.os.Handler.handleCallback(Handler.java:725)
  • 03-11 05:13:04.803: E/AndroidRuntime(814): at android.os.Handler.dispatchMessage(Handler.java:92)
  • 03-11 05:13:04.803: E/AndroidRuntime(814): at android.os.Looper.loop(Looper.java:137)
  • 03-11 05:13:04.803: E/AndroidRuntime(814): at android.app.ActivityThread.main(ActivityThread.java:5041)
  • 03-11 05:13:04.803: E/AndroidRuntime(814): at java.lang.reflect.Method.invokeNative(Native Method)
  • 03-11 05:13:04.803: E/AndroidRuntime(814): at java.lang.reflect.Method.invoke(Method.java:511)
  • 03-11 05:13:04.803: E/AndroidRuntime(814): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
  • 03-11 05:13:04.803: E/AndroidRuntime(814): at com.androi03-11 05:13:04.803: E/AndroidRuntime(814): at dalvik.system.NativeStart.main(Native Method)d.internal.os.ZygoteInit.main(ZygoteInit.java:560)
  • 03-11 05:13:04.803: E/AndroidRuntime(814): at dalvik.system.NativeStart.main(Native Method)
  • 03-11 05:13:08.062: I/Process(814): Sending signal. PID: 814 SIG: 9
Était-ce utile?

La solution

Your code doesn't show an assignment to txtButton. It probably should look like this:

txtButton=(TextView)findViewById(R.id.txtButton);

before you use it for the first time.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top