Click here to Skip to main content
15,892,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is the class where i am getting the error:

C++
public class NewGame extends Activity implements OnClickListener{
    EditText word;
    Button createGame;

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

        word = (EditText) findViewById(R.id.word);
        createGame = (Button) findViewById(R.id.createGame);
        createGame.setOnClickListener(this);

    }

    public void onClick(View v){
        Intent i = new Intent(NewGame.this, Game.class);
        i.putExtra("word", word.getText().toString());
        startActivity(i);
    }
}


The error is ClassCastException: android.widget.Button

And just in case here is newgame.xml:
MSIL
<?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"
    android:layout_height="fill_parent">

    <TextView android:text="Create New Game"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="22sp"
            android:gravity="center"/>

    <EditText android:hint="Enter Word"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/word"
            android:password="true"/>

    <Button android:id="@+id/createGame"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Create Game"
            android:textSize="20sp"/>
</LinearLayout>
Posted

1 solution

If you're sure that the exception is in this code, it's here" createGame = (Button) findViewById(R.id.createGame);. You assume that the type is Button but it is not.

You should indicate the line of code throwing exception. You can look at exception stack under debugger or exception handler and report it properly when asking a question.

—SA
 
Share this answer
 
Comments
Abhinav S 26-Jul-11 13:04pm    
Good answer. My 5.
Sergey Alexandrovich Kryukov 26-Jul-11 13:29pm    
Thank you, Abhinav.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900