简体   繁体   中英

Switch Screens in Android not working

I have a "Play Now" button for a simple android game. When I click the button it calls start , but it doesn't do anything. Here is start() :

public void start(View view) {
    Intent myIntent = new Intent(this, Game.class);
    startActivity(myIntent);
}

and Game.java:

public class Game extends MainActivity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);
        Intent intent = new Intent();
        setResult(RESULT_OK, intent);
        finish();
    }
}

Also, I didn't forget to put it into the manifest

<activity android:name=".Game"></activity>

I'm new to android and this is all very confusing. I tried putting an intent filter although I probably did it wrong. I looked at this How to switch between screens? but it didn't work for me.

You are finishing the activity just when you create it (onCreate). Try deleting or commenting finish(); and good luck!

remove following lines, we use them with startActivityForResult , after removing it should work other than this everything is fine

Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();

Actually,your start function is working fine.But the problem is with onCreate() method in Game activity.You are calling finish() method in this which is killing the activity.Get rid of this method and then check.One thing more,I don't understand what is the purpose of setResult in your context.It is actually used for startActivityForResult() method.Refer to this link for further information: https://developer.android.com/training/basics/intents/result.html

  public class Game extends MainActivity {
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game);
    //Intent intent = new Intent(); 
    //setResult(RESULT_OK, intent);
    //finish();
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM