0

I'm writing a sudoku app. I want to make a main view and puzzleview, but when I was done the second activity, it crashed.

This is onCreate function of my Main activity:

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);
             }
        });
    }

This is onCreate of my Second Activity:

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

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

This 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

If there's any information not enough, please tell me, thanks.

1
  • dmon is right about the location of this LogCat's error. But I want to point out something else: you should never use main = new Main(); to create an Activity, this will cause a lot of errors. Commented Dec 1, 2012 at 20:48

1 Answer 1

3

The problem is in not in any of the bits you posted, but in PuzzleView, line 26:

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

You're trying to cast what you're passing in, a Main instance, into a (Sudoku).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.