【问题标题】:open activity B in different package/folder from activity A in other package (main package)在其他包(主包)中的活动 A 中打开不同包/文件夹中的活动 B
【发布时间】:2021-09-27 05:38:22
【问题描述】:

我目前正在为本学期的期末项目开发一款移动应用。我想知道如何从另一个包的另一个活动中打开一个包中的活动。

例子:

com.example.package1 中的 FirstActivity
Package2 中的 SecondActivity
Package2 在 com.example.package1 里面

我想在 FirstActivity 中单击按钮时打开 SecondActivity。

The underlined class is the 2nd Activity and the highlighted one is the 1st activity.

Here's the code from the MM_Play.class (first activity)

Here's the code in the AndroidManifest.xml

【问题讨论】:

  • 一切正常。检查视图的不匹配或重复 ID。此外,检查日志以查看是否有任何错误或警告。

标签: java android android-studio package


【解决方案1】:

请通过在第一个活动中导入第二个活动类来尝试这种方式

在第一个活动中的 onClickListener 内部

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(intent)

【讨论】:

  • 尝试检查活动名称是否在清单中声明,并检查按钮属性是否可点击,因为上面的行应该在你的项目中按你的意愿工作。或者你可以发送你已经实现的代码或按钮事件
  • @EJPol 请添加有关它如何不起作用的更多详细信息,因为没有人能猜出您的问题是什么。答案中的实际代码有一些在编译时会失败的格式错误,但我更正了它们。现在,答案应该可以解决您的问题。如果没有,请添加有关它如何不起作用的更多详细信息(编译问题、崩溃、没有任何反应等)
【解决方案2】:

像这样在清单文件中添加活动:

<activity
        android:name=". Your folder name where Second Activity is present then . 
        (Second Activity Name)"/>

在这样的Java文件中:

Intent intent = new Intent(FirstActivit.this,Second activity.class);
start activity(intent)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-25
    • 2016-09-30
    • 1970-01-01
    相关资源
    最近更新 更多