Eric Fossum
Eric Fossum

Reputation: 2461

findViewById() question

I don't consider myself a beginner, but my copy and pasting has left me lost in auto-generated errors. Here's my dilemma, if I remove the comments below the program crashes unexpectedly; with comments it displays just fine.

//////// 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>`  

Upvotes: 0

Views: 6947

Answers (6)

user1785130
user1785130

Reputation:

Button STO = (Button)findViewById(R.id.StopBut);  
Button REC = (Button)findViewById(R.id.RecButton);  

//change Button to ImageButton because in xml file defined as ImageButton.

//activity file

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

//OR //In xml.file change Imageview to Button

<?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">  
    <Button  
        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"/>  
    <Button   
        android:layout_height="wrap_content"   
        android:layout_width="fill_parent"   
        android:layout_weight="3"   
        droid:src="@drawable/stop"   
        android:id="@+id/StopBut"/></LinearLayout></LinearLayout>`  

Upvotes: 0

abhi
abhi

Reputation: 759

You have used ImageButton in xml design but You try to access Button type that's why it's crashes your application

Upvotes: 0

nikki
nikki

Reputation: 3228

Your application is crashing because you are trying to cast ImageButton to Button that is wrong you should cast ImageButton to ImageButton like this..

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

replace your code by this your application will not crash.

Upvotes: 0

Manjeet
Manjeet

Reputation: 25

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);  
     ImageButton STO = (ImageButton)findViewById(R.id.StopBut);  
     ImageButton REC = (ImageButton)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>` 

I think this solve your problem

Upvotes: 0

Chris Fei
Chris Fei

Reputation: 1317

It's crashing because you're trying to cast ImageButton to Button. ImageButton does not subclass Button. In the constructor, change to

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

Upvotes: 2

James Lockhart
James Lockhart

Reputation: 1040

Which comments?

Also note @Override is NOT a comment, it is a command

It shouldn't matter but remember to clean up the beginning white space in the XML too

Upvotes: 0

Related Questions