0

我正在修改我的应用程序,设置一些动画。现在基本上我的活动是组成的

<LinearLayout>
    <LinearLayout>
        <!-- THE HEADER AND A BUTTON -->
    </LinearLayout>
    <View>
        <!-- A SEPARATION LINE -->
    </View>
    <ScrollView>
        <!-- THE CONTENT -->
    </ScrollView>
</LinearLayout>

动画始终设置为从左侧推入标题+行,然后从按钮推入内容。现在在一个特定的地方Activity,我看到这条线View被放入了ScrollView(那里有一个RelativeLayout,因为它ScrollView只有一个孩子)。

当我第一次设置动画时(线在错误的位置,一切正常):

private void animateHeaderAndContent(){
    LinearLayout header = (LinearLayout) findViewById(R.id.overview_lin_0);
    header.setAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in_header));
    ScrollView content = (ScrollView) findViewById(R.id.sv_overview_content);
    content.setAnimation(AnimationUtils.loadAnimation(this, R.anim.push_up_in_content));
}

在 xml 中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent" 
        android:layout_height="match_parent"
        android:orientation="vertical" 
        >
        <LinearLayout 
            android:id="@+id/overview_lin_0"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >
            (...)
        </LinearLayout>

        <ScrollView android:id="@+id/sv_overview_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >
            <RelativeLayout 
                android:id="@+id/rel_over" 
                android:orientation="vertical"
                android:layout_width="match_parent" 
                android:layout_height="wrap_content"        
                >
                 <View   
                    android:id="@+id/v_overview_line1"
                    android:layout_height="1dp"
                    android:layout_width="match_parent"
                    android:layout_marginBottom="10dp"
                    android:background="#FFFFFF"
                    />

                (...)

当我像下面这样编辑代码和 xml 时,它在 line 处给了我一个 ClassCastException ScrollView content = (ScrollView) findViewById(R.id.sv_overview_content);,这与以前完全相同。

在代码中:

private void animateHeaderAndContent(){
    LinearLayout header = (LinearLayout) findViewById(R.id.overview_lin_0);
    header.setAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in_header));
    View line = (View) findViewById(R.id.v_overview_line1);
    line.setAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in_header));
    ScrollView content = (ScrollView) findViewById(R.id.sv_overview_content);
    content.setAnimation(AnimationUtils.loadAnimation(this, R.anim.push_up_in_content));
}

在 xml 中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" 
    android:layout_height="match_parent"
    android:orientation="vertical" 
    >
    <LinearLayout 
        android:id="@+id/overview_lin_0"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <View 
            android:layout_width="25dp"
            android:layout_height="0dp"
            />
        <TextView 
            style="@style/TextHeader"
            android:id="@+id/tv_overview_name"
            android:layout_width="0dp"
            android:layout_weight="8"
            android:gravity="center"
            android:layout_height="wrap_content"
            />
        <ImageButton style="@style/ImageButtonSmall"
            android:layout_width="26dp"
            android:layout_height="26dp" 
            android:src="@drawable/ic_action_delete"
            android:contentDescription="@string/desc"
            android:onClick="onClickDeleteBet"
            />
    </LinearLayout>

    <View   
        android:id="@+id/v_overview_line1"
        android:layout_height="1dp"
        android:layout_width="match_parent"
        android:layout_marginBottom="10dp"
        android:background="#FFFFFF"
        />

    <ScrollView 
        android:id="@+id/sv_overview_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <RelativeLayout 
            android:id="@+id/rel_over" 
            android:orientation="vertical"
            android:layout_width="match_parent" 
            android:layout_height="wrap_content"        
            >
            (...)

这怎么解释(因为在这两种情况下都是完全相同的操作,除了View中间有一个不应该干扰 a 的操作findViewById(...))?

4

1 回答 1

1

类中的引用可能R没有正确更新。这有时会发生。销毁Reclipse中生成的文件(文件夹下gen)。然后立即重启eclipse。接下来,选择项目并单击Project-->Clean... 然后再次构建您的应用程序,R该类将重新生成。

我希望这有帮助。

于 2012-10-27T15:41:15.953 回答