Android: невозможно запустить действие. ComponentInfo: java.lang.NullPointerException

Я пытаюсь запустить панель поиска во время записи звука.

Вот мой код

public class ServerAudioSendActivity extends Activity {

private Button buttonPlayStop;
private MediaPlayer mediaPlayer;
private SeekBar seekBar;

private final Handler handler = new Handler();

// Here i override onCreate method.
//
// setContentView() method set the layout that you will see then
// the application will starts
//
// initViews() method i create to init views components.
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    initViews();

}

// This method set the setOnClickListener and method for it (buttonClick())
private void initViews() {
    buttonPlayStop = (Button) findViewById(R.id.ButtonPlayStop);
    buttonPlayStop.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            buttonClick();
        }
    });

    // mediaPlayer = MediaPlayer.create(this, R.raw.sound41772);

    seekBar = (SeekBar) findViewById(R.id.SeekBar01);
    seekBar.setMax(mediaPlayer.getDuration());
    seekBar.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            seekChange(v);
            return false;
        }
    });

}

public void startPlayProgressUpdater() {
    seekBar.setProgress(mediaPlayer.getCurrentPosition());

    if (mediaPlayer.isPlaying()) {
        Runnable notification = new Runnable() {
            public void run() {
                startPlayProgressUpdater();
            }
        };
        handler.postDelayed(notification, 1000);
    } else {
        mediaPlayer.pause();
        buttonPlayStop.setText(getString(R.string.play_str));
        seekBar.setProgress(0);
    }
}

// This is event handler thumb moving event
private void seekChange(View v) {
    if (mediaPlayer.isPlaying()) {
        SeekBar sb = (SeekBar) v;
        mediaPlayer.seekTo(sb.getProgress());
    }
}

// This is event handler for buttonClick event
private void buttonClick() {
    if (buttonPlayStop.getText() == getString(R.string.play_str)) {
        buttonPlayStop.setText(getString(R.string.pause_str));
        try {
            mediaPlayer.start();
            startPlayProgressUpdater();
        } catch (IllegalStateException e) {
            mediaPlayer.pause();
        }
    } else {
        buttonPlayStop.setText(getString(R.string.play_str));
        mediaPlayer.pause();
    }
}
 }

Давать эту ошибку в logcat

06-06 17:57:33.522: W/dalvikvm(1068): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
06-06 17:57:33.542: E/AndroidRuntime(1068): FATAL EXCEPTION: main
06-06 17:57:33.542: E/AndroidRuntime(1068): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sendaudiotoserver/com.sendaudiotoserver.ServerAudioSendActivity}: java.lang.NullPointerException
06-06 17:57:33.542: E/AndroidRuntime(1068):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
06-06 17:57:33.542: E/AndroidRuntime(1068):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
 06-06 17:57:33.542: E/AndroidRuntime(1068):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
 06-06 17:57:33.542: E/AndroidRuntime(1068):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
 06-06 17:57:33.542: E/AndroidRuntime(1068):    at android.os.Handler.dispatchMessage(Handler.java:99)
 06-06 17:57:33.542: E/AndroidRuntime(1068):    at android.os.Looper.loop(Looper.java:137)
 06-06 17:57:33.542: E/AndroidRuntime(1068):    at android.app.ActivityThread.main(ActivityThread.java:4424)
 06-06 17:57:33.542: E/AndroidRuntime(1068):    at java.lang.reflect.Method.invokeNative(Native Method)
 0 6-06 17:57:33.542: E/AndroidRuntime(1068):   at java.lang.reflect.Method.invoke(Method.java:511)
 06-06 17:57:33.542: E/AndroidRuntime(1068):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
 06-06 17:57:33.542: E/AndroidRuntime(1068):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
 0 6-06 17:57:33.542: E/AndroidRuntime(1068):   at dalvik.system.NativeStart.main(Native  Method)
 06-06 17:57:33.542: E/AndroidRuntime(1068): Caused by: java.lang.NullPointerException
 06-06 17:57:33.542: E/AndroidRuntime(1068):    at com.sendaudiotoserver.ServerAudioSendActivity.startPlayProgressUpdater(ServerAudioSendActivity.java:60)
 06-06 17:57:33.542: E/AndroidRuntime(1068):    at com.sendaudiotoserver.ServerAudioSendActivity.onCreate(ServerAudioSendActivity.java:32)
 06-06 17:57:33.542: E/AndroidRuntime(1068):    at android.app.Activity.performCreate(Activity.java:4465)
 06-06 17:57:33.542: E/AndroidRuntime(1068):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
 06-06 17:57:33.542: E/AndroidRuntime(1068):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
 06-06 17:57:33.542: E/AndroidRuntime(1068):    ... 11 more

пожалуйста, дайте мне знать, где я иду не так..

Большое спасибо Рахул

2 ответа

Решение

Вы прокомментировали эту строку:

// mediaPlayer = MediaPlayer.create(this, R.raw.sound41772);

Следовательно, вы получаете NullPointerException по адресу:

mediaPlayer.getDuration()

Вызовите этот метод еще до buttonPlayStop = (Button) findViewById(R.id.ButtonPlayStop);Ваша панель поиска не инициализирована и, следовательно, NPE происходит на seekBar.setProgress(mediaPlayer.getCurrentPosition());,

Другие вопросы по тегам