2012-03-09 163 views
10

我遇到了问题。我想用一个按钮打开一个Activity,但它总是崩溃。 所以我创建了2个类和一个按钮。但它不断崩溃。按钮开始活动Android

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


       } 
      }); 
     } 





    } 

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> 

非常感谢。

+0

什么是抛出的异常? – 2012-03-09 00:17:20

回答

18

意图区分大小写。更改

"my.action.bat.schedule_act" 

"my.action.bat.SCHEDULE_ACT" 

而且,除非你真的需要使用的意图,我将开始新的活动,像这样

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

thisContext

1

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

+1

他确实......检查了他发布的代码中最后一个“”列表。 – 2012-03-09 00:20:06

2

尝试换行

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

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

,看看有没有帮助。

有关更多信息,请参见here

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"); startActivity(myIntent);

要像这样

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

请记住总是指定一个上下文和一个活动。