2012-03-09 164 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);

請記住總是指定一個上下文和一個活動。