gpt4 book ai didi

安卓 Activity 导航

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:16:12 26 4
gpt4 key购买 nike

我是 android 开发的新手,我正在开发一个有 4 个 Activity 的应用程序。每个 Activity 都需要能够导航到其他 3 个 Activity 中的任何一个。所以我在每个 Activity 的顶部创建了 4 个按钮来实现这一点。 XML 代码如下所示:

<Button ... android:onClick="loadProfileLayout"/>  
<Button ... android:onClick="loadRulesLayout"/>
<Button ... android:onClick="loadSettingsLayout"/>
<Button ... android:onClick="loadHelpLayout"/>

list 中的每个都有一个 Activity 标签:

    <activity android:name=".Profiler" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="Rules"></activity>
<activity android:name="Settings"></activity>
<activity android:name="Help"></activity>

调用的函数是:

public void loadProfileLayout()        { startActivity(new Intent(this, Profiler.class)); }
public void loadRulesLayout(View v) { startActivity(new Intent(this, Rules.class)); }
public void loadSettingsLayout(View v) { startActivity(new Intent(this, Settings.class)); }
public void loadHelpLayout(View v) { startActivity(new Intent(this, Help.class)); }

所以最初这是可行的。从主要的“个人资料” Activity 中,我可以导航到其他 3 个 Activity 中的任何一个。从其他 3 个 Activity 中,我可以导航到任何地方,但不能回到主要 Activity 。当我按下主要 Activity 按钮时,应用程序崩溃。我尝试调试,但它甚至似乎没有执行 loadProfileLayout()。 Eclipse 打开一个“View.class”文件,其内容基本上是“找不到源”。如果我按 F8 键继续调试,它会再次加载“ZygoteInit$MethodAndArgsCaller.run()”...“找不到源”。再次按 F8 将在模拟器中加载错误消息“抱歉!应用程序意外停止。请重试。”

同样,我是 Android 的新手,我所知道的 Activity 就是我在开发网站上阅读的内容。我是否犯了一个我不知道的根本性错误?

谢谢,
内特

最佳答案

我不确定这是否是您问题中的错字,但是 loadProfileLayout() 还需要将 View 作为其唯一参数:

public void loadProfileLayout(View v)

编辑:View 参数是导致onClick 事件的 View (在您的例子中是Button 实例)。我没有看过代码,但我假设 View 正在使用反射来查找要调用的方法(特别是将 View 作为参数的方法),并且由于找不到匹配的方法,它决定抛出一个异常(exception)。

关于安卓 Activity 导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4305182/

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