android中奇怪的类型转换错误

时间:2015-01-30 02:00:09

标签: android android-layout android-xml

我有一个Android布局,如下所示:
enter image description here

的内容 Bit pattern --> aaa
在运行时更改为多行字符串,导致Send命令按钮向下移动。为了使按钮保持在相同的位置,我改变了组件的位置,如下所示:
enter image description here

在这两种情况下,xml文件内容看起来都是一样的(如果它有所不同,它使用LinearLayout

<?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:gravity="clip_horizontal"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/freqIndxTextView_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/freqIndxTextView_string" />

    <TextView
        android:id="@+id/freqIndxValTextView_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/freqIndxValTextView_string" />

    <SeekBar
        android:id="@+id/freqIndxSeekBar_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp" />

    <TextView
        android:id="@+id/gainLevelTextView_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/gainLevelTextView_string" />

    <TextView
        android:id="@+id/gainLevelValTextView_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/gainLevelValTextView_string" />

    <SeekBar
        android:id="@+id/gainLevelSeekBar_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp" />

    <Button
        android:id="@+id/btnInSituActivitySend_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="InSituActivitySend_click"
        android:text="@string/btnInSituActivitySend_string" />

    <TextView
        android:id="@+id/bitPatternTextView_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/bitPatternTextView_string" />

    <TextView
        android:id="@+id/bitPatternValTextView_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/bitPatternValTextView_string" />

</LinearLayout>  

但是在第二种情况下,当此活动开始时,我收到错误并且应用程序关闭:

01-30 10:38:43.351: E/AndroidRuntime(3097): FATAL EXCEPTION: main
01-30 10:38:43.351: E/AndroidRuntime(3097): Process: com.example.fourcommandmenu, PID: 3097
01-30 10:38:43.351: E/AndroidRuntime(3097): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fourcommandmenu/com.example.fourcommandmenu.InSituActivity}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
01-30 10:38:43.351: E/AndroidRuntime(3097):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
01-30 10:38:43.351: E/AndroidRuntime(3097):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
01-30 10:38:43.351: E/AndroidRuntime(3097):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
01-30 10:38:43.351: E/AndroidRuntime(3097):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
01-30 10:38:43.351: E/AndroidRuntime(3097):     at android.os.Handler.dispatchMessage(Handler.java:102)
01-30 10:38:43.351: E/AndroidRuntime(3097):     at android.os.Looper.loop(Looper.java:136)
01-30 10:38:43.351: E/AndroidRuntime(3097):     at android.app.ActivityThread.main(ActivityThread.java:5001)
01-30 10:38:43.351: E/AndroidRuntime(3097):     at java.lang.reflect.Method.invokeNative(Native Method)
01-30 10:38:43.351: E/AndroidRuntime(3097):     at java.lang.reflect.Method.invoke(Method.java:515)
01-30 10:38:43.351: E/AndroidRuntime(3097):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
01-30 10:38:43.351: E/AndroidRuntime(3097):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
01-30 10:38:43.351: E/AndroidRuntime(3097):     at dalvik.system.NativeStart.main(Native Method)
01-30 10:38:43.351: E/AndroidRuntime(3097): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
01-30 10:38:43.351: E/AndroidRuntime(3097):     at com.example.fourcommandmenu.InSituActivity.onCreate(InSituActivity.java:70)
01-30 10:38:43.351: E/AndroidRuntime(3097):     at android.app.Activity.performCreate(Activity.java:5231)
01-30 10:38:43.351: E/AndroidRuntime(3097):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-30 10:38:43.351: E/AndroidRuntime(3097):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
01-30 10:38:43.351: E/AndroidRuntime(3097):     ... 11 more

主要错误似乎是:

01-30 10:38:43.351: E/AndroidRuntime(3097): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
01-30 10:38:43.351: E/AndroidRuntime(3097):     at com.example.fourcommandmenu.InSituActivity.onCreate(InSituActivity.java:70)

onCreate()中有问题的一行是:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_in_situ);
    freqIndxTextView = (TextView)findViewById(R.id.freqIndxTextView_id);
    gainLevelTextView = (TextView)findViewById(R.id.gainLevelTextView_id);
    freqIndxValTextView = (TextView)findViewById(R.id.freqIndxValTextView_id);
    gainLevelValTextView = (TextView)findViewById(R.id.gainLevelValTextView_id);
    bitPatternTextView = (TextView)findViewById(R.id.bitPatternTextView_id);
    bitPatternValTextView = (TextView)findViewById(R.id.bitPatternValTextView_id);
    freqIndxSeekBar = (SeekBar)findViewById(R.id.freqIndxSeekBar_id);
    gainLevelSeekBar = (SeekBar)findViewById(R.id.gainLevelSeekBar_id);
    btnInSituActivitySend = (Button)findViewById(R.id.btnInSituActivitySend_id);
    freqIndxValTextView.setText(Integer.toString(freqIndxSeekBar.getProgress()));
    gainLevelValTextView.setText(Integer.toString(gainLevelSeekBar.getProgress()));  
 .  
 .  
 .  

btnInSituActivitySend = (Button)findViewById(R.id.btnInSituActivitySend_id);

btnInSituActivitySend定义为Button btnInSituActivitySend;btnInSituActivitySend_id在xml文件中定义为

<Button
    android:id="@+id/btnInSituActivitySend_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="InSituActivitySend_click"
    android:text="@string/btnInSituActivitySend_string" />  

摘要
我没有将TextView类型转换为按钮。那为什么我会收到这个错误?

编辑(对于未来的Google员工):
这似乎是一个非常常见的问题,但很难找到解决方案,可能是由于错误描述的模糊性,以及看到此问题没有提供关于在Google上使用哪些搜索字词的想法。 解决方案是清理项目

Android.widget,textView cannot be cast to android.widget,button
android.widget.Button cannot be cast to android.widget.EditText
java.lang.ClassCastException: android.widget.TextView. Why am I getting this?
Google search

2 个答案:

答案 0 :(得分:2)

删除gen文件夹并重新编译项目。

我之前看过这个错误,而且R文件中通常是误导。

答案 1 :(得分:1)

在删除gen文件夹或R.java之前,尝试删除问题栏中显示的错误,然后清理项目。它将解决此问题