开发问题及解决--java.lang.IllegalStateException: Circular dependencies cannot exist in RelativeLayout


今天使用Scrollview 和HorizontalScrollview 嵌套显示时,使用如下布局:


<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <HorizontalScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <TextView
                android:id="@+id/textview1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="滚动视图"
                android:textSize="30dp" />

            <ImageView
                android:id="@+id/imageview1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/textview1"
                android:src="@drawable/item1" />

            <TextView
                android:id="@+id/textview2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/imageview1"
                android:text="垂直和水平滚动"
                android:textSize="30dp" />

            <ImageView
                <span style="color:#ff6666;">android:id="@+id/imageview2"
</span>                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/textview1"
                android:src="@drawable/item2" />

            <TextView
                android:id="@+id/textview3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/imageview2"
                android:text="垂直和水平滚动"
                android:textSize="30dp" />

            <ImageView
               <span style="color:#ff6666;"> android:id="@+id/imageview2"</span>
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/textview3"
                android:src="@drawable/item3" />
        </RelativeLayout>
    </HorizontalScrollView>

</ScrollView>

爆出了这样的异常,java.lang.IllegalStateException: Circular dependencies cannot exist in RelativeLayout。

原因就是在RelativeLayout布局中重现了重复的view ID名称,且出现 imageview2 ,textview3,imageview2出现了循环依赖。

解决方案: 将最后一个ImageView的id修改成 imageview3.  避免在RelativeLayout中控件循环依赖。


### 解决 `LettuceConnectionFactory` 被销毁后无法再使用的异常 当遇到 `java.lang.IllegalStateException: LettuceConnectionFactory destroyed cannot be used` 的错误时,这通常意味着连接工厂已经被显式关闭或销毁,在尝试重新使用它之前需要创建新的实例。 #### 原因分析 此异常表明当前线程试图访问已被标记为已销毁的对象。一旦 `LettuceConnectionFactory` 实例被销毁,就不再允许任何操作继续执行。这种行为是为了防止潜在的数据不一致和其他并发问题[^1]。 #### 处理方法 为了处理这种情况,可以采取以下措施: - **确保单例模式下的正确管理**:如果应用程序中仅需一个 Redis 连接池,则应保证该对象在整个应用生命周期内保持有效状态。 - **避免重复初始化和销毁**:频繁地创建和销毁 `LettuceConnectionFactory` 可能会引发资源泄漏或其他不稳定因素。因此建议在适当的地方(如Spring上下文中)配置并注入这个bean而不是手动new出来。 - **捕获异常并重试逻辑** 如果确实存在某些场景下不得不重建的情况,可以在捕捉到上述异常之后安全地释放旧资源,并按照业务需求决定是否要立即新建一个新的实例来替代原来的那个。 ```java try { // 尝试获取Redis连接... } catch (IllegalStateException e) { if ("destroyed".equals(e.getMessage())) { // 关闭现有连接厂库 lettuceConnectionFactory.destroy(); // 创建新实例替换之 lettuceConnectionFactory = new LettuceConnectionFactory(redisConfig); lettuceConnectionFactory.afterPropertiesSet(); // 初始化设置 // 继续正常流程... } else { throw e; // 非预期情况抛出原异常 } } ``` 对于基于Spring框架的应用程序来说,还可以考虑利用其依赖注入机制以及作用域特性(`@Scope`) 来更好地控制组件的生命期[^2]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值