Android Intent NullPointer异常

时间:2013-04-01 20:09:10

标签: android-intent nullpointerexception android-activity android-manifest

我正在尝试拨打其他活动,具体取决于输入的密码是否正确。但我总是收到一个强制关闭我的应用程序的NullPointerException。

我的代码:

 Intent MainScreenIntent = new Intent("com.shr.khg.Login");
 startActivity(MainScreenIntent);

我的文件:

<activity
            android:name="com.shr.khg.LoginActivity"
            android:label="@string/title_activity_login" >
             <intent-filter>
                <action android:name="com.shr.khg.Login" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

日志:

04-01 20:05:59.221: D/dalvikvm(1846): GC_FOR_ALLOC freed 34K, 4% free 8624K/8963K, paused 57ms
04-01 20:05:59.230: I/dalvikvm-heap(1846): Grow heap (frag case) to 9.514MB for 1024016-byte allocation
04-01 20:05:59.320: D/dalvikvm(1846): GC_FOR_ALLOC freed <1K, 4% free 9624K/9991K, paused 58ms
04-01 20:05:59.460: D/dalvikvm(1846): GC_CONCURRENT freed <1K, 4% free 9624K/9991K, paused 5ms+14ms
04-01 20:06:05.860: D/AndroidRuntime(1846): Shutting down VM
04-01 20:06:05.860: W/dalvikvm(1846): threadid=1: thread exiting with uncaught exception (group=0x40014760)
04-01 20:06:05.870: E/AndroidRuntime(1846): FATAL EXCEPTION: main
04-01 20:06:05.870: E/AndroidRuntime(1846): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.shr.khg/com.shr.khg.LoginActivity}: java.lang.NullPointerException
04-01 20:06:05.870: E/AndroidRuntime(1846):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815)
04-01 20:06:05.870: E/AndroidRuntime(1846):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
04-01 20:06:05.870: E/AndroidRuntime(1846):     at android.app.ActivityThread.access$500(ActivityThread.java:122)
04-01 20:06:05.870: E/AndroidRuntime(1846):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
04-01 20:06:05.870: E/AndroidRuntime(1846):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-01 20:06:05.870: E/AndroidRuntime(1846):     at android.os.Looper.loop(Looper.java:132)
04-01 20:06:05.870: E/AndroidRuntime(1846):     at android.app.ActivityThread.main(ActivityThread.java:4123)
04-01 20:06:05.870: E/AndroidRuntime(1846):     at java.lang.reflect.Method.invokeNative(Native Method)
04-01 20:06:05.870: E/AndroidRuntime(1846):     at java.lang.reflect.Method.invoke(Method.java:491)
04-01 20:06:05.870: E/AndroidRuntime(1846):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
04-01 20:06:05.870: E/AndroidRuntime(1846):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
04-01 20:06:05.870: E/AndroidRuntime(1846):     at dalvik.system.NativeStart.main(Native Method)
04-01 20:06:05.870: E/AndroidRuntime(1846): Caused by: java.lang.NullPointerException
04-01 20:06:05.870: E/AndroidRuntime(1846):     at com.shr.khg.LoginActivity.setup(LoginActivity.java:98)
04-01 20:06:05.870: E/AndroidRuntime(1846):     at com.shr.khg.LoginActivity.onCreate(LoginActivity.java:89)
04-01 20:06:05.870: E/AndroidRuntime(1846):     at android.app.Activity.performCreate(Activity.java:4397)
04-01 20:06:05.870: E/AndroidRuntime(1846):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
04-01 20:06:05.870: E/AndroidRuntime(1846):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
04-01 20:06:05.870: E/AndroidRuntime(1846):     ... 11 more

我做错了什么?我只是不理解它,因为它看起来像我的所有其他活动,但只是没有那个。

谢谢!

3 个答案:

答案 0 :(得分:0)

创建意图以开始新活动的语法ActivityB来自ActivityA

Intent intent = new Intent(ActivityA.this, ActivityB.class);
startActivity(intent);

你的语法错了。日志在LoginActivity的第98和89行显示NullPointerException。请从这两行发布代码。

答案 1 :(得分:0)

我现在找到了答案,我不小心为活动分配了错误的布局,因此无法设置处理程序。

答案 2 :(得分:0)

您正在制作的活动的onCreate中存在错误。问题是您的应用程序在需要对象的情况下尝试使用null。