线程退出与未捕获的异常(组= 0x4001b188)。空指针异常

时间:2012-03-06 18:35:00

标签: android eclipse exception nullpointerexception

我遇到以下错误:线程退出未捕获的异常(group = 0x4001b188)。空指针异常 这是由以下代码引起的:     包org.apache.android.media;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnVideoSizeChangedListener;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;


public class VideoViewDemo extends Activity implements
    OnBufferingUpdateListener, OnCompletionListener,
    OnPreparedListener, OnVideoSizeChangedListener, SurfaceHolder.Callback {

private static final String TAG = "MediaPlayerDemo";
private int mVideoWidth;
private int mVideoHeight;
private MediaPlayer mMediaPlayer;
private SurfaceView mPreview;
private SurfaceHolder holder;
private String path;
private Bundle extras;
private static final String MEDIA = "media";
private static final int LOCAL_AUDIO = 1;
private static final int STREAM_AUDIO = 2;
private static final int RESOURCES_AUDIO = 3;
private static final int LOCAL_VIDEO = 4;
private static final int STREAM_VIDEO = 5;
private boolean mIsVideoSizeKnown = false;
private boolean mIsVideoReadyToBePlayed = false;

/**
 * 
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    mPreview = (SurfaceView) findViewById(R.id.surface);
    holder = mPreview.getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    extras = getIntent().getExtras();

}

private void playVideo(Integer Media) {
   doCleanUp();
    try {
      path = "http://pwserver/barbapappa.mp4";

        // Create a new media player and set the listeners
        mMediaPlayer = new MediaPlayer();
       mMediaPlayer.setDataSource(path);
        mMediaPlayer.setDisplay(holder);
        mMediaPlayer.prepare();
        mMediaPlayer.setOnBufferingUpdateListener(this);
        mMediaPlayer.setOnCompletionListener(this);
        mMediaPlayer.setOnPreparedListener(this);
        mMediaPlayer.setOnVideoSizeChangedListener(this);
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
   } catch (Exception e) {
        Log.e(TAG, "PdM error: " + e.getMessage(), e);
    }
}

public void onBufferingUpdate(MediaPlayer arg0, int percent) {
    Log.d(TAG, "onBufferingUpdate percent:" + percent);

}

public void onCompletion(MediaPlayer arg0) {
    Log.d(TAG, "onCompletion called");
}

public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
    Log.v(TAG, "onVideoSizeChanged called");
    if (width == 0 || height == 0) {
        Log.e(TAG, "invalid video width(" + width + ") or height(" + height + ")");
        return;
    }
    mIsVideoSizeKnown = true;
    mVideoWidth = width;
    mVideoHeight = height;
    if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
        startVideoPlayback();
    }
}

public void onPrepared(MediaPlayer mediaplayer) {
    Log.d(TAG, "onPrepared called");
    mIsVideoReadyToBePlayed = true;
    if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
        startVideoPlayback();
    }
}

public void surfaceChanged(SurfaceHolder surfaceholder, int i, int j, int k) {
    Log.d(TAG, "surfaceChanged called");

}

public void surfaceDestroyed(SurfaceHolder surfaceholder) {
    Log.d(TAG, "surfaceDestroyed called");
}


public void surfaceCreated(SurfaceHolder holder) {
    Log.d(TAG, "surfaceCreated called");
    playVideo(extras.getInt(MEDIA));
}

@Override
protected void onPause() {
    super.onPause();
    releaseMediaPlayer();
    doCleanUp();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    releaseMediaPlayer();
    doCleanUp();
}

private void releaseMediaPlayer() {
    if (mMediaPlayer != null) {
        mMediaPlayer.release();
        mMediaPlayer = null;
    }
}

private void doCleanUp() {
    mVideoWidth = 0;
    mVideoHeight = 0;
    mIsVideoReadyToBePlayed = false;
    mIsVideoSizeKnown = false;
}

private void startVideoPlayback() {
    Log.v(TAG, "startVideoPlayback");
    holder.setFixedSize(mVideoWidth, mVideoHeight);
    mMediaPlayer.start();

}

有人可以检查一下,希望能告诉我哪里出错了。我已经调试了几个小时但没有结果。每一个帮助都赞赏!


@ Dr.Dredel:我不知道该怎么做

特此提供logcat内容并感谢您的帮助:

 D/AndroidRuntime(496): Shutting down VM
 W/dalvikvm(496): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
 E/AndroidRuntime(496): Uncaught handler: thread main exiting due to uncaught exception
 E/AndroidRuntime(496): java.lang.NullPointerException
 E/AndroidRuntime(496):     at org.apache.android.media.VideoViewDemo.surfaceCreated(VideoViewDemo.java:133)
 E/AndroidRuntime(496):     at android.view.SurfaceView.updateWindow(SurfaceView.java:454)
 E/AndroidRuntime(496):     at android.view.SurfaceView.dispatchDraw(SurfaceView.java:287)
 E/AndroidRuntime(496):     at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
 E/AndroidRuntime(496):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
 E/AndroidRuntime(496):     at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
 E/AndroidRuntime(496):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
 E/AndroidRuntime(496):     at android.view.View.draw(View.java:6538)
 E/AndroidRuntime(496):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
 E/AndroidRuntime(496):     at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
 E/AndroidRuntime(496):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
 E/AndroidRuntime(496):     at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
 E/AndroidRuntime(496):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
 E/AndroidRuntime(496):     at android.view.View.draw(View.java:6538)
 E/AndroidRuntime(496):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
 E/AndroidRuntime(496):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1830)
 E/AndroidRuntime(496):     at android.view.ViewRoot.draw(ViewRoot.java:1349)
 E/AndroidRuntime(496):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1114)
 E/AndroidRuntime(496):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
 E/AndroidRuntime(496):     at android.os.Handler.dispatchMessage(Handler.java:99)
 E/AndroidRuntime(496):     at android.os.Looper.loop(Looper.java:123)
 E/AndroidRuntime(496):     at android.app.ActivityThread.main(ActivityThread.java:4363)
 E/AndroidRuntime(496):     at java.lang.reflect.Method.invokeNative(Native Method)
 E/AndroidRuntime(496):     at java.lang.reflect.Method.invoke(Method.java:521)
 E/AndroidRuntime(496):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
 E/AndroidRuntime(496):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
 E/AndroidRuntime(496):     at dalvik.system.NativeStart.main(Native Method)
 I/dalvikvm(496): threadid=7: reacting to signal 3
 E/dalvikvm(496): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
 D/MediaPlayerDemo(521): surfaceCreated called
 D/AndroidRuntime(521): Shutting down VM
 W/dalvikvm(521): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
 E/AndroidRuntime(521): Uncaught handler: thread main exiting due to uncaught exception
 E/AndroidRuntime(521): java.lang.NullPointerException
 E/AndroidRuntime(521):     at org.apache.android.media.VideoViewDemo.surfaceCreated(VideoViewDemo.java:134)
 E/AndroidRuntime(521):     at android.view.SurfaceView.updateWindow(SurfaceView.java:454)
 E/AndroidRuntime(521):     at android.view.SurfaceView.dispatchDraw(SurfaceView.java:287)
 E/AndroidRuntime(521):     at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
 E/AndroidRuntime(521):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
 E/AndroidRuntime(521):     at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
 E/AndroidRuntime(521):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
 E/AndroidRuntime(521):     at android.view.View.draw(View.java:6538)
 E/AndroidRuntime(521):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
 E/AndroidRuntime(521):     at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
 E/AndroidRuntime(521):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
 E/AndroidRuntime(521):     at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
 E/AndroidRuntime(521):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
 E/AndroidRuntime(521):     at android.view.View.draw(View.java:6538)
 E/AndroidRuntime(521):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
 E/AndroidRuntime(521):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1830)
 E/AndroidRuntime(521):     at android.view.ViewRoot.draw(ViewRoot.java:1349)
 E/AndroidRuntime(521):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1114)
 E/AndroidRuntime(521):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
 E/AndroidRuntime(521):     at android.os.Handler.dispatchMessage(Handler.java:99)
 E/AndroidRuntime(521):     at android.os.Looper.loop(Looper.java:123)
 E/AndroidRuntime(521):     at android.app.ActivityThread.main(ActivityThread.java:4363)
 E/AndroidRuntime(521):     at java.lang.reflect.Method.invokeNative(Native Method)
 E/AndroidRuntime(521):     at java.lang.reflect.Method.invoke(Method.java:521)
 E/AndroidRuntime(521):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
E/AndroidRuntime(521):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
E/AndroidRuntime(521):  at dalvik.system.NativeStart.main(Native Method)
I/dalvikvm(521): threadid=7: reacting to signal 3
E/dalvikvm(521): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
I/Process(521): Sending signal. PID: 521 SIG: 9
D/MediaPlayerDemo(547): surfaceCreated called
D/AndroidRuntime(547): Shutting down VM
W/dalvikvm(547): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
E/AndroidRuntime(547): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime(547): java.lang.NullPointerException
E/AndroidRuntime(547):  at org.apache.android.media.VideoViewDemo.surfaceCreated(VideoViewDemo.java:134)
E/AndroidRuntime(547):  at android.view.SurfaceView.updateWindow(SurfaceView.java:454)
E/AndroidRuntime(547):  at android.view.SurfaceView.dispatchDraw(SurfaceView.java:287)
E/AndroidRuntime(547):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
E/AndroidRuntime(547):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
E/AndroidRuntime(547):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
E/AndroidRuntime(547):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
E/AndroidRuntime(547):  at android.view.View.draw(View.java:6538)
E/AndroidRuntime(547):  at android.widget.FrameLayout.draw(FrameLayout.java:352)
E/AndroidRuntime(547):  at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
E/AndroidRuntime(547):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
E/AndroidRuntime(547):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
E/AndroidRuntime(547):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
E/AndroidRuntime(547):  at android.view.View.draw(View.java:6538)
E/AndroidRuntime(547):  at android.widget.FrameLayout.draw(FrameLayout.java:352)
E/AndroidRuntime(547):  at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1830)
E/AndroidRuntime(547):  at android.view.ViewRoot.draw(ViewRoot.java:1349)
E/AndroidRuntime(547):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1114)
E/AndroidRuntime(547):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
E/AndroidRuntime(547):  at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(547):  at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(547):  at android.app.ActivityThread.main(ActivityThread.java:4363)
E/AndroidRuntime(547):  at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(547):  at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(547):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
E/AndroidRuntime(547):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
E/AndroidRuntime(547):  at dalvik.system.NativeStart.main(Native Method)
I/dalvikvm(547): threadid=7: reacting to signal 3
E/dalvikvm(547): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
I/Process(547): Sending signal. PID: 547 SIG: 9

1 个答案:

答案 0 :(得分:0)

我今天面临同样的问题。

我需要做的就是删除playVideo方法的参数,并通过删除“extras.getInt()”来更新surfaceCreated方法。

通过这样做,我可以播放视频,但我仍然不知道附加内容是用于...

希望这有帮助