java.lang.RuntimeException:无法启动活动ComponentInfo java.lang.ClassCastException

时间:2012-12-01 20:36:23

标签: android

我正在写一个数独应用。我想制作一个主视图和益智视图,但是当我完成第二个活动时,它就崩溃了。

这是我主要活动的onCreate功能:

public class Main extends Activity {
    private Button button1, button3,button4;
    PuzzleView puzzleview;
    Score score;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        puzzleview = new PuzzleView(this);
        score = new Score(this);

        setContentView(R.layout.activity_main);

        button1 = (Button)findViewById(R.id.button1);
        button1.setOnClickListener(new Button.OnClickListener(){
             public void onClick(View v) {
                setContentView(puzzleview);
             }
        });

        button4 = (Button)findViewById(R.id.button4);
        button4.setOnClickListener(new Button.OnClickListener(){
             public void onClick(View v) {
                setContentView(score);
             }
        });
    }

这是我第二次活动的创作:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        main = new Main();
        puzzleview = new PuzzleView(this);
        puzzleview.requestFocus();
    }

此LogCat:

12-01 20:17:56.291: E/AndroidRuntime(1345): FATAL EXCEPTION: main
12-01 20:17:56.291: E/AndroidRuntime(1345): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sudoku/com.example.sudoku.Main}: java.lang.ClassCastException: com.example.sudoku.Main cannot be cast to com.example.sudoku.Sudoku
12-01 20:17:56.291: E/AndroidRuntime(1345):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
12-01 20:17:56.291: E/AndroidRuntime(1345):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-01 20:17:56.291: E/AndroidRuntime(1345):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-01 20:17:56.291: E/AndroidRuntime(1345):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-01 20:17:56.291: E/AndroidRuntime(1345):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-01 20:17:56.291: E/AndroidRuntime(1345):     at android.os.Looper.loop(Looper.java:137)
12-01 20:17:56.291: E/AndroidRuntime(1345):     at android.app.ActivityThread.main(ActivityThread.java:4745)
12-01 20:17:56.291: E/AndroidRuntime(1345):     at java.lang.reflect.Method.invokeNative(Native Method)
12-01 20:17:56.291: E/AndroidRuntime(1345):     at java.lang.reflect.Method.invoke(Method.java:511)
12-01 20:17:56.291: E/AndroidRuntime(1345):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-01 20:17:56.291: E/AndroidRuntime(1345):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-01 20:17:56.291: E/AndroidRuntime(1345):     at dalvik.system.NativeStart.main(Native Method)
12-01 20:17:56.291: E/AndroidRuntime(1345): Caused by: java.lang.ClassCastException: com.example.sudoku.Main cannot be cast to com.example.sudoku.Sudoku
12-01 20:17:56.291: E/AndroidRuntime(1345):     at com.example.sudoku.PuzzleView.<init>(PuzzleView.java:26)
12-01 20:17:56.291: E/AndroidRuntime(1345):     at com.example.sudoku.Main.onCreate(Main.java:18)
12-01 20:17:56.291: E/AndroidRuntime(1345):     at android.app.Activity.performCreate(Activity.java:5008)
12-01 20:17:56.291: E/AndroidRuntime(1345):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-01 20:17:56.291: E/AndroidRuntime(1345):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
12-01 20:17:56.291: E/AndroidRuntime(1345):     ... 11 more

如果有任何信息不够,请告诉我,谢谢。

1 个答案:

答案 0 :(得分:3)

问题出在你发布的任何比特中,但在PuzzleView第26行:

at com.example.sudoku.PuzzleView.<init>(PuzzleView.java:26)

您正在尝试将您传入的内容Main实例转换为(Sudoku)