首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >findViewById()问题

findViewById()问题
EN

Stack Overflow用户
提问于 2010-11-14 10:21:03
回答 6查看 6.5K关注 0票数 0

我不认为自己是初学者,但我的复制和粘贴让我迷失在自动生成的错误中。这是我的两难境地,如果我删除下面的注释,程序会意外崩溃;带有注释的程序显示得很好。

代码语言:javascript
运行
复制
//////// Options.java /////  
public class Options extends Activity {  
/** Called when the activity is first created. */  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.reclayout);  
        //Button STO = (Button)findViewById(R.id.StopBut);  
        //Button REC = (Button)findViewById(R.id.RecButton);  
    }  
}  

///////reclayout.xml/////  
<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout  
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_height="fill_parent"  
    android:layout_width="fill_parent"  
    android:orientation="vertical"  
    android:gravity="center_vertical">
    <Spinner   
        android:id="@+id/Spinner01"  
        android:layout_height="wrap_content"  
        android:layout_width="fill_parent"  
        android:contentDescription="Classes"/>  
    <EditText  
        android:layout_height="wrap_content"   
        android:layout_width="fill_parent"   
        android:lines="3"   
        android:id="@+id/DescriptionText"   
        android:text="Class Description"/>  
    <LinearLayout  
        android:layout_height="wrap_content"   
        android:id="@+id/ButtonLayout"   
        android:orientation="horizontal"   
        android:layout_width="fill_parent">  
        <ImageButton  
            android:layout_height="wrap_content"   
            android:layout_width="fill_parent"   
            android:layout_weight="3"   
            android:id="@+id/RecButton"   
            android:focusableInTouchMode="true"   
            android:src="@drawable/rec"/>  
        <ImageButton   
            android:layout_height="wrap_content"   
            android:layout_width="fill_parent"   
            android:layout_weight="3"   
            android:id="@+id/PauseButton"   
            android:src="@drawable/pause"/>  
        <ImageButton   
            android:layout_height="wrap_content"   
            android:layout_width="fill_parent"   
            android:layout_weight="3"   
            droid:src="@drawable/stop"   
            android:id="@+id/StopBut"/></LinearLayout></LinearLayout>`  
EN

Stack Overflow用户

回答已采纳

发布于 2010-11-14 10:40:40

它崩溃是因为您试图将ImageButton转换为ButtonImageButton不是Button的子类。在构造函数中,更改为

ImageButton STO = (ImageButton)findViewById(R.id.StopBut);

ImageButton REC = (ImageButton)findViewById(R.id.RecButton);

票数 2
EN
查看全部 6 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4175868

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档