Reputation: 518
I got this error
03-03 16:53:01.790: E/AndroidRuntime(1366): FATAL EXCEPTION: main
03-03 16:53:01.790: E/AndroidRuntime(1366): Process: com.mad.tictactoepk, PID: 1366
03-03 16:53:01.790: E/AndroidRuntime(1366): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mad.tictactoepk/com.mad.tictactoepk.TicTacToe}: java.lang.ClassCastException: com.mad.tictactoepk.TicTacToe cannot be cast to android.view.View$OnClickListener
03-03 16:53:01.790: E/AndroidRuntime(1366): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-03 16:53:01.790: E/AndroidRuntime(1366): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-03 16:53:01.790: E/AndroidRuntime(1366): at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-03 16:53:01.790: E/AndroidRuntime(1366): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-03 16:53:01.790: E/AndroidRuntime(1366): at android.os.Handler.dispatchMessage(Handler.java:102)
03-03 16:53:01.790: E/AndroidRuntime(1366): at android.os.Looper.loop(Looper.java:136)
03-03 16:53:01.790: E/AndroidRuntime(1366): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-03 16:53:01.790: E/AndroidRuntime(1366): at java.lang.reflect.Method.invokeNative(Native Method)
03-03 16:53:01.790: E/AndroidRuntime(1366): at java.lang.reflect.Method.invoke(Method.java:515)
03-03 16:53:01.790: E/AndroidRuntime(1366): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-03 16:53:01.790: E/AndroidRuntime(1366): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-03 16:53:01.790: E/AndroidRuntime(1366): at dalvik.system.NativeStart.main(Native Method)
03-03 16:53:01.790: E/AndroidRuntime(1366): Caused by: java.lang.ClassCastException: com.mad.tictactoepk.TicTacToe cannot be cast to android.view.View$OnClickListener
03-03 16:53:01.790: E/AndroidRuntime(1366): at com.mad.tictactoepk.TicTacToe.onCreate(TicTacToe.java:46)
03-03 16:53:01.790: E/AndroidRuntime(1366): at android.app.Activity.performCreate(Activity.java:5231)
03-03 16:53:01.790: E/AndroidRuntime(1366): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-03 16:53:01.790: E/AndroidRuntime(1366): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-03 16:53:01.790: E/AndroidRuntime(1366): ... 11 more
I understand that I cannot start my activity, but no idea where the error is.
Here is my main java class
package com.mad.tictactoepk;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class TicTacToe extends Activity implements OnClickListener{
private Button gameGrid[][]=new Button[3][3];
private Button newGameButton;
private TextView messageTextView;
private int turn;// whos turn it is
private boolean gameOver;
private String message;
private SharedPreferences savedValues;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tic_tac_toe);
// get ref to widgets
gameGrid[0][0] = (Button) findViewById(R.id.square1);
gameGrid[0][1] = (Button) findViewById(R.id.square2);
gameGrid[0][2] = (Button) findViewById(R.id.square3);
gameGrid[1][0] = (Button) findViewById(R.id.square4);
gameGrid[1][1] = (Button) findViewById(R.id.square5);
gameGrid[1][2] = (Button) findViewById(R.id.square6);
gameGrid[2][0] = (Button) findViewById(R.id.square7);
gameGrid[2][1] = (Button) findViewById(R.id.square8);
gameGrid[2][2] = (Button) findViewById(R.id.square9);
newGameButton=(Button) findViewById(R.id.newGameButton);
messageTextView=(TextView)findViewById(R.id.messageTextView);
for(int i=0; i<gameGrid.length; i++){
for(int j=0; j<gameGrid[i].length; j++);
gameGrid[0][0].setOnClickListener((android.view.View.OnClickListener) this);
}
newGameButton.setOnClickListener((android.view.View.OnClickListener) this);
setStartingValues();
}
private void setStartingValues(){
turn=1;
gameOver=false;
message ="pLAYER x\'S TURN";
messageTextView.setText(R.string.messageLabel);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.tic_tac_toe, menu);
return true;
}
public void onClick(View v) {
switch(v.getId()){
case R.id.newGameButton:
startGame();
break;
default:
if(!gameOver){
Button b = (Button) v;
if(b.getText().equals("")){
if(turn % 2 !=0){
b.setText("X");
message = "Player O\'s turn";
}
else{
b.setText("O");
message = "Player X\'s turn";
}
turn++;
checkForGameOver();
}
else{
message = "That square is already taken";
}
}
messageTextView.setText("message");
}
}
private void checkForGameOver() {
// TODO Auto-generated method stub
/// check for win
for (int x = 0; x < 3; x++) {
if (!gameGrid[x][0].getText().equals("") &&
gameGrid[x][0].getText().equals(gameGrid[x][1].getText()) &&
gameGrid[x][1].getText().equals(gameGrid[x][2].getText())
) {
message = gameGrid[x][0].getText() + " wins!"; // message is the String displayed in messageTextView
gameOver = true; // can only play as long as this var is false
return; // exit you’ve found a winning position
}
}
}
private void startGame() {
// TODO Auto-generated method stub
}
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}
my xml layout
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/TableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".TicTacToe" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/square1"
android:layout_width="100dp"
android:layout_height="100dp"
android:textSize="30sp" />
<Button
android:id="@+id/square2"
android:layout_width="100dp"
android:layout_height="100dp"
android:textSize="30sp" />
<Button
android:id="@+id/square3"
android:layout_width="100dp"
android:layout_height="100dp"
android:textSize="30sp" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/square4"
android:layout_width="100dp"
android:layout_height="100dp"
android:textSize="30sp" />
<Button
android:id="@+id/square5"
android:layout_width="100dp"
android:layout_height="100dp"
android:textSize="30sp" />
<Button
android:id="@+id/square6"
android:layout_width="100dp"
android:layout_height="100dp"
android:textSize="30sp" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/square7"
android:layout_width="100dp"
android:layout_height="100dp"
android:textSize="30sp" />
<Button
android:id="@+id/square8"
android:layout_width="100dp"
android:layout_height="100dp"
android:textSize="30sp" />
<Button
android:id="@+id/square9"
android:layout_width="100dp"
android:layout_height="100dp"
android:textSize="30sp" />
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/messageTextView"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_gravity="center"
android:layout_span="3"
android:gravity="center"
android:text="@string/messageLabel"
android:textSize="20sp"
android:lines="2"
android:maxLines="2"
android:padding="10dp"/>
</TableRow>
<TableRow
android:id="@+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/newGameButton"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:text="@string/newGameButton"
android:layout_span="3"
android:textSize="20sp"/>
</TableRow>
</TableLayout>
and my manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mad.tictactoepk"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.mad.tictactoepk.TicTacToe"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I assume something wrong with my activity and OnClickListener? but I can't spot an error. Thanks in advance.
Upvotes: 1
Views: 1100
Reputation: 4964
1. delete (android.view.View.OnClickListener)
in on setOnClickListener
for(int i=0; i<gameGrid.length; i++){
for(int j=0; j<gameGrid[i].length; j++);
gameGrid[0][0].setOnClickListener(this); //change here
}
newGameButton.setOnClickListener( this); //change here
2. delete this method (at the end):
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
Upvotes: 3
Reputation: 13705
"TicTacToe cannot be cast to android.view.View$OnClickListener", you accidentally imported Dialog listener:
import android.content.DialogInterface.OnClickListener;
When you actually should have imported View.OnClickListener
import android.view.View.OnClickListener;
So, since the Activity is implementing the wrong listener, by the time the activity is created it just crashes because of a ClassCastException.
Just remove the first import and replace with the second one or do this in you activity class declaration(make sure you are not registering that click listener in a widget you need DialogInterface.OnClickListener):
public class TicTacToe extends Activity implements View.OnClickListener
Regards!
Upvotes: 6