0

我制作了一个使用媒体播放器从在线资源播放媒体的应用程序,它可以在运行 android 2.3.7 姜饼的手机上运行,​​但是当我尝试在另一部运行 android 4.0 的手机上运行它时,它会在播放按钮时崩溃按下。我到处搜索,只是找不到问题的解决方案。任何帮助将不胜感激。我的代码和日志结果如下:

import android.annotation.SuppressLint;
import android.app.Activity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

@SuppressLint({ "UseValueOf", "UseValueOf" })
public class DisplayMessage5 extends Activity
implements Runnable, OnClickListener, OnSeekBarChangeListener {

    private ImageButton play;
    private ImageButton pause;
    private ImageButton stop;
    private MediaPlayer mediaPlayer;
    private SeekBar seekBar;
    private TextView textViewSecs;
    private TextView textViewTotal;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_message5);
        play = (ImageButton)findViewById(R.id.play_button);
        pause = (ImageButton)findViewById(R.id.pause_button);
        stop = (ImageButton)findViewById(R.id.stop_button);
        seekBar = (SeekBar)findViewById(R.id.seekBar1);
        textViewSecs = (TextView)findViewById(R.id.progress_text_secs);
        textViewTotal = (TextView)findViewById(R.id.progress_text_mins);
        seekBar.setOnSeekBarChangeListener(this);
        play.setOnClickListener(this);
        pause.setOnClickListener(this);
        stop.setOnClickListener(this);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_display_message5, menu);
        return true;
    }


    @Override
    public void run() {
        int currentPosition= 0;
        int total = mediaPlayer.getDuration();
        while (mediaPlayer!=null && currentPosition<total) {
            try {
                Thread.sleep(1000);
                currentPosition= mediaPlayer.getCurrentPosition();
            } catch (InterruptedException e) {
                return;
            } catch (Exception e) {
                return;
            }            
            seekBar.setProgress(currentPosition);
        }

    }

    public void onClick(View v) {
        if (v.equals(play)) {
            if (mediaPlayer != null && mediaPlayer.isPlaying()) return;
            if(seekBar.getProgress() > 0) {
                try {
                    mediaPlayer.prepareAsync();
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                }
                mediaPlayer.start();
                return;
            }
            Uri uri = Uri.parse("myurl");
            mediaPlayer = MediaPlayer.create(this, uri);
            try {
                mediaPlayer.prepareAsync();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            }
            mediaPlayer.start();
            seekBar.setProgress(0);
            seekBar.setMax(mediaPlayer.getDuration());
            new Thread(this).start();
        }

        if (pause.isPressed()) {
            mediaPlayer.pause();
        } 

        if(stop.isPressed()){
            mediaPlayer.stop();
            mediaPlayer.reset();
        }

    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
        int progressSecs = (progress / 1000) % 60;
        int progressMins = (progress / 1000) / 60;
        int totalTime = mediaPlayer.getDuration();
        int totalSecs = (totalTime / 1000) % 60;
        int totalMins = ((totalTime / 1000) / 60) % 60;
        int totalHours = totalTime / 1000 / 60 / 60;
        textViewSecs.setText(String.valueOf(String.format("%02d", progressMins)) + ":" + String.valueOf(String.format("%02d", progressSecs)));
        textViewTotal.setText(String.valueOf(String.format("%02d", totalHours)) + ":" + String.valueOf(String.format("%02d", totalMins)) + ":" + String.valueOf(String.format("%02d", totalSecs)));
        if(fromUser){
            mediaPlayer.seekTo(progress);
        }



    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekbar) {


    }



}

日志

10-10 20:15:47.156: E/AndroidRuntime(15124): FATAL EXCEPTION: main
10-10 20:15:47.156: E/AndroidRuntime(15124): java.lang.NullPointerException
10-10 20:15:47.156: E/AndroidRuntime(15124):    at com.marnis.na.shares.DisplayMessage5.onClick(DisplayMessage5.java:86)
10-10 20:15:47.156: E/AndroidRuntime(15124):    at android.view.View.performClick(View.java:3511)
10-10 20:15:47.156: E/AndroidRuntime(15124):    at android.view.View$PerformClick.run(View.java:14105)
10-10 20:15:47.156: E/AndroidRuntime(15124):    at android.os.Handler.handleCallback(Handler.java:605)
10-10 20:15:47.156: E/AndroidRuntime(15124):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-10 20:15:47.156: E/AndroidRuntime(15124):    at android.os.Looper.loop(Looper.java:137)
10-10 20:15:47.156: E/AndroidRuntime(15124):    at android.app.ActivityThread.main(ActivityThread.java:4613)
10-10 20:15:47.156: E/AndroidRuntime(15124):    at java.lang.reflect.Method.invokeNative(Native Method)
10-10 20:15:47.156: E/AndroidRuntime(15124):    at java.lang.reflect.Method.invoke(Method.java:511)
10-10 20:15:47.156: E/AndroidRuntime(15124):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
10-10 20:15:47.156: E/AndroidRuntime(15124):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
10-10 20:15:47.156: E/AndroidRuntime(15124):    at dalvik.system.NativeStart.main(Native Method)
10-10 20:15:48.119: I/dalvikvm(15124): threadid=3: reacting to signal 3
10-10 20:15:48.176: I/dalvikvm(15124): Wrote stack traces to '/data/anr/traces.txt'
10-10 20:15:48.686: I/dalvikvm(15124): threadid=3: reacting to signal 3
10-10 20:15:48.686: I/dalvikvm(15124): Wrote stack traces to '/data/anr/traces.txt'
10-10 20:15:49.186: I/dalvikvm(15124): threadid=3: reacting to signal 3
10-10 20:15:49.186: I/dalvikvm(15124): Wrote stack traces to '/data/anr/traces.txt'
10-10 20:15:49.686: I/dalvikvm(15124): threadid=3: reacting to signal 3
10-10 20:15:49.686: I/dalvikvm(15124): Wrote stack traces to '/data/anr/traces.txt'
10-10 20:15:50.186: I/dalvikvm(15124): threadid=3: reacting to signal 3
10-10 20:15:50.186: I/dalvikvm(15124): Wrote stack traces to '/data/anr/traces.txt'
10-10 20:15:50.686: I/dalvikvm(15124): threadid=3: reacting to signal 3
10-10 20:15:50.686: I/dalvikvm(15124): Wrote stack traces to '/data/anr/traces.txt'
10-10 20:15:51.196: I/dalvikvm(15124): threadid=3: reacting to signal 3
10-10 20:15:51.196: I/dalvikvm(15124): Wrote stack traces to '/data/anr/traces.txt'
10-10 20:15:51.696: I/dalvikvm(15124): threadid=3: reacting to signal 3
10-10 20:15:51.696: I/dalvikvm(15124): Wrote stack traces to '/data/anr/traces.txt'
10-10 20:15:52.196: I/dalvikvm(15124): threadid=3: reacting to signal 3
10-10 20:15:52.196: I/dalvikvm(15124): Wrote stack traces to '/data/anr/traces.txt'
10-10 20:15:52.696: I/dalvikvm(15124): threadid=3: reacting to signal 3
10-10 20:15:52.696: I/dalvikvm(15124): Wrote stack traces to '/data/anr/traces.txt'
10-10 20:15:53.198: I/dalvikvm(15124): threadid=3: reacting to signal 3
10-10 20:15:53.198: I/dalvikvm(15124): Wrote stack traces to '/data/anr/traces.txt'
10-10 20:15:53.706: I/dalvikvm(15124): threadid=3: reacting to signal 3
10-10 20:15:53.706: I/dalvikvm(15124): Wrote stack traces to '/data/anr/traces.txt'
10-10 20:15:54.206: I/dalvikvm(15124): threadid=3: reacting to signal 3
10-10 20:15:54.206: I/dalvikvm(15124): Wrote stack traces to '/data/anr/traces.txt'
10-10 20:15:54.706: I/dalvikvm(15124): threadid=3: reacting to signal 3
10-10 20:15:54.706: I/dalvikvm(15124): Wrote stack traces to '/data/anr/traces.txt'
10-10 20:15:55.206: I/dalvikvm(15124): threadid=3: reacting to signal 3
10-10 20:15:55.216: I/dalvikvm(15124): Wrote stack traces to '/data/anr/traces.txt'
10-10 20:15:55.706: I/dalvikvm(15124): threadid=3: reacting to signal 3
10-10 20:15:55.716: I/dalvikvm(15124): Wrote stack traces to '/data/anr/traces.txt'
10-10 20:15:56.207: I/dalvikvm(15124): threadid=3: reacting to signal 3
10-10 20:15:56.216: I/dalvikvm(15124): Wrote stack traces to '/data/anr/traces.txt'
10-10 20:15:56.706: I/dalvikvm(15124): threadid=3: reacting to signal 3
10-10 20:15:56.706: I/dalvikvm(15124): Wrote stack traces to '/data/anr/traces.txt'
10-10 20:15:57.206: I/dalvikvm(15124): threadid=3: reacting to signal 3
10-10 20:15:57.206: I/dalvikvm(15124): Wrote stack traces to '/data/anr/traces.txt'
10-10 20:15:57.706: I/dalvikvm(15124): threadid=3: reacting to signal 3
10-10 20:15:57.706: I/dalvikvm(15124): Wrote stack traces to '/data/anr/traces.txt'
10-10 20:16:47.626: I/dalvikvm(15124): threadid=3: reacting to signal 3
10-10 20:16:47.666: I/dalvikvm(15124): Wrote stack traces to '/data/anr/traces.txt'
10-10 20:20:47.226: I/Process(15124): Sending signal. PID: 15124 SIG: 9
10-10 20:21:50.226: V/ActivityThread(15667): com.marnis.na.shares white listed for hwui
10-10 20:21:50.536: D/libEGL(15667): loaded /system/lib/egl/libGLES_android.so
10-10 20:21:50.576: D/libEGL(15667): loaded /system/lib/egl/libEGL_tegra.so
10-10 20:21:50.626: I/dalvikvm(15667): threadid=3: reacting to signal 3
10-10 20:21:50.657: I/dalvikvm(15667): Wrote stack traces to '/data/anr/traces.txt'
10-10 20:21:50.746: D/libEGL(15667): loaded /system/lib/egl/libGLESv1_CM_tegra.so
10-10 20:21:50.766: D/libEGL(15667): loaded /system/lib/egl/libGLESv2_tegra.so
10-10 20:21:50.796: D/OpenGLRenderer(15667): Enabling debug mode 0
10-10 20:21:53.796: D/OpenGLRenderer(15667): Flushing caches (mode 0)
10-10 20:21:55.966: D/MediaPlayer(15667): Couldn't open file on client side, trying server side
10-10 20:21:55.996: E/MediaPlayer(15667): Unable to to create media player
10-10 20:21:56.026: D/MediaPlayer(15667): create failed:
10-10 20:21:56.026: D/MediaPlayer(15667): java.io.IOException: setDataSource failed.: status=0x80000000
10-10 20:21:56.026: D/MediaPlayer(15667):   at android.media.MediaPlayer._setDataSource(Native Method)
10-10 20:21:56.026: D/MediaPlayer(15667):   at android.media.MediaPlayer.setDataSource(MediaPlayer.java:844)
10-10 20:21:56.026: D/MediaPlayer(15667):   at android.media.MediaPlayer.setDataSource(MediaPlayer.java:806)
10-10 20:21:56.026: D/MediaPlayer(15667):   at android.media.MediaPlayer.setDataSource(MediaPlayer.java:761)
10-10 20:21:56.026: D/MediaPlayer(15667):   at android.media.MediaPlayer.create(MediaPlayer.java:695)
10-10 20:21:56.026: D/MediaPlayer(15667):   at android.media.MediaPlayer.create(MediaPlayer.java:676)
10-10 20:21:56.026: D/MediaPlayer(15667):   at com.marnis.na.shares.DisplayMessage4.onClick(DisplayMessage4.java:77)
10-10 20:21:56.026: D/MediaPlayer(15667):   at android.view.View.performClick(View.java:3511)
10-10 20:21:56.026: D/MediaPlayer(15667):   at android.view.View$PerformClick.run(View.java:14105)
10-10 20:21:56.026: D/MediaPlayer(15667):   at android.os.Handler.handleCallback(Handler.java:605)
10-10 20:21:56.026: D/MediaPlayer(15667):   at android.os.Handler.dispatchMessage(Handler.java:92)
10-10 20:21:56.026: D/MediaPlayer(15667):   at android.os.Looper.loop(Looper.java:137)
10-10 20:21:56.026: D/MediaPlayer(15667):   at android.app.ActivityThread.main(ActivityThread.java:4613)
10-10 20:21:56.026: D/MediaPlayer(15667):   at java.lang.reflect.Method.invokeNative(Native Method)
10-10 20:21:56.026: D/MediaPlayer(15667):   at java.lang.reflect.Method.invoke(Method.java:511)
10-10 20:21:56.026: D/MediaPlayer(15667):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
10-10 20:21:56.026: D/MediaPlayer(15667):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
10-10 20:21:56.026: D/MediaPlayer(15667):   at dalvik.system.NativeStart.main(Native Method)
10-10 20:21:56.026: D/AndroidRuntime(15667): Shutting down VM
10-10 20:21:56.026: W/dalvikvm(15667): threadid=1: thread exiting with uncaught exception (group=0x40a2f3f8)
10-10 20:21:56.026: E/AndroidRuntime(15667): FATAL EXCEPTION: main
10-10 20:21:56.026: E/AndroidRuntime(15667): java.lang.NullPointerException
10-10 20:21:56.026: E/AndroidRuntime(15667):    at com.marnis.na.shares.DisplayMessage4.onClick(DisplayMessage4.java:79)
10-10 20:21:56.026: E/AndroidRuntime(15667):    at android.view.View.performClick(View.java:3511)
10-10 20:21:56.026: E/AndroidRuntime(15667):    at android.view.View$PerformClick.run(View.java:14105)
10-10 20:21:56.026: E/AndroidRuntime(15667):    at android.os.Handler.handleCallback(Handler.java:605)
10-10 20:21:56.026: E/AndroidRuntime(15667):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-10 20:21:56.026: E/AndroidRuntime(15667):    at android.os.Looper.loop(Looper.java:137)
10-10 20:21:56.026: E/AndroidRuntime(15667):    at android.app.ActivityThread.main(ActivityThread.java:4613)
10-10 20:21:56.026: E/AndroidRuntime(15667):    at java.lang.reflect.Method.invokeNative(Native Method)
10-10 20:21:56.026: E/AndroidRuntime(15667):    at java.lang.reflect.Method.invoke(Method.java:511)
10-10 20:21:56.026: E/AndroidRuntime(15667):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
10-10 20:21:56.026: E/AndroidRuntime(15667):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
10-10 20:21:56.026: E/AndroidRuntime(15667):    at dalvik.system.NativeStart.main(Native Method)
10-10 20:21:56.556: I/dalvikvm(15667): threadid=3: reacting to signal 3
10-10 20:21:56.566: I/dalvikvm(15667): Wrote stack traces to '/data/anr/traces.txt'
10-10 20:21:57.086: I/dalvikvm(15667): threadid=3: reacting to signal 3
10-10 20:21:57.096: I/dalvikvm(15667): Wrote stack traces to '/data/anr/traces.txt'
10-10 20:21:57.586: I/dalvikvm(15667): threadid=3: reacting to signal 3
10-10 20:21:57.596: I/dalvikvm(15667): Wrote stack traces to '/data/anr/traces.txt'
10-10 20:21:58.096: I/dalvikvm(15667): threadid=3: reacting to signal 3
10-10 20:21:58.106: I/dalvikvm(15667): Wrote stack traces to '/data/anr/traces.txt'
10-10 20:21:58.596: I/dalvikvm(15667): threadid=3: reacting to signal 3
10-10 20:21:58.606: I/dalvikvm(15667): Wrote stack traces to '/data/anr/traces.txt'
10-10 20:21:59.097: I/dalvikvm(15667): threadid=3: reacting to signal 3
10-10 20:21:59.097: I/dalvikvm(15667): Wrote stack traces to '/data/anr/traces.txt'
10-10 20:21:59.596: I/dalvikvm(15667): threadid=3: reacting to signal 3
10-10 20:21:59.596: I/dalvikvm(15667): Wrote stack traces to '/data/anr/traces.txt'
10-10 20:22:00.096: I/dalvikvm(15667): threadid=3: reacting to signal 3
10-10 20:22:00.096: I/dalvikvm(15667): Wrote stack traces to '/data/anr/traces.txt'
10-10 20:22:00.597: I/dalvikvm(15667): threadid=3: reacting to signal 3
10-10 20:22:00.606: I/dalvikvm(15667): Wrote stack traces to '/data/anr/traces.txt'
10-10 20:22:01.096: I/dalvikvm(15667): threadid=3: reacting to signal 3
10-10 20:22:01.096: I/dalvikvm(15667): Wrote stack traces to '/data/anr/traces.txt'
10-10 20:22:01.306: I/Process(15667): Sending signal. PID: 15667 SIG: 9
4

1 回答 1

2
if(seekBar.getProgress() > 0) {
    try {
        mediaPlayer.prepareAsync();
    } 
    catch (IllegalStateException e) {
        e.printStackTrace();
    }
        mediaPlayer.start();
        return;
}

发生错误是因为您尝试访问空对象 (mediaPlayer) 的方法。它可能发生在 IF 块中,因为您的 seekBar 的默认值不是 0。在这种情况下,您应该在 onCreate 方法中初始化 mediaPlayer,而不是在 onClick 中。

于 2012-10-11T02:28:08.550 回答