当我有意启动它时,为啥android无法识别第二个活动?
Posted
技术标签:
【中文标题】当我有意启动它时,为啥android无法识别第二个活动?【英文标题】:Why android doesn't recognize second activity when i launch it with an intent?当我有意启动它时,为什么android无法识别第二个活动? 【发布时间】:2019-06-16 18:29:45 【问题描述】:点击侦听器尝试打开新活动时弹出一个奇怪的错误:
android.content.ActivityNotFoundException:无法找到显式 活动课 com.example.room2n.practice8/com.example.room2n.practice8.NewDbaActivity; 您是否在 AndroidManifest.xml 中声明了此活动?
这很奇怪,因为我无法在清单中指出任何问题:
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:theme="@style/AppTheme.NoActionBar"
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=".NewDbaActivity">
</activity>
</application>
还有就是触发函数:
public void onClick(View view)
Intent intent = new Intent(MainActivity.this, NewDbaActivity.class);
startActivity(intent);
【问题讨论】:
这很奇怪。我已尝试复制您的问题,并想通知您它运行良好。你有保存你的清单文件吗?此外,您可以在再次运行之前尝试清理并重建项目。其次,尝试将应用主题从 AppTheme.NoActionBar 更改为普通 AppTheme 并再次运行。 在类似的项目上(基本上是相同的,只是变量名不同等),它也运行良好,我尝试比较它们,但找不到任何可能导致它的原因。 【参考方案1】:您必须在 AndroidManifest.xml 文件中声明您新创建的 Activity。比如上面的例子:
<activity
android:name=".NewDbaActivity"
android:label="@string/title_yourTitle"
android:theme="@style/AppTheme.NoActionBar"/>
我看到你已经做了一些事情。我建议查看第二个活动的路径,是在包中吗?因为我可以读到类似的内容: practice8 .. 如果是这样,您需要将其声明为 .practice8.NewDbaActivity 。
PS:我看到了您对问题的编辑,这导致了问题。您已经通过编辑这些行来更改和弄乱它们。所以根据包名按照前面的方式。
【讨论】:
以上是关于当我有意启动它时,为啥android无法识别第二个活动?的主要内容,如果未能解决你的问题,请参考以下文章