Android:使用按钮事件切换到另一个活动?

发布于 2024-12-01 11:16:13 字数 810 浏览 4 评论 0原文

我想使用按钮将当前活动更改为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水晶透心 2024-12-08 11:16:13

点击处理程序必须如下所示:

public void toManager(View view) {
    Intent i = new Intent(getApplicationContext(), DegreeActivity.class);
    startActivity(i);
}

来自 Button 文档:

现在,当用户单击该按钮时,Android 系统会调用
Activity 的 selfDestruct(View) 方法。为了使其发挥作用,
方法必须是公共的,并接受 View 作为其唯一参数。

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 a View as its only parameter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文