Starting new activity using Intents


Last Updated:

  1. rick99gtp

    rick99gtp Member This Topic's Starter

    Joined:
    Aug 6, 2010
    Messages:
    12
    Likes Received:
    0
    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 (Text):
    1.  
    2. package com.androidgame;
    3.  
    4. import android.app.Activity;
    5. import android.content.Intent;
    6. import android.os.Bundle;
    7. import android.view.View;
    8. import android.view.View.OnClickListener;
    9. import android.widget.Button;
    10.  
    11. public class Screen1 extends Activity {
    12.     @Override
    13.     public void onCreate(Bundle savedInstanceState) {
    14.         super.onCreate(savedInstanceState);
    15.  
    16.         setContentView(R.layout.main);
    17.  
    18.         Button button1 = (Button) findViewById(R.id.btnNewGame);
    19.         button1.setOnClickListener(new OnClickListener() {
    20.             public void onClick(View v) {
    21.                 // Perform action on click
    22.                 Intent intent = new Intent(Screen1.this, Screen2.class);
    23.                 Screen1.this.startActivity(intent);
    24.             }
    25.         });
    26.  
    27.     }
    28. }
    29.  
    Screen1.xml
    Code (Text):
    1.  
    2. <Button android:id="@+id/btnNewGame"
    3.      android:typeface="serif"
    4.      android:layout_width="200dp"
    5.      android:layout_height="wrap_content"
    6.      android:text="New Game"
    7.      android:layout_gravity="center"
    8.      android:layout_margin="10dp" />
    9.  
    Screen2.java
    Code (Text):
    1.  
    2. package com.androidgame;
    3.  
    4. import android.app.Activity;
    5. import android.os.Bundle;
    6. import android.view.View;
    7. import android.widget.Button;
    8.  
    9. public class Screen2 extends Activity {
    10.     @Override
    11.     public void onCreate(Bundle savedInstanceState)
    12.    {
    13.         super.onCreate(savedInstanceState);
    14.  
    15.         setContentView(R.layout.Screen2);
    16.     }
    17. }
    18.  
    AndroidManifest.xml
    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    4.       package="com.androidgame"
    5.       android:versionCode="1"
    6.       android:versionName="1.0">
    7.     <application android:icon="@drawable/icon" android:label="@string/app_name">
    8.         <activity android:label="@string/app_name" android:name="Screen1" >
    9.             <intent-filter>
    10.                 <action android:name="android.intent.action.MAIN" />
    11.                 <category android:name="android.intent.category.LAUNCHER" />
    12.             </intent-filter>
    13.         </activity>
    14.        
    15.         <activity
    16.             android:name=".Screen2"
    17.             android:label="@string/app_name">
    18.         </activity>
    19.  
    20.     </application>
    21.     <uses-sdk android:minSdkVersion="4" />
    22.  
    23. </manifest>
    24.  
     

    Advertisement
  2. rick99gtp

    rick99gtp Member This Topic's Starter

    Joined:
    Aug 6, 2010
    Messages:
    12
    Likes Received:
    0
    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. ;)
     

Share This Page

Loading...