2014-03-03 134 views
1

我得到这个错误Android的 - 了java.lang.RuntimeException:无法启动活动

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 

我明白,我不能启动我的活动,但不知道哪里出错。

这是我主要的Java类

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 

    } 



} 

我的XML布局

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

和我的清单文件

<?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> 

我认为有毛病我的活动和OnClickListener?但我不能发现错误。提前致谢。

回答

6

“井字游戏不能转换到android.view.View $ OnClickListener”,你不小心输入对话框听众:

进口android.content.DialogInterface.OnClickListener;

当你真正应该进口View.OnClickListener

进口android.view.View.OnClickListener;

因此,由于Activity正在实现错误的侦听器,因此在创建活动时,它只是因ClassCastException而崩溃。

只是删除了第一进口和与第二个替代或你做这个活动类声明(确保你不登记即点击监听器,一个小部件,你需要DialogInterface.OnClickListener):

public class TicTacToe extends Activity implements View.OnClickListener 

问候!

+0

工程!谢谢。 –

+0

优秀的,不要忘记标记为正确的进一步用户相同的问题,以注意到它的工作... –

3

1.在删除(android.view.View.OnClickListener)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 

。删除此方法(最后):

@Override 
    public void onClick(DialogInterface dialog, int which) { 
     // TODO Auto-generated method stub 

    } 
相关问题