void android.view.View.dispatchWindowVisibilityChanged(int)'在null对象引用上

时间:2017-04-07 23:15:00

标签: android view nullpointerexception

我有一个来自wic的活动A(MainActivity)我开始活动B(视频播放器)
案例1:点击Act A =>中的vid缩略图转到行为B =>播放视频几秒钟(重要播放几秒钟)=>点击返回btn =>回到法案A,圣洁的我,没有崩溃。

案例2:点击Act A =>中的vid缩略图转到行为B =>缓冲区和视频开始播放,立即点击返回(重要的是不在这里播放)=>试图回到行动A,地狱我,崩溃和它的一贯。

注意:我在onResume中没有代码,只是为了确保我没有覆盖任何onResume内容,这可能会导致崩溃。 疯狂的部分是它没有指向应用程序代码中的一行,它只显示来自android源代码库的堆栈跟踪。我花了超过 4 + hrs ,请帮帮我。

Activty B / Player活动代码:

public class VitamioPlayerActivity extends AppCompatActivity implements MediaPlayer.OnErrorListener, MediaPlayer.OnPreparedListener, MediaPlayer.OnTimedTextListener {
public static final String TAG = VitamioPlayerActivity.class.getSimpleName();

@Bind(R.id.videoView)
VideoView videoView;

@Bind(R.id.progressBar)
ProgressBar progressBar;

@Bind(R.id.subtitle_view)
TextView mSubtitleView;

private VideoModel videoModel;
private long mPosition = 0;

private boolean isLiveChannel;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_vitamio);
    ButterKnife.bind(this);
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    if (getIntent() == null)
        return;

    if (getIntent().getExtras() == null)
        return;

    if (!getIntent().getExtras().containsKey(AppConstant.KEY_BUNDLE_VIDEO))
        return;

    videoModel = (VideoModel) getIntent().getExtras().getSerializable(AppConstant.KEY_BUNDLE_VIDEO);
    if (videoModel == null)
        return;

    playVideo();
}


VideoControllerView controllerView;

void playVideo() {
    String path = null;
    if (videoModel.isLoadBalancer()) {
        path = URLHelper.getInstance().getPlayableVideoUrl(videoModel.getVideoUrl());
    } else
        path = videoModel.getVideoUrl();

    if (TextUtils.isEmpty(path)) {
        Toast.makeText(this, "Please edit MediaPlayer Activity, " + "and set the path variable to your media file path." + " Your media file must be stored on sdcard.", Toast.LENGTH_LONG).show();
        return;
    }
    isLiveChannel = !(path.contains(".mp4"));
    videoView.setVideoURI(Uri.parse(path));
    videoView.setMediaController(controllerView = new VideoControllerView(this, !isLiveChannel));
    videoView.requestFocus();
    videoView.start();

    videoView.setOnPreparedListener(this);
    videoView.setOnErrorListener(this);
}

@Override
public void onPrepared(MediaPlayer mediaPlayer) {
    mediaPlayer.setPlaybackSpeed(1.0f);
}

@Override
public void onTimedText(String text) {
    mSubtitleView.setText(text);
}

@Override
public void onTimedTextUpdate(byte[] pixels, int width, int height) {
    int a = 1;
}


@Nullable
@OnClick(R.id.imvHeaderBack)
void doBack() {
    finish();
}

@Override
public void onBackPressed() {
    finish();
}

@Override
protected void onPause() {
    mPosition = videoView.getCurrentPosition();
    videoView.stopPlayback();
    super.onPause();
}

@Override
protected void onResume() {
    if (mPosition > 0) {
        videoView.seekTo((int) mPosition);
        mPosition = 0;
    }
    super.onResume();
}

@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
    WindowUtils.getInstance().genericInfoMsgWithOKCallBack(this, null, "Sorry, Cannot Play Video", R.color.redStatus,
            new ViewConstructor.InfoDisplayListener() {
                @Override
                public void onPositiveSelection(DialogInterface alertDialog) {
                    doBack();
                }
            });
    return true;
}

}

堆栈跟踪:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.dispatchWindowVisibilityChanged(int)' on a null object reference
    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1310)
    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1310)
    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1310)
    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1310)
    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1310)
    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1310)
    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1310)
    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1310)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1410)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1134)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6050)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:860)
    at android.view.Choreographer.doCallbacks(Choreographer.java:672)
    at android.view.Choreographer.doFrame(Choreographer.java:608)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:846)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5442)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)

1 个答案:

答案 0 :(得分:3)

在更多地查看文档后,您似乎从onCreate文档陷入了这种情况:https://developer.android.com/reference/android/app/Activity.html#onCreate(android.os.Bundle)

  

您可以在此函数中调用finish(),在这种情况下,将立即调用onDestroy(),而不执行任何其余的活动生命周期(onStart(),onResume(),onPause()等)。

由于您的onBackPresseddoBack都致电finish(),您可能会遇到videoView仍在您的活动onCreate中开始的竞争条件,在onCreate结束之前按下后退并且在跳过所有其他生命周期函数时尝试播放videoView时活动被破坏,导致此奇怪堆栈跟踪崩溃。

FIX:覆盖onResume并在其中调用videoView.start并覆盖onDestroyonStop并在其中调用if (videoView.isPlaying()) videoView.stopPlayback < / p>