title: Android - java.lang.RuntimeException: Unable to start activity
body: <p>I got this error </p>
  
  <pre><code>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
  </code></pre>
  
  <p>I understand that I cannot start my activity, but no idea where the error is.</p>
  
  <p>Here is my main java class</p>
  
  <pre><code>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&lt;gameGrid.length; i++){
                      for(int j=0; j&lt;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 &lt; 3; x++) {
              if (!gameGrid[x][0].getText().equals("") &amp;&amp;                                                                       
                                  gameGrid[x][0].getText().equals(gameGrid[x][1].getText()) &amp;&amp;
                                  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
  
      }
  
  
  
  }
  </code></pre>
  
  <p>my xml layout</p>
  
  <pre><code>&lt;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" &gt;
  
      &lt;TableRow
          android:id="@+id/tableRow1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" &gt;
  
          &lt;Button
              android:id="@+id/square1"
              android:layout_width="100dp"
              android:layout_height="100dp"
              android:textSize="30sp" /&gt;
  
          &lt;Button
              android:id="@+id/square2"
              android:layout_width="100dp"
              android:layout_height="100dp"
              android:textSize="30sp" /&gt;
  
          &lt;Button
              android:id="@+id/square3"
              android:layout_width="100dp"
              android:layout_height="100dp"
              android:textSize="30sp" /&gt;
      &lt;/TableRow&gt;
  
      &lt;TableRow
          android:id="@+id/tableRow2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" &gt;
  
          &lt;Button
              android:id="@+id/square4"
              android:layout_width="100dp"
              android:layout_height="100dp"
              android:textSize="30sp" /&gt;
  
          &lt;Button
              android:id="@+id/square5"
              android:layout_width="100dp"
              android:layout_height="100dp"
              android:textSize="30sp" /&gt;
  
          &lt;Button
              android:id="@+id/square6"
              android:layout_width="100dp"
              android:layout_height="100dp"
              android:textSize="30sp" /&gt;
      &lt;/TableRow&gt;
  
      &lt;TableRow
          android:id="@+id/tableRow3"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" &gt;
  
          &lt;Button
              android:id="@+id/square7"
              android:layout_width="100dp"
              android:layout_height="100dp"
              android:textSize="30sp" /&gt;
  
          &lt;Button
              android:id="@+id/square8"
              android:layout_width="100dp"
              android:layout_height="100dp"
              android:textSize="30sp" /&gt;
  
          &lt;Button
              android:id="@+id/square9"
              android:layout_width="100dp"
              android:layout_height="100dp"
              android:textSize="30sp" /&gt;
      &lt;/TableRow&gt;
  
      &lt;TableRow
          android:id="@+id/tableRow4"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" &gt;
  
          &lt;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"/&gt;
  
      &lt;/TableRow&gt;
  
      &lt;TableRow
          android:id="@+id/tableRow5"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" &gt;
  
          &lt;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"/&gt;
      &lt;/TableRow&gt;
  
  &lt;/TableLayout&gt;
  </code></pre>
  
  <p>and my manifest file</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.mad.tictactoepk"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-sdk
          android:minSdkVersion="8"
          android:targetSdkVersion="18" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
          &lt;activity
              android:name="com.mad.tictactoepk.TicTacToe"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>I assume something wrong with my activity and OnClickListener? but I can't spot an error. Thanks in advance. </p>