滚动视图中的异常 [英] Exception in Scrollview
本文介绍了滚动视图中的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的 android 应用程序中,我遇到了 IllegalStateException.我以后无法重现此异常.这是堆栈跟踪
In my android application, I'm facing a IllegalStateException. I can't reproduce this exception later. This is the stacktrace
Non-fatal Exception: java.lang.IllegalStateException: ScrollView can host only one direct child
at android.widget.ScrollView.addView(ScrollView.java:397)
at android.support.design.widget.BaseTransientBottomBar.showView(BaseTransientBottomBar.java:436)
at android.support.design.widget.BaseTransientBottomBar$1.handleMessage(BaseTransientBottomBar.java:178)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5679)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(NativeStart.java)
请大家帮帮我!
推荐答案
我遇到了同样的问题.我已经使用以下步骤进行了复制.
第 1 步:使用 Scrollview 的父级为您创建 Fragment 或 Activity 布局.
Step 1: make you Fragment Or Activity Layout with a parent of Scrollview.
第 2 步:像 belove 一样在 onPause()、onStop()、onDestroy() 中显示 Snackbar.
Step 2: show Snackbar in onPause(), onStop(), onDestroy() like belove.
@Override
public void onPause() {
super.onPause();
Snackbar.make(button, "onPause", Snackbar.LENGTH_LONG).show();
}
@Override
public void onStop() {
super.onPause();
Snackbar.make(button, "onStop", Snackbar.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
super.onDestroy();
Snackbar.make(button, "onDestroy", Snackbar.LENGTH_LONG).show();
}
现在运行一个应用程序并检查 logcat.当你点击返回按钮时你会得到同样的错误提到的问题.
Now run an app and check in logcat. when you click on back button you will get same error mention in question.
解决方案:
制作一个普通的 Snackbar,如下所示.
@Override
public void onPause() {
super.onPause();
showSnackbar("onPause");
}
@Override
public void onStop() {
super.onPause();
showSnackbar("onStop");
}
@Override
public void onDestroy() {
super.onDestroy();
showSnackbar("onDestroy");
}
private void showSnackbar(String message) {
if (isValidContext(getActivity())) {
Snackbar.make(btnConsume, message, Snackbar.LENGTH_LONG).show();
}
}
public static boolean isValidContext(final Context context) {
if (context == null) {
return false;
}
if (context instanceof Activity) {
final Activity activity = (Activity) context;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
return !activity.isDestroyed() && !activity.isFinishing();
} else {
return !activity.isFinishing();
}
}
return true;
}
这篇关于滚动视图中的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文