简体   繁体   English

Android中的切换屏幕不起作用

[英]Switch Screens in Android not working

I have a "Play Now" button for a simple android game. 我有一个简单的Android游戏的“立即播放”按钮。 When I click the button it calls start , but it doesn't do anything. 当我单击按钮时,它将调用start ,但是它什么也没做。 Here is start() : 这是start()

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

and Game.java: 和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. 我是android新手,这一切都很令人困惑。 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). 您仅在创建活动(onCreate)时便完成了该活动。 Try deleting or commenting finish(); 尝试删除或评论finish(); and good luck! 还有祝你好运!

remove following lines, we use them with startActivityForResult , after removing it should work other than this everything is fine 删除以下几行,我们将它们与startActivityForResult一起使用,删除后它应该可以工作,除此之外,其他一切都很好

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 实际上,您的启动函数运行正常。但是问题出在Game活动中的onCreate()方法上。您正在其中调用finish()方法,这会杀死该活动。摆脱该方法,然后进行检查。还有一点,我不了解您的上下文中setResult的用途,它实际上用于startActivityForResult()方法,请参阅此链接以获取更多信息: 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();
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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