10

Ive got a problem. I want to open an Activity with a Button but it crashes all the time. So I created 2 Classes and A Button. But it keeps crashing.

  1. Class is activity_home class() and second is schedule_act() class.

activity_home class:

    package my.action.bat;

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

    public class activity_home extends Activity {

        private Button ScheduleBtn;

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

             ScheduleBtn = (Button) findViewById(R.id.home_btn_schedule);

            ScheduleBtn.setOnClickListener(new View.OnClickListener() {

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


                    Intent myIntent = new Intent("my.action.bat.schedule_act");
                    startActivity(myIntent);


                }
            });
        }





    }  

schedule_act class:

package my.action.bat;

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

    public class schedule_act extends Activity {

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

            setContentView(R.layout.schedule_layout);
        }



    }

Android Manifest:

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

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

        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name" >
            <activity
                android:label="@string/app_name"
                android:name=".activity_home" >
                <intent-filter >
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>

            <activity
                android:label="@string/app_name"
                android:name=".schedule_act" >
                <intent-filter >
                    <action android:name="my.action.bat.SCHEDULE_ACT" />

                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity>
        </application>

    </manifest>  

Thank you very much.

1
  • Whats the exception being thrown?? Commented Mar 9, 2012 at 0:17

5 Answers 5

18

Intents are case sensitive. Change

"my.action.bat.schedule_act"

To

"my.action.bat.SCHEDULE_ACT"

Also, unless you really need to use an intent, I would start your activity like so

startActivity(new Intent(this, schedule_act.class));

Where this is a Context subclass

Sign up to request clarification or add additional context in comments.

Comments

3

Try this

localIntent = new Intent(activity_home.this, schedule_act.class);
    activity_home.this.startActivity(localIntent);

Comments

2

Try changing the line

 Intent myIntent = new Intent("my.action.bat.schedule_act");

To

 Intent myIntent = new Intent(v.getContext(), schedule_act.class);

And see if that helps.

See here for more info.

Comments

2

You can change this line

Intent myIntent = new Intent("my.action.bat.schedule_act"); startActivity(myIntent);

To something like this

Intent intent = new Intent ("Your context", "Your activity to launch"); startActivity(intent);

Remember always especify a context and a activity.

Comments

1

You have to add all the activity classes to the manifest file!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.