2014-03-03 135 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 

    } 
相關問題