Dexto
Dexto

Reputation: 1201

Switching between layout in android

  public class menu extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity_page);
    {
    Button but1 = (Button)findViewById(R.id.imageButton4);
    but1.setOnClickListener(new View.OnClickListener() {


     @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        startActivity(new Intent("com.indore.indoreindicator.Busone"));

        }
    });
    }
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
}


   }

I have this line of code from my menu class

busone class -

  package com.indore.indoreindicator;

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

    public class Busone extends Activity {

   @Override
     protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.busmenu);
    }



    }

Android manifest file

   <?xml version="1.0" encoding="utf-8"?>
   <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.indore.indoreindicator"
    android:versionCode="1"
    android:versionName="1.0" >

     <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

    <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >


    <activity
        android:name="com.indore.indoreindicator.MainActivityPage"
        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=".menu"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.Menu" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

    <activity
        android:name=".Busone"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.indore.indoreindicaotr.Busone" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>


   </application>

   </manifest>

Main activity page -

    <ImageButton
    android:id="@+id/imageButton4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginTop="47dp"
    android:layout_toLeftOf="@+id/TextView05"
    android:src="@drawable/bus"
    tools:ignore="ContentDescription" />

My application starts but fails after the splash screen, unable to move on first page. but if I comment the button coding part the applcation moves to the menu page.

Why cant I go from the my menu page to the 2nd page named bus menu?

Upvotes: 0

Views: 1786

Answers (4)

Kosh
Kosh

Reputation: 6334

if you just need to switch between layouts then simply add on your setOnClickListener

setContentView(R.layout.whatevername);

and then simply add after this , your layout stuff like imageview , button etc . do that only if you need to add another onClick for that layout specific child! and if you like to switch between activity "Classs" then

Intent intent = new Intent(getApplication(), SecondClass.class);
startActivity(intent);

and don't forget to add your other activity in android manifest

<activity android:name=".SecondClass"/> 

base on your CODE

see the edits !!!

  public class menu extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity_page);
    **{** // why this is here ?????
    Button but1 = (Button)findViewById(R.id.imageButton4);
    but1.setOnClickListener(new View.OnClickListener() {


     @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        startActivity(new Intent("com.indore.indoreindicator.Busone"));

        }
    });
    **}** // why this is here ????
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
}


   }

Upvotes: 1

Raymond P
Raymond P

Reputation: 752

I don't understand what you're trying to explain, but what I do see is you are using an image button in your xml layout and using a normal button in the menu class. No need to use Image Button any way, just use android:background to set the image on a "normal" button.

Upvotes: 0

jcw
jcw

Reputation: 5162

You should try using

startActivity(new Intent(getApplicationContext(), Busone.class);

instead of

startActivity(new Intent("com.indore.indoreindicator.Busone"));

Upvotes: 2

Sean
Sean

Reputation: 5266

Try this:

startActivity(new Intent(menu.this, Busone.class));

Upvotes: 0

Related Questions