gpt4 book ai didi

android - 空对象引用上的 void android.view.View.dispatchWindowVisibilityChanged(int)'

转载 作者:行者123 更新时间:2023-11-29 17:00:22 26 4
gpt4 key购买 nike

我有一个 Activity A [MainActivity],我从中启动 Activity B [Videoplayer]
案例 1:点击 Activity A 中的视频缩略图 => 转到 Activity B => 播放视频几秒钟(重要:您需要播放几秒钟的视频)=> 点击返回导航 =>回到 Activity A => 没有崩溃

案例 2:单击 Activity A 中的视频缩略图 => 转到 Activity B => 缓冲区和视频开始播放 => 立即单击返回导航(重要:不要播放视频)= > 应用尝试返回 Activity A => 崩溃及其一致

注意:我没有在 Activity A 的 onResume() 中编写任何代码,只是为了确保我没有覆盖任何 onResume() 相关代码,这可能会导致崩溃。stacktrace 不指向应用程序代码中的一行,它只是显示来自 android 源 lib 的 stacktrace。我为此花费了超过 4 小时,感谢任何帮助。

Activity B [Player Activity]代码:

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)

最佳答案

在进一步检查文档后,您似乎陷入了 onCreate 文档中的这种情况:https://developer.android.com/reference/android/app/Activity.html#onCreate(android.os.Bundle)

You can call finish() from within this function, in which case onDestroy() will be immediately called without any of the rest of the activity lifecycle (onStart(), onResume(), onPause(), etc) executing.

由于您的 onBackPresseddoBack 都调用了 finish(),您可能会遇到 videoView 的竞争条件code> 仍在您的 Activity 的 onCreate 中启动,在 onCreate 结束之前按下返回键,您的 Activity 在尝试播放 videoView 时被销毁同时跳过所有其他生命周期函数,导致这个奇怪的堆栈跟踪崩溃。

修复:覆盖 onResume 并在其中调用 videoView.start 并覆盖 onDestroyonStop并在那里调用 if (videoView.isPlaying()) videoView.stopPlayback

关于android - 空对象引用上的 void android.view.View.dispatchWindowVisibilityChanged(int)',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43288356/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com