Android:使用按钮事件切换到另一个活动?
问题描述:
我想使用按钮将当前活动更改为android中的其他活动。然而,无论何时我点击按钮,eclipse调试透视图都会出现错误“未找到源”。这是我用来改变活动的功能Android:使用按钮事件切换到另一个活动?
public void toManager(){
Intent i = new Intent(getApplicationContext(), DegreeActivity.class);
startActivity(i);
}
在我的xml文件中,按钮有一个onClick监听器。这是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 -->
/>
如果我打电话的第一个活动的onCreate()
块toManager()
功能,它切换到没有错误的一个活动。但是,当我尝试使用按钮切换它不起作用。
答
Click处理程序必须像:
public void toManager(View view) {
Intent i = new Intent(getApplicationContext(), DegreeActivity.class);
startActivity(i);
}
从Button文档:
现在,当用户点击该按钮,Android系统调用 活动的
selfDestruct(View)
方法。为了使其发挥作用, 方法必须公开并接受View
作为其唯一参数。
哦,我明白了!谢谢。 – deztructicus
你可以忽略它,但处理程序的签名必须是'public void methodName(View)'。这是因为Android使用反射来查找处理程序,并且它可以找到您的方法,因为它具有错误的签名。 – Michael