1. Download our Official Android App: Forums for Android!

Apps Problem starting a new activity using explicit intent

Discussion in 'Android Development' started by Yathushan, Feb 19, 2011.

  1. Yathushan

    Yathushan Well-Known Member
    Thread Starter
    Rank:
    None
    Points:
    36
    Posts:
    205
    Joined:
    Jan 3, 2010

    Jan 3, 2010
    205
    7
    36
    London
    Hi all I am having some trouble starting up a new activity from my initial activity even though it is shown in code to be right. In practise, all that happens is that the button is clicked and everything stays the same. BTW this is only one way I have yet to implement a reverse way just relying on the back button.

    Here is the code I am currently using.

    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.transport.assistant"
    5.       android:versionCode="1"
    6.       android:versionName="1.0">
    7.  
    8.     <application android:icon="@drawable/icon"
    9.                  android:label="@string/app_name"
    10.                  android:enabled="true"
    11.                  android:debuggable="true">
    12.         <activity android:name="mainmenu"
    13.                   android:label="@string/app_name">
    14.             <intent-filter>
    15.                 <action android:name="android.intent.action.MAIN" />
    16.                 <category android:name="android.intent.category.LAUNCHER" />
    17.             </intent-filter>
    18.         </activity>
    19.         <activity android:name="underground"></activity>
    20.         <activity android:name="splashscreen"></activity>
    21.  
    22.     </application>
    23. </manifest>
    24.  
    mainmenu.java
    Code (Text):
    1.  
    2. package com.transport.assistant;
    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 mainmenu extends Activity {
    12.    
    13.     /** Called when activity is initially created. */
    14.     @Override
    15.     public void onCreate(Bundle savedInstanceState) {
    16.         super.onCreate(savedInstanceState);
    17.         setContentView(R.layout.main);
    18.        
    19.          // click-handlers for buttons
    20.         Button underground = (Button)findViewById(R.id.ButtonM1);
    21.         underground.setOnClickListener(new OnClickListener() {
    22.            
    23.             public void onClick(View v) {
    24.                 Intent explicitIntent = new
    25.                 Intent(mainmenu.this, underground.class);
    26.                 startActivity(explicitIntent);
    27.             }
    28.            
    29.         });
    30.     }
    31. }
    32.  
    mainmenu.xml
    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3.  
    4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    5.     android:orientation="vertical"
    6.     android:layout_width="fill_parent"
    7.     android:layout_height="fill_parent">
    8.    
    9.     <TextView
    10.     android:layout_width="wrap_content"
    11.     android:id="@+id/textView1"
    12.     android:text="@string/main_menu"
    13.     android:layout_height="wrap_content">
    14.     </TextView>
    15.    
    16.     <Button
    17.     android:id="@+id/ButtonM1"
    18.     android:layout_width="match_parent"
    19.     android:layout_height="wrap_content"
    20.     android:text="@string/Underground">
    21.     </Button>
    22.    
    23.     <Button
    24.     android:id="@+id/ButtonM2"
    25.     android:layout_width="match_parent"
    26.     android:layout_height="wrap_content"
    27.     android:text="@string/Bus">
    28.     </Button>
    29.    
    30.     <Button
    31.     android:id="@+id/ButtonM3"
    32.     android:layout_width="match_parent"
    33.     android:layout_height="wrap_content"
    34.     android:text="@string/oyster_locations">
    35.     </Button>
    36.    
    37.     <Button android:id="@+id/ButtonM4"
    38.     android:layout_width="match_parent"
    39.     android:layout_height="wrap_content"
    40.     android:text="@string/plan_journey">
    41.     </Button>
    42.    
    43.     <Button
    44.     android:id="@+id/ButtonM5"
    45.     android:layout_width="match_parent"
    46.     android:layout_height="wrap_content"
    47.     android:text="@string/Settings">
    48.     </Button>
    49.    
    50. </LinearLayout>
    51.  
    underground.java
    Code (Text):
    1.  
    2. package com.transport.assistant;
    3.  
    4. import android.app.Activity;
    5. import android.os.Bundle;
    6.  
    7.  
    8. public class underground extends Activity {
    9.    
    10.     /** Called when the activity is first created. */
    11.     @Override
    12.     public void onCreate(Bundle savedInstanceState) {
    13.         super.onCreate(savedInstanceState);
    14.         setContentView(R.layout.underground);
    15.  
    16.       }
    17.     }
    18.  
    If anyone could help me figure out what is going wrong here, it would be greatly appreciated.

    Thanks
     

    Advertisement

  2. idavis

    idavis Newbie
    Rank:
    None
    Points:
    16
    Posts:
    27
    Joined:
    Feb 11, 2011

    Feb 11, 2011
    27
    4
    16
    Yathushan,
    Maybe try changing the underground class to this:

    public class underground extends mainmenu {

    Also, not sure if this is just a typo, but you use the layout:
    setContentView(R.layout.main);
    But you list the layout as mainmenu.xml

    Since you don't mention that as failing I would guess it's just a typo in the post.
     
    Yathushan likes this.
  3. Yathushan

    Yathushan Well-Known Member
    Thread Starter
    Rank:
    None
    Points:
    36
    Posts:
    205
    Joined:
    Jan 3, 2010

    Jan 3, 2010
    205
    7
    36
    London
    That seemed to have done the trick.

    Thank you!!
     
  4. cr5315

    cr5315 Android Enthusiast
    Rank:
    None
    Points:
    68
    Posts:
    434
    Joined:
    Jul 23, 2010

    Jul 23, 2010
    434
    158
    68
    Narnia
    I'm having the same problem, but with an options menu button. I've tried a lot of different ways to launch an activity, and none have worked. In the activity that is being launched, I have it extending the first activity, but it still doesn't work. My code is basically like Yathushan's, except that the Intent is in a menu.
     
  5. idavis

    idavis Newbie
    Rank:
    None
    Points:
    16
    Posts:
    27
    Joined:
    Feb 11, 2011

    Feb 11, 2011
    27
    4
    16
    Yathushan,
    Glad that worked!

    norton5315,
    Do you have the class identified as an activity in the Manifest?
     
  6. Yathushan

    Yathushan Well-Known Member
    Thread Starter
    Rank:
    None
    Points:
    36
    Posts:
    205
    Joined:
    Jan 3, 2010

    Jan 3, 2010
    205
    7
    36
    London
    Just wondering is better to keep all the strings in one xml file or in multiple?
     
  7. Yathushan

    Yathushan Well-Known Member
    Thread Starter
    Rank:
    None
    Points:
    36
    Posts:
    205
    Joined:
    Jan 3, 2010

    Jan 3, 2010
    205
    7
    36
    London
    I was just wondering how do I go about using multiple onclicklisteners? Because right now I used the same setOnClickListener code but replaced underground with something else.

    It says:
     
  8. idavis

    idavis Newbie
    Rank:
    None
    Points:
    16
    Posts:
    27
    Joined:
    Feb 11, 2011

    Feb 11, 2011
    27
    4
    16
    You just set up two listeners one for each button:

    Button underground = (Button)findViewById(R.id.ButtonM1); underground.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent explicitIntent = new Intent(mainmenu.this, underground.class); startActivity(explicitIntent); } });

    Button aboveground = (Button)findViewById(R.id.ButtonM2);
    aboveground.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent explicitIntent = new Intent(mainmenu.this, underground.class); startActivity(explicitIntent); } });
     

Share This Page

Loading...