Android:在清单中找不到活动,但它在那里?
我不明白为什么!我检查了 logcat,发现它崩溃的原因是因为出于某种未知原因在清单文件中找不到 Activity。
我看过类似的帖子,但似乎没有一个答案对我有用。
这是我启动活动的代码:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
Intent ntnt = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(ntnt);
}
}
和清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="se.jbhalmstad.ndroid"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
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=".SettingsActivity"></activity>
</application>
</manifest>
如您所见,我在清单底部的标签内部声明了我尝试启动的 SettingsActivity。
我可能是瞎子,但我找不到任何问题。你可以吗?
如果您需要更多源代码,请告诉我,但 SettingsActivity 应该是无关紧要的,因为当我运行程序时它没有得到该变量。
源代码
如果您想仔细查看,这里是完整的源代码。
And i can't figure out why! I checked logcat and i saw that the reason it crashed was because it isn't finding the Activity in the manifest file for some unknown reason.
I've looked at similar threads but none of the answers seem to work for me.
Here's the code where i start the activity:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
Intent ntnt = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(ntnt);
}
}
And the manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="se.jbhalmstad.ndroid"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
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=".SettingsActivity"></activity>
</application>
</manifest>
As you can see i declared the SettingsActivity that i'm trying to start, at the bottom of the Manifest, inside of the tag.
I might be blind, but i can't find anything wrong. Can you?
Let me know if you need more source, but the SettingsActivity should be irrelevant because it doesn't get that var when i run the program.
Source code
If you want to take a closer look, here's the entire source code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题出在您的
SettingsActivity
当您使用
PreferenceActivity
时,您可以用来设置内容。
但你正在使用
The problem is in your
SettingsActivity
when you are using
PreferenceActivity
you useto set contents.
but you are using
可能是包名不匹配。 Activity 类位于哪个 Java 包中?必须与清单中的匹配,即
se.jbhalmstad.ndroid
。或者在android:name
属性中提供完整的类名称。编辑:我有一个工作示例,我的 Preferences 活动的
元素看起来略有不同:Prefs
之前没有点。另外,检查 SettingsActivity 类是否是公共的。你从来没有向我们展示过。
Could be package name mismatch. What Java package is the activity class in? Must match the one in the manifest, i. e.
se.jbhalmstad.ndroid
. Or provide full class name in theandroid:name
attribute.EDIT: I have a working example, and the
<activity>
element for my Preferences activity looks slightly different:No dot before
Prefs
.Also, check if the SettingsActivity class is public. You never showed it to us.
我认为您不需要将包的名称添加到您的活动名称中,您只需添加您的活动SettingActivity,如下所示:
因为您的活动位于清单上声明的同一个包中:
请告诉我是否有帮助
i think you dont need to add the name of the package to your acitivity name , you can just add your activity SettingActivity like this :
because your activity is in the same package that is declared on the manifest :
let me know if it helps
确保 Manifest 意图正确的活动,而不仅仅是基本的 Java / Kotlin 类文件
新->活动->空 Activity
而不是New ->类/文件
或者
使用 setContentView(R.layout.yours_activity_class_here); OnCreate
Be sure that Manifest intent on the right activity and not just the basic Java / Kotlin class file
New -> Activity -> Empty Activity
instead ofNew -> Class/file
or
Use
setContentView(R.layout.yours_activity_class_here);
OnCreate