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

Apps Problem starting a new activity using explicit intent

Yathushan

Well-Known Member
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:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.transport.assistant"
      android:versionCode="1"
      android:versionName="1.0">

    <application android:icon="@drawable/icon"
    			 android:label="@string/app_name"
    			 android:enabled="true"
    			 android:debuggable="true">
        <activity android:name="mainmenu"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
		<activity android:name="underground"></activity>
		<activity android:name="splashscreen"></activity>

    </application>
</manifest>

mainmenu.java
Code:
package com.transport.assistant;

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 mainmenu extends Activity {
	
	/** Called when activity is initially created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		 // click-handlers for buttons
		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);
			}
			
		});
	}
}

mainmenu.xml
Code:
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    
    <TextView
    android:layout_width="wrap_content"
    android:id="@+id/textView1"
    android:text="@string/main_menu"
    android:layout_height="wrap_content">
    </TextView>
    
    <Button
    android:id="@+id/ButtonM1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/Underground">
    </Button>
    
    <Button
    android:id="@+id/ButtonM2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/Bus">
    </Button>
    
    <Button
    android:id="@+id/ButtonM3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/oyster_locations">
    </Button>
    
    <Button android:id="@+id/ButtonM4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/plan_journey">
    </Button>
    
    <Button
    android:id="@+id/ButtonM5"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/Settings">
    </Button>
    
</LinearLayout>

underground.java
Code:
package com.transport.assistant;

import android.app.Activity;
import android.os.Bundle;


public class underground extends Activity {
   
	/** Called when the activity is first created. */
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.underground);

      }
    }

If anyone could help me figure out what is going wrong here, it would be greatly appreciated.

Thanks
 
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.
 
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.
 
Yathushan,
Glad that worked!

norton5315,
Do you have the class identified as an activity in the Manifest?
 
Yathushan,
Glad that worked!

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:
The type new View.OnClickListener(){} must implement the inherited abstract method View.OnClickListener.onClick(View)
 
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); } });
 
Back
Top Bottom