2012-03-09 35 views
10

Tôi gặp sự cố. Tôi muốn mở một Hoạt động với một Nút nhưng nó luôn luôn bị treo. Vì vậy, tôi đã tạo 2 Lớp và Nút A. Nhưng nó vẫn bị rơi.Bắt đầu hoạt động với nút Android

  1. Lớp học là activity_home class() và thứ hai là class schedule_act().

activity_home lớp: lớp

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> 

Cảm ơn bạn rất nhiều.

+0

Có ngoại lệ gì khi bị ném ?? –

Trả lời

18

Ý định phân biệt chữ hoa chữ thường. Thay đổi

"my.action.bat.schedule_act" 

Để

"my.action.bat.SCHEDULE_ACT" 

Ngoài ra, trừ khi bạn thực sự cần phải sử dụng một ý định, tôi sẽ bắt đầu hoạt động của bạn như vậy

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

đâu this là một lớp con Context

1

Bạn phải thêm tất cả các lớp hoạt động vào tệp kê khai !!

+1

Anh ấy đã .... kiểm tra danh sách '' cuối cùng trong mã mà anh ta đã đăng. –

2

Thử thay đổi dòng

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

Để

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

Và xem giúp.

Xem here để biết thêm thông tin.

3

Hãy thử điều này

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

Bạn có thể thay đổi dòng này

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

Để một cái gì đó như thế này

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

Hãy nhớ luôn luôn xác định một ngữ cảnh và một hoạt động.

Các vấn đề liên quan