Android:使用按钮事件切换到另一个活动?
我想使用按钮将当前活动更改为 android 中的另一个活动。但是,每当我单击该按钮时,Eclipse 调试透视图都会出现错误“找不到源”。这是我用来更改活动的函数
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() 函数,它会毫无错误地切换到下一个活动。但是,当我尝试使用按钮进行切换时,它不起作用。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
点击处理程序必须如下所示:
来自 Button 文档:
Click handler must look like:
From Button documentation: