使用按钮Android启动活动

Ahm*_*mad 10 android android-activity

我有一个问题.我想用Button打开一个Activity但它一直崩溃.所以我创建了2个Classes和一个Button.但它一直在崩溃.

  1. 类是activity_home class(),第二个是schedule_act()类.

activity_home类:

    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);


                }
            });
        }





    }  
Run Code Online (Sandbox Code Playgroud)

schedule_act类:

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);
        }



    }
Run Code Online (Sandbox Code Playgroud)

Android清单:

    <?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>  
Run Code Online (Sandbox Code Playgroud)

非常感谢你.

Chr*_*son 18

意图区分大小写.更改

"my.action.bat.schedule_act"
Run Code Online (Sandbox Code Playgroud)

"my.action.bat.SCHEDULE_ACT"
Run Code Online (Sandbox Code Playgroud)

此外,除非你真的需要使用意图,否则我会开始你的活动

startActivity(new Intent(this, schedule_act.class));
Run Code Online (Sandbox Code Playgroud)

this一个Context子类