使用按钮启动Android Activity

10

我遇到了一个问题。我想用按钮打开一个Activity,但是它总是崩溃。所以我创建了两个类和一个按钮,但它仍然会崩溃。

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

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 类:

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清单文件:

    <?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>  

非常感谢。


抛出了什么异常? - Chris Thompson
5个回答

18

意图(Intents)是区分大小写的。更改

"my.action.bat.schedule_act"

To

"my.action.bat.SCHEDULE_ACT"

此外,除非你真正需要使用intent,否则我会这样启动你的activity。

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

this 是一个 Context 子类


3

试试这个

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

2
尝试更改这一行。
 Intent myIntent = new Intent("my.action.bat.schedule_act");

To

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

看看这是否有所帮助。

更多信息请参见此处


2

你可以修改这行代码

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

改为以下代码

Intent intent = new Intent ("你的上下文", "你要启动的活动"); startActivity(intent);

记得始终指定上下文和活动。


1

你必须将所有的活动类添加到清单文件中!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接