有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

运行Android活动时出现java NullPointer异常

我不明白为什么我的应用程序一直崩溃。google analytics一直说这是我应用程序中最常见的崩溃,我无法修复它。我已经检查了代码多次,不知道如何解决这个问题

以下是错误:

java.lang.IllegalStateException: Could not execute method of the activity
at 安卓.view.View$1.onClick(View.java:3969)
at 安卓.view.View.performClick(View.java:4640)
at 安卓.view.View$PerformClick.run(View.java:19421)
at 安卓.os.Handler.handleCallback(Handler.java:733)
at 安卓.os.Handler.dispatchMessage(Handler.java:95)
at 安卓.os.Looper.loop(Looper.java:136)
at 安卓.app.ActivityThread.main(ActivityThread.java:5579)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at 安卓.view.View$1.onClick(View.java:3964)
... 11 more
Caused by: java.lang.NullPointerException
at com.soloinc.meip.RecordRap.onClick(RecordRap.java:371)
... 14 more

这是录音带

@Override
protected void onCreate(Bundle savedInstanceState)
{

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


    //instrument name from previous activity
    instrument_file_name = getIntent().getExtras().getString("instrument_file_name");
    instrument_title = getIntent().getExtras().getString("instrument_title");
    TextView tv = (TextView) findViewById(R.id.instrument_title);
    tv.setText(instrument_title);    
    //getting buffer size for our audio specification
    bufferSize = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE,RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING);



    //setting listeners for seek bars
    seekBar1 = (SeekBar)(findViewById(R.id.seekBar1));
    seekBar1.setOnSeekBarChangeListener(osbcl);
    seekBar2 = (SeekBar)(findViewById(R.id.seekBar2));
    seekBar2.setOnSeekBarChangeListener(osbcl);


 // Get the AudioManager
    AudioManager audioManager =
    (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
    // Set the volume of played media to maximum.
    audioManager.setStreamVolume (
    AudioManager.STREAM_MUSIC,
    audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC),
    0);


    //saving beats data in List

    if(savedInstanceState == null)
        new SaveInputStreamTask(this).execute();
}



  @Override
  public void onStart() {
    super.onStart();
     // The rest of your onStart() code.
    EasyTracker.getInstance(this).activityStart(this);  // Add this method.
  }

  @Override
  public void onStop() {
    super.onStop();
    // The rest of your onStop() code.
    EasyTracker.getInstance(this).activityStop(this);  // Add this method.
  }

//Listener for seek bars
final SeekBar.OnSeekBarChangeListener osbcl = new SeekBar.OnSeekBarChangeListener() {

    @Override
    public void onStopTrackingTouch(SeekBar arg0) {
        if(arg0.getId() == R.id.seekBar1)
        {
            seekBar1Value = arg0.getProgress();
            Toast.makeText(getApplicationContext(), "Beat Volume:"+seekBar1Value, Toast.LENGTH_SHORT).show();
        }
        else
        {
            seekBar2Value = arg0.getProgress();
            Toast.makeText(getApplicationContext(), "Lyrics Volume:"+seekBar2Value, Toast.LENGTH_SHORT).show();
        }
        simultaneousPlay();//whenever volume value changes play audios again to show change in volume level
    }

    @Override
    public void onStartTrackingTouch(SeekBar arg0) {}

    @Override
    public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {}
};
//initializing mediaplayer for playing beat 
private void initializePlayer()
{

    try
    {
        //int resID=getResources().getIdentifier(instrument_file_name.substring(0,instrument_file_name.length()-4), "raw", getPackageName());
        player = MediaPlayer.create(this,Uri.parse(instrument_file_name));

        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer arg0) {
                isPlayingInstrument = false;

            }
        });
    }
    catch(Exception e)
    {
        System.out.print(e.getMessage());
    }
}
//return file name with current millisecond
private String getFilename()
{
    String filepath = Environment.getExternalStorageDirectory().getPath();
    File file = new File(filepath,MEIP_FOLDER);

    if(!file.exists()){
            file.mkdirs();
    }
    String temp = file.getAbsolutePath() + "/" + mixed_file_name + MEIP_FILE_EXT_WAV;
    recorded_rap_file_name = temp;
    return temp;
}
//return temporary fileName
private String getTempFilename()
{
    String filepath = Environment.getExternalStorageDirectory().getPath();
    File file = new File(filepath,MEIP_FOLDER);

    if(!file.exists()){
            file.mkdirs();
    }

    return (file.getAbsolutePath() + "/" + MEIP_TEMP_FILE);
}
//This function records user voice
private void startRecording()
{
    recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,RECORDER_SAMPLERATE, RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING, bufferSize);
    recorder.startRecording();
    isRecording = true;

    recordingThread = new Thread(new Runnable() {

            @Override
            public void run() 
            {
                    saveLyricsPCMData(recorder);
                    //writePCMDataToFile(getTempFilename(),recorder);
            }
    },"AudioRecorder Thread");

    recordingThread.start();
}
//this function saves recording PCM data to temp file 
private void saveLyricsPCMData(AudioRecord ar)
{
    short data[] = new short[bufferSize];
    lyricsShortList.clear();

    int read = 0;

    while(isRecording)
    {
            read = ar.read(data, 0, bufferSize);               
            if(AudioRecord.ERROR_INVALID_OPERATION != read)
            {
                for(int k = 0; k < read; k++)
                {
                    lyricsShortList.add(data[k]);
                }
            }
    }
}
//this function executes when user stop recording
private void stopRecording()
{
    if(null != recorder)
    {
            isRecording = false;

            recorder.stop();
            recorder.release();

            recorder = null;
            recordingThread = null;
    }

    //writePCMToWaveFile(getTempFilename(),getFilename());
    //deleteTempFile();
}

//this function writes wav file header to out
private void WriteWaveFileHeader(FileOutputStream out, long totalAudioLen,long totalDataLen, long longSampleRate, int channels,long byteRate) throws IOException 
{    
    byte[] header = new byte[44];

    header[0] = 'R';  // RIFF/WAVE header
    header[1] = 'I';
    header[2] = 'F';
    header[3] = 'F';
    header[4] = (byte) (totalAudioLen & 0xff);
    header[5] = (byte) ((totalAudioLen >> 8) & 0xff);
    header[6] = (byte) ((totalAudioLen >> 16) & 0xff);
    header[7] = (byte) ((totalAudioLen >> 24) & 0xff);
    header[8] = 'W';
    header[9] = 'A';
    header[10] = 'V';
    header[11] = 'E';
    header[12] = 'f';  // 'fmt ' chunk
    header[13] = 'm';
    header[14] = 't';
    header[15] = ' ';
    header[16] = 16;  // 4 bytes: size of 'fmt ' chunk
    header[17] = 0;
    header[18] = 0;
    header[19] = 0;
    header[20] = 1;  // format = 1
    header[21] = 0;
    header[22] = (byte) channels;
    header[23] = 0;
    header[24] = (byte) (longSampleRate & 0xff);
    header[25] = (byte) ((longSampleRate >> 8) & 0xff);
    header[26] = (byte) ((longSampleRate >> 16) & 0xff);
    header[27] = (byte) ((longSampleRate >> 24) & 0xff);
    header[28] = (byte) (byteRate & 0xff);
    header[29] = (byte) ((byteRate >> 8) & 0xff);
    header[30] = (byte) ((byteRate >> 16) & 0xff);
    header[31] = (byte) ((byteRate >> 24) & 0xff);
    header[32] = (byte) ((RECORDER_BPP*channels) / 8);  // block align
    header[33] = 0;
    header[34] = RECORDER_BPP;  // bits per sample
    header[35] = 0;
    header[36] = 'd';
    header[37] = 'a';
    header[38] = 't';
    header[39] = 'a';
    header[40] = (byte) (totalDataLen & 0xff);
    header[41] = (byte) ((totalDataLen >> 8) & 0xff);
    header[42] = (byte) ((totalDataLen >> 16) & 0xff);
    header[43] = (byte) ((totalDataLen >> 24) & 0xff);

    out.write(header, 0, 44);
}   

//onClick listeners for all the buttons
@Override
public void onClick(View view) 
{
    final Chronometer myChronometer = (Chronometer)findViewById(R.id.chronometer);

    switch(view.getId())
    {

        case R.id.play_button:
            if(isPlayingInstrument == false)
            {
                if(player == null){initializePlayer();}
                isPlayingInstrument = true;
                player.start();
                myChronometer.setBase(SystemClock.elapsedRealtime());

            }
        break;

        case R.id.pause_button:
line 366            player.pause();
            isPlayingInstrument = false;
        break;

        case R.id.stop_button:
    line 371        player.stop();
            player = null;
            isPlayingInstrument = false;
            play_thread_running = false;

        break;

        case R.id.start_recording_button:
            if(isRecording == false)
            {
                ((ImageButton)findViewById(R.id.start_recording_button)).setImageResource(R.drawable.button_pause);
                startRecording();
                myChronometer.start();

            }
        break;

        case R.id.stop_recording_button:
            stopRecording();
            myChronometer.stop();
            play_thread_running = false;
            ((ImageButton)findViewById(R.id.start_recording_button)).setImageResource(R.drawable.button_record);
        break;

        case R.id.mix_and_play_button:
                simultaneousPlay();

        break;

        case R.id.save_recording_button:
            showDialog();       
        break;

        case R.id.share_button:
            share();
            break;
    }

}

public void share(){
    if( mixed_file_name == null || mixed_file_name.equals(""))
    {
        Toast.makeText(this, "Save Your Song First!", Toast.LENGTH_SHORT).show();
        return;
    }
    Bundle data = new Bundle();
    data.putString("SHAREFILE", getFilename());
    Message msg = mShareHandler.obtainMessage();
    msg.setData(data);
    mShareHandler.sendMessage(msg);
}   

//play beat and lyrics simultaneously
public void simultaneousPlay()
{
    if(play_thread_running == true){play_thread_running = false;}

     try
    {    
         beat_playing_thread = new Thread(new Runnable() {

            @Override
            public void run() {
                play(buildShortArray(beatsShortList),seekBar1Value);    
            }
        });
         lyrics_playing_thread = new Thread(new Runnable() {

                @Override
                public void run() {
                    play(buildShortArray(lyricsShortList),seekBar2Value);   
                }
            });
         beat_playing_thread.start();
         lyrics_playing_thread.start();
    }
    catch(Exception ex)
    {
        ex.printStackTrace();
    }
}


//play an audio file
private void play(final short[] soundData, final int volume)
{
    short[] output = new short[soundData.length];
    // find the max:
    float max = 0;
    for (int i = 44; i < output.length; i++) 
    {
        if (Math.abs((soundData[i])) > max)
        {
            max = Math.abs((soundData[i]));
        }
    }
 // now find the result, with scaling:
    float a,c;
    for (int i = 44; i < output.length; i++) 
    {
        a = (float)(soundData[i]);
        c = Math.round(Short.MAX_VALUE * (a)/ max);

        if (c > Short.MAX_VALUE)
            c = Short.MAX_VALUE;
        if (c < Short.MIN_VALUE)
            c = Short.MIN_VALUE;
        output[i] = (short) c; 
    }
    AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 22050, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize, AudioTrack.MODE_STREAM);

    play_thread_running = true;
    audioTrack.play();
    audioTrack.setStereoVolume((float)(volume/100.0f), (float)(volume/100.0f));
    int bufferSize = 512;
    ShortBuffer sb = ShortBuffer.wrap(output);
    short[] buffer = new short[bufferSize];
    for(int i = 0 ; i < output.length-512 && play_thread_running == true; i = i+512)
    {
        sb.get(buffer, 0, 512);
        audioTrack.write(buffer, 0, 512);
    }
}
//this function mix audios
private byte[] mixSound() throws IOException 
{
    completeStreams(beatsShortList,lyricsShortList);
    short[] output = new short[beatsShortList.size()];

    // find the max:
    float max = 0;
    for (int i = 44; i < output.length; i++) 
    {
        if (Math.abs((beatsShortList.get(i)*(seekBar1Value/100.0f)) + (lyricsShortList.get(i)*(seekBar2Value/100.0f))) > max)
        {
            max = Math.abs((beatsShortList.get(i)*(seekBar1Value/100.0f)) + (lyricsShortList.get(i)*(seekBar2Value/100.0f)));
        }
    }

    // now find the result, with scaling:
    float a, b, c;
    for (int i = 44; i < output.length; i++) {
        a = beatsShortList.get(i)*(seekBar1Value/100.0f);
        b = lyricsShortList.get(i)*(seekBar2Value/100.0f);

        c = Math.round(Short.MAX_VALUE * (a + b)/ max);

        if (c > Short.MAX_VALUE)
            c = Short.MAX_VALUE;
        if (c < Short.MIN_VALUE)
            c = Short.MIN_VALUE;

        output[i] = (short) c; 
    }

    ByteBuffer bb = ByteBuffer.allocate(output.length * 2);
    bb.order(ByteOrder.LITTLE_ENDIAN);
    bb.asShortBuffer().put(output);
    byte[] bytes = bb.array();
    return bytes;
}

//convert inputstream to byte array
public static byte[] getBytesFromInputStream(InputStream is)
{
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    try
    {
        byte[] buffer = new byte[0xFFFF];

        for (int len; (len = is.read(buffer)) != -1;)
            os.write(buffer, 0, len);

        os.flush();

        return os.toByteArray();
    }
    catch (IOException e)
    {
        return null;
    }
}

public void saveInputStream(InputStream is) throws IOException
{
    int n = 0;
    DataInputStream in1;
    in1 = new DataInputStream(is);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    try 
    {

        while ((n = in1.read()) != -1) 
        {
            bos.write(n);
        }
    } 
    catch (IOException e) 
    {   
        e.printStackTrace();
    }

    ByteBuffer bb = ByteBuffer.wrap(bos.toByteArray());
    bb.order(ByteOrder.LITTLE_ENDIAN);
    ShortBuffer sb = bb.asShortBuffer();

    for (int i = 0; i < sb.capacity(); i++) {
        beatsShortList.add(sb.get(i));
    }
}
//add zeros to shorter audio        
public void completeStreams(List<Short> l1,List<Short> l2)
{
    if(l1.size() > l2.size())
    {
        while(l1.size() != l2.size())
        {
            l2.add((short)0);
        }
    }
    if(l2.size() > l1.size())
    {
        while(l1.size() != l2.size())
        {
            l1.add((short)0);
        }
    }
}
//converts short list to short array
public short[] buildShortArray(List<Short> list)
{
    short[] arr = new short[list.size()];
    for(int i = 0; i < list.size(); i++)
    {
        arr[i] = list.get(i);
    }
    return arr;
}
//overriding back button functionality
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
        Log.d(this.getClass().getName(), "back button pressed");
        if(player != null)
            player.stop();
        play_thread_running = false;
        //beat_playing_thread.stop();
        //lyrics_playing_thread.stop();
        return super.onKeyDown(keyCode, event);
    }
    return true;
}

public void showDialog()
{
    AlertDialog.Builder alert = new AlertDialog.Builder(this);

    alert.setTitle("What's Your Song Name!");
    alert.setMessage("Song Name");

    // Set an EditText view to get user input 
    final EditText input = new EditText(this);
    alert.setView(input);

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {

      mixed_file_name = input.getText().toString();
        new SaveFileTask(RecordRap.this).execute();

          //progress = ProgressDialog.show(getApplicationContext(), "", "Mixing and Saving...");
          }
        });

        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int whichButton) {
            // Canceled.
          }
        });

共 (1) 个答案

  1. # 1 楼答案

    这里有一个小的重新分解,至少可以解决NPE问题的一部分。我不知道这在功能上是否正确,因为我没有查看您的整个应用程序,但它应该为您指出正确的方向。对于播放逻辑,我假设有时initializePlayer()方法无法根据错误报告将player设置为非空值

        case R.id.play_button:
            if(isPlayingInstrument == false) {
                if(player == null)
                    initializePlayer();
    
                if(player != null) {
                    isPlayingInstrument = true;
                    player.start();
                    myChronometer.setBase(SystemClock.elapsedRealtime());
                }
            }
        break;
    
        case R.id.pause_button:
            if(player != null) {
                player.pause();
                isPlayingInstrument = false;
            }
            break;
    
        case R.id.stop_button:
            if(player != null) {
                player.stop();
                player = null;
                isPlayingInstrument = false;
                play_thread_running = false;
            }
            break;