मैं एक ऐप बना रहा हूं जो पहले 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 ComponentInfo{com.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: सिग्नल भेजना। पीआईडी: 9057 एसआईजी: 9
कृपया मुझे बताएं कि मैंने क्या गलत किया है। मैं मुख्य रूप से कोटलिन का उपयोग करता हूं, लेकिन मैं किसी तरह जावा को भी समझ सकता हूं।
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 में कुछ इंस्टेंस init कोड है जो Intent
को इंस्टेंट करता है। गतिविधि जीवनचक्र के संबंध में यह बहुत जल्दी है। प्रारंभिक चरण में गतिविधि अभी तक Context
के रूप में उपयोग करने के लिए तैयार नहीं है। ऐसे init कोड के लिए सामान्य स्थान गतिविधि का onCreate()
है।
नए सवाल
java
जावा एक उच्च स्तरीय प्रोग्रामिंग भाषा है। इस टैग का उपयोग तब करें जब आपको भाषा का उपयोग करने या समझने में समस्या हो। इस टैग का उपयोग शायद ही कभी किया जाता है और इसका उपयोग अक्सर [वसंत], [वसंत-बूट], [जकार्ता-ई], [Android], [javafx], [हडूप], [श्रेणी] और [मावेन] के साथ किया जाता है।