• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Starting new activity using Intents

rick99gtp

Newbie
Hello.

I have 2 classes, Screen1 and Screen2. On Screen1, I have a button which when pressed should load Screen2. I'm having such a hard time figuring it out. I'm getting a force close button in the Eclipse emulator when I click on the 'New Game' button.

Here is the relevant code. If you can spot what's missing or what's wrong, that'd really be great.

Screen1.java:
Code:
package com.androidgame;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Screen1 extends Activity {
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        Button button1 = (Button) findViewById(R.id.btnNewGame);
        button1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // Perform action on click
            	Intent intent = new Intent(Screen1.this, Screen2.class);
            	Screen1.this.startActivity(intent);
            }
        });

    }
}

Screen1.xml
Code:
<Button android:id="@+id/btnNewGame"
     android:typeface="serif"
     android:layout_width="200dp"
     android:layout_height="wrap_content"
     android:text="New Game"
     android:layout_gravity="center"
     android:layout_margin="10dp" />

Screen2.java
Code:
package com.androidgame;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Screen2 extends Activity {
	@Override
    public void onCreate(Bundle savedInstanceState)
   {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.Screen2);
    }
}

AndroidManifest.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.androidgame"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:label="@string/app_name" android:name="Screen1" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <activity
        	android:name=".Screen2"
        	android:label="@string/app_name">
        </activity>

    </application>
    <uses-sdk android:minSdkVersion="4" />

</manifest>
 
Wow. After spending hours trying to figure it out...it turns out I had used the same button names between the two apps....after changing them, everything worked perfectly. If you read this, sorry to waste your time. ;)
 
Back
Top Bottom