Android:我的应用突然崩溃了

时间:2012-10-30 20:32:54

标签: android classcastexception

我一直在调整我的主要xml布局,在我的相对布局中调整不同视图的位置,突然当我在我的模拟器上启动它时,它崩溃了消息:ClassCastException

我的XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" >

 <com.example.bingbong.Render
    android:id="@+id/render"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:background="@color/pattern5"
    android:clickable="true" />

<TextView
    android:id="@+id/fps"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="fpasds"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/score_top"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="asd"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/score_bot"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true"
    android:text="asfa"
    android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>

错误日志:

10-30 20:25:33.266: E/AndroidRuntime(269): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bingbong/com.example.bingbong.Main_Activity}: java.lang.ClassCastException: android.widget.TextView
...
10-30 20:25:33.266: E/AndroidRuntime(269): Caused by: java.lang.ClassCastException: android.widget.TextView
10-30 20:25:33.266: E/AndroidRuntime(269):  at com.example.bingbong.Main_Activity.onCreate(Main_Activity.java:26)

Main_Activity - 第26行是:

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.main_layout);
        r = (Render) findViewById(R.id.render);   // <--- Line 26.
        r.setView((TextView)findViewById(R.id.fps),(TextView)findViewById(R.id.score_top),(TextView)findViewById(R.id.score_bot));
    }

2 个答案:

答案 0 :(得分:4)

如果我移动视图或更改视图类型(即TextView更改为Button,或类似的东西),我偶尔会得到这个。通常为我修复的是做一个干净的构建。

在Eclipse的“项目”菜单中,选择“清除...”,然后执行所有项目或选择该特定项目。

答案 1 :(得分:0)

哟需要清理项目,我经常遇到这个错误

相关问题