点击欢迎页面上的按钮后如何开始游戏循环?

时间:2016-06-12 06:36:01

标签: java android

我正在Android Studio中制作Android游戏。

我首先编写了游戏循环代码。然后我创建了一个欢迎页面/开始页面并将其添加到onclick事件中。

但是当我运行应用程序并单击“开始”按钮时,应用程序会突然停止。这是Java,XML和清单文件。我错过了什么吗?

我的主要活动(Game.java):

public class Game extends ActionBarActivity{
private static Button button_sbm;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //turn off title
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    //set to full screen
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_game);
   onClickButtonListener();
}

 public void onClickButtonListener(){
    button_sbm=(Button)findViewById(R.id.button);
    button_sbm.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent("com.project.androidgame.GAME");
                    startActivity(intent);
                }
            }
    );
}
/*public void startGame(){
    Intent intent=new Intent(Splash.this, Game.class);
    startActivity(intent);

}*/

activity_game.xml

 <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="START"
    android:textSize="50sp"
    android:id="@+id/button"
    android:layout_centerVertical="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="How to play"
    android:textSize="24sp"
    android:id="@+id/button2"
    android:layout_below="@+id/button"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

起始页面的活动(Splash.java):

public class Splash extends Activity {
    MediaPlayer ourSong;
    @Override
    protected void onCreate(Bundle ILoveFootball) {
        super.onCreate(ILoveFootball);
        setContentView(R.layout.splash);
        ourSong=MediaPlayer.create(Splash.this,R.raw.splashsound);
        ourSong.start();
        Thread timer=new Thread(){
          public void run()
          {
              try{
                    sleep(5000);
              }catch(InterruptedException e){
                    e.printStackTrace();
              }finally{
                  Intent openStartingPoint=new Intent("com.project.androidgame.GAME");
                  startActivity(openStartingPoint);
              }
          }
        };
        timer.start();
    }

    @Override
    protected void onPause() {
        super.onPause();
        ourSong.release();
        finish();
    }
}

splash.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/splash_back">

清单

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:screenOrientation="landscape"
        android:name=".Splash" android:label="AndroidGame">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:screenOrientation="landscape"
        android:name=".Game" android:label="AndroidGame">
        <intent-filter>
            <action android:name="com.project.androidgame.GAME" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

1 个答案:

答案 0 :(得分:1)

按照此步骤编辑代码

在清单中,删除第二个活动中的intent-filter,intent-filter仅用于识别启动活动。改变这个

<activity android:screenOrientation="landscape"
    android:name=".Game" android:label="AndroidGame">
    <intent-filter>
        <action android:name="com.project.androidgame.GAME" />

        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

到此

<activity android:screenOrientation="landscape"
    android:name=".Game" android:label="AndroidGame">
</activity>

启动活动的最佳方式是当前内容到下一个班级一个例子

Intent intent=new Intent(Splash.this, Game.class);
                startActivity(intent);