gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 14:34:01 24 4
gpt4 key购买 nike

我想使用按钮将当前 Activity 更改为 android 中的另一个 Activity 。但是,每当我单击该按钮时,eclipse 调试透视图都会出现错误“找不到源”。这是我用来更改 Activity 的功能

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 -->
/>

如果我在第一个 Activity 的 onCreate() block 中调用 toManager() 函数,它会毫无错误地切换到下一个 Activity 。但是,当我尝试使用按钮进行切换时,它不起作用。

最佳答案

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

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

来自 Button文档:

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.

关于Android:使用按钮事件切换到另一个 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7214206/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com