java - 安卓 - java.lang.RuntimeException : Unable to start activity

标签 java android

我遇到了这个错误

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

我知道我无法开始我的 Activity ,但不知道错误在哪里。

这是我的主要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>

和我的 list 文件

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

我假设我的 Activity 和 OnClickListener 有问题?但我无法发现错误。提前致谢。

最佳答案

“TicTacToe 无法转换为 android.view.View$OnClickListener”,您不小心导入了对话框监听器:

import android.content.DialogInterface.OnClickListener;

当你实际上应该导入 View.OnClickListener

import android.view.View.OnClickListener;

因此,由于 Activity 实现了错误的监听器,因此在创建 Activity 时它会因为 ClassCastException 而崩溃。

只需删除第一个导入并替换为第二个导入,或者在您的 Activity 类声明中执行此操作(确保您没有在需要 DialogInterface.OnClickListener 的小部件中注册该点击监听器):

public class TicTacToe extends Activity implements View.OnClickListener

问候!

关于java - 安卓 - java.lang.RuntimeException : Unable to start activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22158500/

相关文章:

java - 纯Java API中的意外非空类型

java - 来自 Hazelcast 客户端的 AuthenticationException

java - 我可以将自定义解串器应用到 GSON 的另一个自定义解串器中吗

android - firebaseAppDistribution with Github Actions throwing error "Missing app id",即使使用谷歌服务插件集

java - Android - ProgressDialog.show() 和 ProgressDialog.show() 有什么区别?

java - RecyclerView fragment 不出现

java - 消息包间歇性地无法正确渲染

java - Hibernate - 与列名的一对一映射

android - 按钮不引起 onClick 事件

Android Studio Gradle 错误 - 协议(protocol)系列不可用