2011-05-25 68 views
0

我在布局(XML)上声明了一个ImageView,但是当我尝试通过ID获取它时,我得到一个TextField!!!然后我的代码抛出ClassCastExceptionImageView xml返回为TextView

这里是我的布局代码(splash.xml):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    style="@android:style/Theme.Light" android:layout_height="wrap_content" android:background="@drawable/bg"> 
    <ImageView android:layout_height="wrap_content" android:layout_width="match_parent" android:src="@drawable/splash" android:layout_marginTop="20pt" android:id="@+id/splashLogo"></ImageView> 
    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name" android:id="@+id/splashTitle" android:textStyle="bold" android:textSize="@dimen/title_size" android:layout_gravity="center" android:lineSpacingExtra="@dimen/spacer" android:gravity="center"></TextView> 
    <TextView android:id="@+id/splashCopyleft" android:textSize="@dimen/version_size" android:lineSpacingExtra="@dimen/version_spacing" android:layout_width="wrap_content" android:gravity="center" android:layout_height="wrap_content" android:text="@string/app_version_info" android:layout_gravity="center" android:textColor="@color/version"></TextView> 
</LinearLayout> 

这里是我的Java(活动)代码:

// imports 

public class SplashActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.splash); 

     // Grab a ClassCastException here 
     ImageView logo = (ImageView) findViewById(R.id.splashLogo); 

     // Never get here :'(
     Log.i("GOT", logo.toString()); 
    } 
} 
+0

你的问题是什么? – 2011-05-25 05:43:07

回答

3

有时候Eclipse的转移资源ID。尝试清理您的项目并重新构建。

+0

我该怎么做? – 2011-05-25 14:53:01

+0

我会关闭日食,重新打开然后干净。项目(顶部菜单)>清洁。 Eclipse得到的ID有点狡猾,我经常需要这样做。 – ataulm 2011-05-25 15:07:50

+0

@Marcoz在Eclipse的菜单中选择* Project-> Clean ... *,然后选择* Clean all projects *单选按钮并单击* OK *。 – Michael 2011-05-25 15:44:41

0

首先,干净你的项目然后建立它。

如果问题仍然存在,关闭 Eclipse和重新打开它。

+0

我该怎么做? – 2011-05-25 14:53:18