Android: Switching to another activity using a button event?
I want to change the current activity to another activity in android using a button. However whenever I click the button, eclipse debug perspective comes up with the error "source not found". This is the function I'm using to change the activity
public void toManager(){
Intent i = new Intent(getApplicationContext(), DegreeActivity.class);
startActivity(i);
}
In my xml file, the button has an onClick 开发者_运维问答listener. This is the xml
<Button
android:id="@+id/btn_toDegree"
android:text="@string/btn_toDegree"
android:textSize="13pt"
android:layout_centerVertical="true"
android:gravity="center"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginLeft="15dip"
android:layout_marginRight="15dip"
android:onClick="toManager" <!-- This line -->
/>
If I call the toManager()
function in the onCreate()
block of the first activity, It switches to the next activity with no error. However when I try to switch using the button it doesn't work.
Click handler must look like:
public void toManager(View view) {
Intent i = new Intent(getApplicationContext(), DegreeActivity.class);
startActivity(i);
}
From Button documentation:
Now, when a user clicks the button, the Android system calls the activity's
selfDestruct(View)
method. In order for this to work, the method must be public and accept aView
as its only parameter.
精彩评论