重定向到另一个活动

时间:2015-03-11 21:59:14

标签: android redirect android-activity menu options

我正在尝试通过我的选项菜单重定向到另一个活动,但是,当选择该项时,它会崩溃。任何人都可以看到错误吗?

这是GameCentral.java

public class GameCentral extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gamecentral);
    }

    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.gamecentralmenu, menu);
        return true;

    }

    public boolean onOptionsItemSelected (MenuItem item)
    {
        switch(item.getItemId()){
        case R.id.GuessingGame:
            Intent intent = new Intent(GameCentral.this, MainActivity.class);
            startActivity(intent);


        case R.id.Example1:
            Toast.makeText(this, "Example 1", Toast.LENGTH_SHORT);
        case R.id.Example2:
            Toast.makeText(this, "Example 2", Toast.LENGTH_SHORT);
        case R.id.Example3:
            Toast.makeText(this, "Example 3", Toast.LENGTH_SHORT);
        }

        return true;
    }



}

这是Logcat:

03-11 17:46:50.353: E/AndroidRuntime(1522): FATAL EXCEPTION: main
03-11 17:46:50.353: E/AndroidRuntime(1522): Process: com.example.c3438525Task3, PID: 1522
03-11 17:46:50.353: E/AndroidRuntime(1522): android.util.SuperNotCalledException: Activity {com.example.c3438525Task3/com.example.c3438525Task3.GameCentral} did not call through to super.onCreate()

这是MainActivity.java

public class MainActivity extends Activity {
    int i;
    int min;
    int max;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        min = 1;
        max = 20;
        Random r = new Random();
        i = r.nextInt(max - min + 1) + min;


    }
    public void guessingGame (View v)
    {


        EditText guess = (EditText) findViewById(R.id.guess);
        TextView finalMessage = (TextView) findViewById(R.id.finalMessage);
        TextView numGuessesLeft = (TextView) findViewById(R.id.guessesLeft);
        TextView compGuess = (TextView) findViewById(R.id.tvCompGuess);
        TextView restartCounter = (TextView) findViewById(R.id.restartCount);
        TextView guessCorrect = (TextView) findViewById(R.id.guessesCorrect);
        TextView yourGuess = (TextView) findViewById(R.id.userGuess);
        Button start = (Button) findViewById(R.id.start);
        compGuess.setVisibility(View.INVISIBLE);
        int guessesCorrect = Integer.parseInt(guessCorrect.getText().toString());
        int guessValue = Integer.parseInt(guess.getText().toString());
        start.setText("Start");
        int numGuessesValue = Integer.parseInt(numGuessesLeft.getText().toString());
        int restartCount = Integer.parseInt(restartCounter.getText().toString());

        if (guessValue > 20)
        {
            finalMessage.setText("Error");
            guess.getText().clear();
        }
        else if (guessValue < 1)
        {
            finalMessage.setText("Error");
            guess.getText().clear();
        }
        else 
        {

            numGuessesValue -= 1;
            numGuessesLeft.setText(Integer.toString(numGuessesValue));

                if (numGuessesValue > 0)
                    {
                    int computerNumber = i;
                    compGuess.setText(Integer.toString(i));
                    yourGuess.setText(Integer.toString(guessValue));

                        if (computerNumber > guessValue)
                        {
                            finalMessage.setText("Too low!");

                            finalMessage.setTextColor(Color.BLUE);
                        }
                        else if (computerNumber < guessValue)
                        {
                        finalMessage.setText("Too high!");


                        finalMessage.setTextColor(Color.RED);
                        }
                        else if (computerNumber == guessValue)
                            {
                            compGuess.setVisibility(View.VISIBLE);
                            finalMessage.setText("Good Guess!");
                            numGuessesValue += 5;
                            numGuessesLeft.setText("5");
                            finalMessage.setTextColor(Color.GREEN);
                            guessesCorrect += 1;
                            guessCorrect.setText(Integer.toString(guessesCorrect));
                            Random r = new Random();
                            i = r.nextInt(max - min + 1) + min;
                            }

            }

            else if (numGuessesValue == 0)
            {

                guess.setText("10");    
                start.setText("Restart");
                finalMessage.setText("You are out of guesses! - Enter a new number to start again");
                compGuess.setVisibility(View.VISIBLE);
                numGuessesValue += 5;
                numGuessesLeft.setText("5");
                restartCount += 1;
                restartCounter.setText(Integer.toString(restartCount));
                guess.getText().clear();                
                Random r = new Random();
                i = r.nextInt(max - min + 1) + min;


            }



        }

    }

}

2 个答案:

答案 0 :(得分:0)

检查Logcat的最后一行,您似乎没有在super.onCreate()的{​​{1}}方法中调用onCreate()。 并确保将MainActivity添加到清单中,如下所示:

MainActivity.java

答案 1 :(得分:0)

要重定向另一个活动,只需创建一个班级名称为(通用班级)并将以下代码粘贴到活动中 不仅仅是写

->类名称(通用).redirectActivity(活动名称)

private fun redirectActivity(activity: Activity) {
        Handler().postDelayed({
            startActivity(Intent(this, activity::class.java))
        }, 500)
    }