2

私の Android プロジェクトでは、ビデオの再生に SurfaceView を使用していますが、findViewById は SurfaceView に対して null を返します。

これは私のコードです:

MultipleVideoPlayFragmentActivity.java

public class MultipleVideoPlayFragmentActivity extends Activity {

    private static final String TAG = "MediaPlayer";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.multi_videos_activity_layout);
    }



}

VideoFragment.java

public class VideoFragment extends Fragment implements
        OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener,
         OnVideoSizeChangedListener, SurfaceHolder.Callback {

    private static final String TAG = "MediaPlayer";

    private MediaPlayer mMediaPlayer;
    private SurfaceView mSurfaceView;
    private SurfaceHolder mSurfaceHolder;
    private boolean mSizeKnown;
    private boolean mVideoReady;
    View v;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.multi_videos_fragment_layout,
            container, false);

         return v;
     }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mSurfaceView = (SurfaceView) v.findViewById(
                R.id.video_surfaceview);

        mSurfaceHolder = mSurfaceView.getHolder();
        mSurfaceHolder.addCallback(this);
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

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

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

     public void onVideoSizeChanged(MediaPlayer player, 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;
        }

        mSizeKnown = true;
        if (mVideoReady && mSizeKnown) {
            startVideoPlayback();
        }
    }

    public void onPrepared(MediaPlayer player) {
        Log.d(TAG, "onPrepared called");

        mVideoReady = true;
        if (mVideoReady && mSizeKnown) {
            startVideoPlayback();
        }
    }

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

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

    public void surfaceCreated(SurfaceHolder holder) {
        Log.d(TAG, "surfaceCreated called");

        try {
            mMediaPlayer = new MediaPlayer();
            AssetFileDescriptor afd = getActivity().getAssets().openFd(
                    "sample.3gp");
            mMediaPlayer.setDataSource(afd.getFileDescriptor(),
                    afd.getStartOffset(), afd.getLength());
            mMediaPlayer.setDisplay(mSurfaceHolder);
            mMediaPlayer.prepare();
            mMediaPlayer.setOnBufferingUpdateListener(this);
            mMediaPlayer.setOnCompletionListener(this);
            mMediaPlayer.setOnPreparedListener(this);
            mMediaPlayer.setOnVideoSizeChangedListener(this);
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
         } catch (Exception e) {
            e.printStackTrace();
        }
    }

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

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

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

    private void startVideoPlayback() {
        Log.v(TAG, "startVideoPlayback");
        mMediaPlayer.start();
     }
}

multi_videos_activity_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical">

    <fragment class="com.example.multivideo.VideoFragment"
         android:id="@+id/video_1_fragment" android:layout_width="0dp"
        android:layout_height="wrap_content" android:layout_weight="1" />


</LinearLayout>

multi_videos_fragment_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<SurfaceView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/video_surfaceview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />


VideoFragment.java クラスの mSurfaceHolder = mSurfaceView.getHolder() で nullPointerExceptionを取得しています。

解決策を教えてください。

4

2 に答える 2

0

コードのこの部分を移動してみてください:

 mSurfaceView = (SurfaceView) v.findViewById(R.id.video_surfaceview);

あなたのonCreateView()方法に。

于 2012-12-04T08:08:18.053 に答える
-2

onActivityCreate() メソッドで onCreateView() を呼び出すのを忘れたと思います。

于 2012-12-04T07:58:47.167 に答える