使用按钮Android启动活动

时间:2012-03-09 00:14:07

标签: android android-activity

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

  1. Class是activity_home class(),第二个是schedule_act()类。
  2. 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 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>  
    

    非常感谢。

5 个答案:

答案 0 :(得分:18)

意图区分大小写。改变

"my.action.bat.schedule_act"

"my.action.bat.SCHEDULE_ACT"

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

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

其中thisContext子类

答案 1 :(得分:3)

试试这个

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

答案 2 :(得分:2)

尝试更改行

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

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

看看是否有帮助。

有关详细信息,请参阅here

答案 3 :(得分:2)

您可以更改此行

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

这样的事情

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

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

答案 4 :(得分:1)

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