我正在开发一个应用程序,其中我有一个用 ScrollView 包装的布局。在此布局的 fragment 中,我正在使用 Web 服务从服务器获取数据并将其显示在我的布局中。每当 Web 服务中出现错误或没有可用数据时,我都会向用户显示一个带有适当消息的 snackbar 。
问题
我遇到了一个很少发生的问题。当有时数据不可用并显示 snackbar 时,我的应用程序崩溃并出现非法状态异常显示消息
ScrollView 只能托管一个直接子级
这是崩溃日志
java.lang.IllegalStateException: ScrollView can host only one direct child
at android.widget.ScrollView.addView(ScrollView.java:253)
at android.support.design.widget.Snackbar.showView(Snackbar.java:475)
at android.support.design.widget.Snackbar$1.handleMessage(Snackbar.java:162)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:6102)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
我在很多屏幕上都使用这个组合,所以我只发布其中一个 xml 的布局代码
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none">
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background"
android:orientation="vertical"
android:paddingBottom="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="10dp">
<com.xsinfosol.emergency.progressbar.RopeProgressBar
android:id="@+id/progress_bar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:ropeDynamicLayout="true"
app:ropePrimaryColor="@color/red"
app:ropeSecondaryColor="#20FFFFFF"
app:ropeSlack="0dp"
app:ropeStrokeWidth="10dp" />
<ImageView
android:id="@+id/iv_default_sos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="40dp"
android:src="@drawable/on_light" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@drawable/edittext_bg">
<com.xsinfosol.emergency.utils.CustomTextView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:layout_margin="10dp"
android:background="@drawable/number_circle"
android:gravity="center"
android:text="1"
android:textColor="@color/red"
android:textSize="22sp" />
<com.xsinfosol.emergency.utils.CustomTextView
android:id="@+id/tv_view_health"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:drawableRight="@drawable/medical"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="Health"
android:textColor="@android:color/white" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@drawable/edittext_bg">
<com.xsinfosol.emergency.utils.CustomTextView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:layout_margin="10dp"
android:background="@drawable/number_circle"
android:gravity="center"
android:text="2"
android:textColor="@color/red"
android:textSize="22sp" />
<com.xsinfosol.emergency.utils.CustomTextView
android:id="@+id/tv_view_police"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:drawableRight="@drawable/police"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="Police"
android:textColor="@android:color/white" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@drawable/edittext_bg">
<com.xsinfosol.emergency.utils.CustomTextView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:layout_margin="10dp"
android:background="@drawable/number_circle"
android:gravity="center"
android:text="3"
android:textColor="@color/red"
android:textSize="22sp" />
<com.xsinfosol.emergency.utils.CustomTextView
android:id="@+id/tv_view_fire"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:drawableRight="@drawable/fire"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="Fire"
android:textColor="@android:color/white" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@drawable/edittext_bg">
<com.xsinfosol.emergency.utils.CustomTextView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:layout_margin="10dp"
android:background="@drawable/number_circle"
android:gravity="center"
android:text="4"
android:textColor="@color/red"
android:textSize="22sp" />
<com.xsinfosol.emergency.utils.CustomTextView
android:id="@+id/tv_view_no_4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:drawableRight="@drawable/contact_person"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="Contact Person 1"
android:textColor="@android:color/white" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@drawable/edittext_bg">
<com.xsinfosol.emergency.utils.CustomTextView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:layout_margin="10dp"
android:background="@drawable/number_circle"
android:gravity="center"
android:text="5"
android:textColor="@color/red"
android:textSize="22sp" />
<com.xsinfosol.emergency.utils.CustomTextView
android:id="@+id/tv_view_no_5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:drawableRight="@drawable/contact_person"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="Contact Person 2"
android:textColor="@android:color/white" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@drawable/edittext_bg">
<com.xsinfosol.emergency.utils.CustomTextView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:layout_margin="10dp"
android:background="@drawable/number_circle"
android:gravity="center"
android:text="6"
android:textColor="@color/red"
android:textSize="22sp" />
<com.xsinfosol.emergency.utils.CustomTextView
android:id="@+id/tv_view_no_6"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:drawableRight="@drawable/contact_person"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="Contact Person 3"
android:textColor="@android:color/white" />
</LinearLayout>
</LinearLayout>
</ScrollView>
这是我从api解析数据的方法
public void parseSOSResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
String Message=jsonObject.optString("Message");
int error_code = jsonObject.getInt("error_code");
if (error_code == 1) {
String num_4 = jsonObject.getString("num_4");
String num_5 = jsonObject.getString("num_5");
String num_6 = jsonObject.getString("num_6");
String health_number=jsonObject.getString("health_number");
String fire_number=jsonObject.getString("fire_number");
String police_number=jsonObject.getString("police_number");
Profile_complete=jsonObject.getInt("Profile_complete");
default_sos = jsonObject.getString("default_sos");
tv_view_no_4.setText(num_4);
tv_view_no_5.setText(num_5);
tv_view_no_6.setText(num_6);
tv_view_fire.setText("Fire \n"+fire_number);
tv_view_police.setText("Police \n"+police_number);
tv_view_health.setText("Health \n"+health_number);
progress_bar1.setMax(100);
progress_bar1.animate();
progress_bar1.animateProgress(Profile_complete);
prefManager.putString(PrefrenceConstants.KEY_EMERGENCY_TYPE,default_sos);
}else{
((ActivityNavigationDrawer)getActivity()).showSnackBar(iv_default_sos,Message);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
showSnackbar 方法
public void showSnackBar(View view, String message) {
Snackbar.make(view, message, Snackbar.LENGTH_SHORT).show();
}
最佳答案
问题是 ScrollViews 只能有一个直接的 child ,而在你的代码中这个 child 是外部线性布局。当调用 snackbar 显示时,我想您请求在其中绘制 snackbar 的 View 是 ScrollView 本身,因此将有两个直接子级(您的线性布局和现在关于 snackbar 的 View )
关于android - SnackBar 导致 ScrollView 崩溃并出现非法状态异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38525371/
我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
当我在Rails控制台中按向上或向左箭头时,出现此错误:irb(main):001:0>/Users/me/.rvm/gems/ruby-2.0.0-p247/gems/rb-readline-0.4.2/lib/rbreadline.rb:4269:in`blockin_rl_dispatch_subseq':invalidbytesequenceinUTF-8(ArgumentError)我使用rvm来管理我的ruby安装。我正在使用=>ruby-2.0.0-p247[x86_64]我使用bundle来管理我的gem,并且我有rb-readline(0.4.2)(人们推荐的最少
当我的预订模型通过rake任务在状态机上转换时,我试图找出如何跳过对ActiveRecord对象的特定实例的验证。我想在reservation.close时跳过所有验证!叫做。希望调用reservation.close!(:validate=>false)之类的东西。仅供引用,我们正在使用https://github.com/pluginaweek/state_machine用于状态机。这是我的预订模型的示例。classReservation["requested","negotiating","approved"])}state_machine:initial=>'requested
我正在学习Rails,并阅读了关于乐观锁的内容。我已将类型为integer的lock_version列添加到我的articles表中。但现在每当我第一次尝试更新记录时,我都会收到StaleObjectError异常。这是我的迁移:classAddLockVersionToArticle当我尝试通过Rails控制台更新文章时:article=Article.first=>#我这样做:article.title="newtitle"article.save我明白了:(0.3ms)begintransaction(0.3ms)UPDATE"articles"SET"title"='dwdwd
在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee
我早就知道Ruby中的“常量”(即大写的变量名)不是真正常量。与其他编程语言一样,对对象的引用是唯一存储在变量/常量中的东西。(侧边栏:Ruby确实具有“卡住”引用对象不被修改的功能,据我所知,许多其他语言都没有提供这种功能。)所以这是我的问题:当您将一个值重新分配给常量时,您会收到如下警告:>>FOO='bar'=>"bar">>FOO='baz'(irb):2:warning:alreadyinitializedconstantFOO=>"baz"有没有办法强制Ruby抛出异常而不是打印警告?很难弄清楚为什么有时会发生重新分配。 最佳答案
对于作为String#tr参数的单引号字符串文字中反斜杠的转义状态,我觉得有些神秘。你能解释一下下面三个例子之间的对比吗?我特别不明白第二个。为了避免复杂化,我在这里使用了'd',在双引号中转义时不会改变含义("\d"="d")。'\\'.tr('\\','x')#=>"x"'\\'.tr('\\d','x')#=>"\\"'\\'.tr('\\\d','x')#=>"x" 最佳答案 在tr中转义tr的第一个参数非常类似于正则表达式中的括号字符分组。您可以在表达式的开头使用^来否定匹配(替换任何不匹配的内容)并使用例如a-f来匹配一
我目前正在使用以下方法获取页面的源代码:Net::HTTP.get(URI.parse(page.url))我还想获取HTTP状态,而无需发出第二个请求。有没有办法用另一种方法做到这一点?我一直在查看文档,但似乎找不到我要找的东西。 最佳答案 在我看来,除非您需要一些真正的低级访问或控制,否则最好使用Ruby的内置Open::URI模块:require'open-uri'io=open('http://www.example.org/')#=>#body=io.read[0,50]#=>"["200","OK"]io.base_ur
SPI接收数据左移一位问题目录SPI接收数据左移一位问题一、问题描述二、问题分析三、探究原理四、经验总结最近在工作在学习调试SPI的过程中遇到一个问题——接收数据整体向左移了一位(1bit)。SPI数据收发是数据交换,因此接收数据时从第二个字节开始才是有效数据,也就是数据整体向右移一个字节(1byte)。请教前辈之后也没有得到解决,通过在网上查阅前人经验终于解决问题,所以写一个避坑经验总结。实际背景:MCU与一款芯片使用spi通信,MCU作为主机,芯片作为从机。这款芯片采用的是它规定的六线SPI,多了两根线:RDY和INT,这样从机就可以主动请求主机给主机发送数据了。一、问题描述根据从机芯片手