Android Manifest Error _ 应用程序无法运行

Posted

技术标签:

【中文标题】Android Manifest Error _ 应用程序无法运行【英文标题】:Android Manifest Error _ the app does not run 【发布时间】:2021-03-31 00:43:49 【问题描述】:

我正在制作一个首先运行 login_nb 的应用程序。在我将 userinfoinput.kt 添加到此项目之前没有问题,但之后出现了一些错误。

我的代码清单代码是:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.nb_main">


    <uses-permission android:name="android.permission.INTERNET" />

    <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/Theme.Nb_main"
        android:usesCleartextTraffic="true">

        <activity android:name=".SignUp" />
        <activity android:name=".photoboard" />
        <activity android:name=".addPhoto" />
        <activity android:name=".textboard" />
        <activity android:name=".login_nb">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" /> <!-- Move this to set the initial activity -->

                <category android:name="android.intent.category.LAUNCHER" /> <!-- Move this to set the initial activity -->
            </intent-filter>
        </activity>
        <activity android:name=".popup_color" />
        <activity android:name=".built" />
        <activity android:name=".userinfoinput"/>
        <!--
               Set to true if your app is Standalone, that is, it does not require the handheld
               app to run.
        -->
        <activity
            android:name=".square"
            android:label="@string/title_activity_square" />
        <activity android:name=".MainActivity" />
    </application>

错误信息如下:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.nb_main, PID: 9057
java.lang.RuntimeException: Unable to instantiate activity ComponentInfocom.example.nb_main/com.example.nb_main.login_nb: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3194)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7356)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
    at android.content.ContextWrapper.getPackageName(ContextWrapper.java:145)
    at android.content.ComponentName.<init>(ComponentName.java:131)
    at android.content.Intent.<init>(Intent.java:6510)
    at com.example.nb_main.login_nb.<init>(login_nb.kt:24)
    at java.lang.Class.newInstance(Native Method)
    at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
    at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1243)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3182)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
    at android.os.Handler.dispatchMessage(Handler.java:107) 
    at android.os.Looper.loop(Looper.java:214) 
    at android.app.ActivityThread.main(ActivityThread.java:7356) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 

I/Process:发送信号。 PID:9057 SIG:9

请告诉我我做错了什么。我主要使用kotlin,但我也可以理解java。

【问题讨论】:

查看此链接***.com/questions/28515049/… 这能回答你的问题吗? What is a NullPointerException, and how do I fix it? 【参考方案1】:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
    at android.content.ContextWrapper.getPackageName(ContextWrapper.java:145)
    at android.content.ComponentName.<init>(ComponentName.java:131)
    at android.content.Intent.<init>(Intent.java:6510)
    at com.example.nb_main.login_nb.<init>(login_nb.kt:24)

login_nb.kt 第 24 行中,有一些实例初始化代码实例化了 Intent。关于活动生命周期还为时过早。在初始化阶段,活动尚未准备好用作Context。这种初始化代码的通常位置是活动的onCreate()

【讨论】:

哦,你是对的,我声明了将身份验证数据传递给其他活动的意图。我这样做是因为我希望在 login_nb.kt 中全局使用意图,但我发现在 OnCreate() 上声明它无法实现我的意图。你能告诉我怎么做吗? 您可以在类级别声明一个字段,但任何需要上下文的初始化分配都需要稍后。

以上是关于Android Manifest Error _ 应用程序无法运行的主要内容,如果未能解决你的问题,请参考以下文章

Workbox with Angular 12 Error: Can't find self.__WB_MANIFEST in your SW source

Android Studio 报出此错AAPT: error: unexpected element <intent-filter> found in <manifest><application>.

错误:Android 12 中的 INSTALL_PARSE_FAILED_MANIFEST_MALFORMED

AAPT 错误:<manifest> 标记中的属性“包”不是有效的 Android 包名称

Android异常篇 Manifest merger failed : Attribute application@label value=() from AndroidM

android O Error inflating class android.webkit.WebView