Android:在清单中找不到活动,但它在那里?

发布于 2024-12-05 00:36:17 字数 1734 浏览 4 评论 0原文

我不明白为什么!我检查了 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 应该是无关紧要的,因为当我运行程序时它没有得到该变量。


源代码

如果您想仔细查看,这里是完整的源代码。

http://www.speedyshare.com/files/30343046/project.zip

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.

http://www.speedyshare.com/files/30343046/project.zip

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我要还你自由 2024-12-12 00:36:17

问题出在您的 SettingsActivity
当您使用 PreferenceActivity 时,您可以用来

addPreferencesFromResource(R.layout.settings);

设置内容。
但你正在使用

setContentView(R.layout.settings);

The problem is in your SettingsActivity
when you are using PreferenceActivity you use

addPreferencesFromResource(R.layout.settings);

to set contents.
but you are using

setContentView(R.layout.settings);
如日中天 2024-12-12 00:36:17

可能是包名不匹配。 Activity 类位于哪个 Java 包中?必须与清单中的匹配,即 se.jbhalmstad.ndroid。或者在 android:name 属性中提供完整的类名称。

编辑:我有一个工作示例,我的 Preferences 活动的 元素看起来略有不同:

<activity android:name="Prefs"/>

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 the android:name attribute.

EDIT: I have a working example, and the <activity> element for my Preferences activity looks slightly different:

<activity android:name="Prefs"/>

No dot before Prefs.

Also, check if the SettingsActivity class is public. You never showed it to us.

羁〃客ぐ 2024-12-12 00:36:17

我认为您不需要将包的名称添加到您的活动名称中,您只需添加您的活动SettingActivity,如下所示:

<activity android:name="SettingsActivity" />

因为您的活动位于清单上声明的同一个包中:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="se.jbhalmstad.ndroid"...

请告诉我是否有帮助

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 :

<activity android:name="SettingsActivity" />

because your activity is in the same package that is declared on the manifest :

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="se.jbhalmstad.ndroid"...

let me know if it helps

ゞ记忆︶ㄣ 2024-12-12 00:36:17

确保 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 of New -> Class/file
or
Use setContentView(R.layout.yours_activity_class_here); OnCreate

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文