Android如何避免NullPointErexception

时间:2013-03-05 08:18:32

标签: android nullpointerexception

我的代码中有NullPointException,这就是我的应用程序崩溃的原因... 我是Android开发的新手,所以请你帮我避免这个Null异常吗?

这里有一些代码,我认为这是错误:

@Override
public void onClick(View v) {
    btn_skip = (Button) findViewById(R.id.btn_skip);
    // get data
    String email_txt = email.getEditableText().toString();
    String confirme_email = confirm_email.getEditableText().toString();

    //get IMEI
    TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    imei = telephonyManager.getDeviceId();

    if (v == btn_skip)
    {
        sendPostRequest(null, null, imei);
    }
    //if email is not valid then show errors
    else if (!checkEmail(email.getText().toString()) && v == btn_get_access)
    {
        email_error.setVisibility(View.VISIBLE);
        email.requestFocus();
        //Toast.makeText(getBaseContext(), "lkdjfgkdjg", Toast.LENGTH_LONG).show();
    }
    //else if (!confirme_email.equals(email_txt) && v == btn_get_access)
    else if (!email_txt.equals(confirme_email))
    {
        email_confirm_error.setVisibility(View.VISIBLE);
        confirm_email.requestFocus();
    }
    else 
    {
        email_error.setVisibility(View.GONE);
        email_confirm_error.setVisibility(View.GONE);
        sendPostRequest(email_txt, confirme_email, imei);
    }
}

所以我是如何得到这个错误的: 在我的应用程序中我有2个活动,我可以通过按钮切换它们,第一个按钮发送我从服务器上的用户获得的一些数据(所有输入都填充数据),第二个按钮我可以跳过输入填充和去到第二个屏幕(所有输入都是空的); 从第二个屏幕我可以回到没有任何数据的第一个屏幕(当我按下这个后退按钮,我有这个空的例外)。 请帮我解决这个错误,因为我无法想象如何避免它......

UPD LOGCAT

03-05 10:01:07.214: W/ActivityThread(12011): Application com.vladimir.expert_suise is      waiting for the debugger on port 8100...
03-05 10:01:07.224: I/System.out(12011): Sending WAIT chunk
03-05 10:01:07.224: I/dalvikvm(12011): Debugger is active
03-05 10:01:07.424: I/System.out(12011): Debugger has connected
03-05 10:01:07.424: I/System.out(12011): waiting for debugger to settle...
03-05 10:01:07.624: I/System.out(12011): waiting for debugger to settle...

03-05 10:01:07.814: I/System.out(12011): waiting for debugger to settle...
03-05 10:01:08.014: I/System.out(12011): waiting for debugger to settle...
03-05 10:01:08.215: I/System.out(12011): waiting for debugger to settle...
03-05 10:01:08.415: I/System.out(12011): waiting for debugger to settle...
03-05 10:01:08.615: I/System.out(12011): waiting for debugger to settle...
03-05 10:01:08.815: I/System.out(12011): waiting for debugger to settle...
03-05 10:01:09.015: I/System.out(12011): waiting for debugger to settle...
03-05 10:01:09.226: I/System.out(12011): waiting for debugger to settle...
03-05 10:01:09.426: I/System.out(12011): waiting for debugger to settle...
03-05 10:01:09.626: I/System.out(12011): waiting for debugger to settle...
03-05 10:01:09.826: I/System.out(12011): waiting for debugger to settle...
03-05 10:01:10.026: I/System.out(12011): waiting for debugger to settle...
03-05 10:01:10.226: I/System.out(12011): waiting for debugger to settle...
03-05 10:01:10.427: I/System.out(12011): waiting for debugger to settle...
03-05 10:01:10.627: I/System.out(12011): waiting for debugger to settle...
03-05 10:01:10.827: I/System.out(12011): waiting for debugger to settle...
03-05 10:01:11.027: I/System.out(12011): waiting for debugger to settle...
03-05 10:01:11.227: I/System.out(12011): waiting for debugger to settle...
03-05 10:01:11.428: I/System.out(12011): waiting for debugger to settle...
03-05 10:01:11.628: I/System.out(12011): waiting for debugger to settle...
03-05 10:01:11.828: I/System.out(12011): waiting for debugger to settle...
03-05 10:01:12.028: I/System.out(12011): debugger has settled (1463)
03-05 10:01:13.620: D/libEGL(12011): loaded /system/lib/egl/libGLES_android.so
03-05 10:01:13.800: D/libEGL(12011): loaded /system/lib/egl/libEGL_adreno200.so
03-05 10:01:14.080: D/libEGL(12011): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
03-05 10:01:14.080: D/libEGL(12011): loaded /system/lib/egl/libGLESv2_adreno200.so
03-05 10:01:14.401: I/Adreno200-EGLSUB(12011): <ConfigWindowMatch:2078>: Format RGBA_8888.
03-05 10:01:14.561: D/memalloc(12011): ashmem: Mapped buffer base:0x520a0000 size:1536000 fd:61
03-05 10:01:14.591: D/OpenGLRenderer(12011): Enabling debug mode 0
03-05 10:01:15.141: D/OpenGLRenderer(12011): has fontRender patch
03-05 10:01:15.171: D/OpenGLRenderer(12011): has fontRender patch
03-05 10:01:15.211: D/OpenGLRenderer(12011): has fontRender patch
03-05 10:01:15.281: D/memalloc(12011): ashmem: Mapped buffer base:0x5251a000 size:1536000 fd:64
03-05 10:01:15.962: D/memalloc(12011): ashmem: Mapped buffer base:0x52691000 size:1536000 fd:67
03-05 10:01:19.996: I/Adreno200-EGLSUB(12011): <ConfigWindowMatch:2078>: Format RGBA_8888.
03-05 10:01:20.006: D/memalloc(12011): ashmem: Mapped buffer base:0x52b08000 size:1536000 fd:76
03-05 10:01:20.316: D/memalloc(12011): ashmem: Mapped buffer base:0x52cff000 size:1536000 fd:79
03-05 10:01:20.647: I/Adreno200-EGLSUB(12011): <ConfigWindowMatch:2078>: Format RGBA_8888.
03-05 10:01:20.657: D/memalloc(12011): ashmem: Mapped buffer base:0x52f76000 size:1536000 fd:82
03-05 10:01:20.797: D/memalloc(12011): ashmem: Mapped buffer base:0x530ed000 size:1536000 fd:88
03-05 10:01:20.957: D/memalloc(12011): ashmem: Mapped buffer base:0x53264000 size:1536000 fd:91
03-05 10:01:21.157: D/memalloc(12011): ashmem: Mapped buffer base:0x533db000 size:1536000 fd:94
03-05 10:01:21.177: D/OpenGLRenderer(12011): Flushing caches (mode 0)
03-05 10:01:21.207: D/OpenGLRenderer(12011): Flushing caches (mode 0)
03-05 10:01:22.999: I/Adreno200-EGLSUB(12011): <ConfigWindowMatch:2078>: Format RGBA_8888.
03-05 10:01:23.019: D/memalloc(12011): ashmem: Mapped buffer base:0x5251a000 size:1536000 fd:63
03-05 10:01:23.309: D/memalloc(12011): ashmem: Mapped buffer base:0x52691000 size:1536000 fd:69
03-05 10:01:23.419: I/Adreno200-EGLSUB(12011): <ConfigWindowMatch:2078>: Format RGBA_8888.
03-05 10:01:23.429: D/memalloc(12011): ashmem: Mapped buffer base:0x52a08000 size:1536000 fd:79
03-05 10:01:23.610: D/memalloc(12011): ashmem: Mapped buffer base:0x52cff000 size:1536000 fd:88
03-05 10:01:23.660: D/memalloc(12011): ashmem: Mapped buffer base:0x530ed000 size:1536000 fd:97
03-05 10:01:23.830: D/memalloc(12011): ashmem: Mapped buffer base:0x53552000 size:1536000 fd:100
03-05 10:01:23.850: D/OpenGLRenderer(12011): Flushing caches (mode 0)
03-05 10:01:23.870: D/OpenGLRenderer(12011): Flushing caches (mode 0)
03-05 10:01:25.291: I/Adreno200-EGLSUB(12011): <ConfigWindowMatch:2078>: Format RGBA_8888.
03-05 10:01:25.301: D/memalloc(12011): ashmem: Mapped buffer base:0x5261a000 size:1536000 fd:63
03-05 10:01:25.451: D/memalloc(12011): ashmem: Mapped buffer base:0x52e76000 size:1536000 fd:82
03-05 10:01:25.481: D/OpenGLRenderer(12011): Flushing caches (mode 0)
03-05 10:01:25.882: D/memalloc(12011): ashmem: Mapped buffer base:0x52a08000 size:1536000 fd:79
03-05 10:01:27.603: D/AndroidRuntime(12011): Shutting down VM
03-05 10:01:27.603: W/dalvikvm(12011): threadid=1: thread exiting with uncaught exception (group=0x40ad9228)
03-05 10:01:27.633: E/AndroidRuntime(12011): FATAL EXCEPTION: main
03-05 10:01:27.633: E/AndroidRuntime(12011): java.lang.NullPointerException
03-05 10:01:27.633: E/AndroidRuntime(12011):    at com.vladimir.expert_suise.FirstScreen.onClick(FirstScreen.java:174)
03-05 10:01:27.633: E/AndroidRuntime(12011):    at android.view.View.performClick(View.java:3549)
03-05 10:01:27.633: E/AndroidRuntime(12011):    at android.view.View$PerformClick.run(View.java:14400)
03-05 10:01:27.633: E/AndroidRuntime(12011):    at android.os.Handler.handleCallback(Handler.java:605)
03-05 10:01:27.633: E/AndroidRuntime(12011):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-05 10:01:27.633: E/AndroidRuntime(12011):    at android.os.Looper.loop(Looper.java:154)
03-05 10:01:27.633: E/AndroidRuntime(12011):    at android.app.ActivityThread.main(ActivityThread.java:4945)
03-05 10:01:27.633: E/AndroidRuntime(12011):    at java.lang.reflect.Method.invokeNative(Native Method)
03-05 10:01:27.633: E/AndroidRuntime(12011):    at java.lang.reflect.Method.invoke(Method.java:511)
03-05 10:01:27.633: E/AndroidRuntime(12011):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-05 10:01:27.633: E/AndroidRuntime(12011):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-05 10:01:27.633: E/AndroidRuntime(12011):    at dalvik.system.NativeStart.main(Native Method)
03-05 10:01:30.747: D/Process(12011): killProcess, pid=12011
03-05 10:01:30.757: D/Process(12011): dalvik.system.VMStack.getThreadStackTrace(Native Method)
03-05 10:01:30.757: D/Process(12011): java.lang.Thread.getStackTrace(Thread.java:599)
03-05 10:01:30.757: D/Process(12011): android.os.Process.killProcess(Process.java:788)
03-05 10:01:30.757: D/Process(12011): com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:104)
03-05 10:01:30.757: D/Process(12011): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
03-05 10:01:30.757: D/Process(12011): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
03-05 10:01:30.757: D/Process(12011): dalvik.system.NativeStart.main(Native Method)
03-05 10:01:30.757: I/Process(12011): Sending signal. PID: 12011 SIG: 9
03-05 10:01:31.307: D/libEGL(12216): loaded /system/lib/egl/libGLES_android.so
03-05 10:01:31.307: D/libEGL(12216): loaded /system/lib/egl/libEGL_adreno200.so
03-05 10:01:31.307: D/libEGL(12216): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
03-05 10:01:31.317: D/libEGL(12216): loaded /system/lib/egl/libGLESv2_adreno200.so
03-05 10:01:31.337: I/Adreno200-EGLSUB(12216): <ConfigWindowMatch:2078>: Format RGBA_8888.
03-05 10:01:31.357: D/memalloc(12216): ashmem: Mapped buffer base:0x51fa0000 size:1536000 fd:61
03-05 10:01:31.367: D/OpenGLRenderer(12216): Enabling debug mode 0
03-05 10:01:31.417: D/OpenGLRenderer(12216): has fontRender patch
03-05 10:01:31.437: D/OpenGLRenderer(12216): has fontRender patch
03-05 10:01:31.467: D/OpenGLRenderer(12216): has fontRender patch
03-05 10:01:31.507: D/memalloc(12216): ashmem: Mapped buffer base:0x5241a000 size:1536000 fd:64
03-05 10:01:31.918: D/memalloc(12216): ashmem: Mapped buffer base:0x52691000 size:1536000 fd:67
03-05 10:01:47.313: D/AndroidRuntime(12216): Shutting down VM
03-05 10:01:47.313: W/dalvikvm(12216): threadid=1: thread exiting with uncaught exception (group=0x40ad9228)
03-05 10:01:47.313: E/AndroidRuntime(12216): FATAL EXCEPTION: main
03-05 10:01:47.313: E/AndroidRuntime(12216): java.lang.NullPointerException
03-05 10:01:47.313: E/AndroidRuntime(12216):    at com.vladimir.expert_suise.FirstScreen.onClick(FirstScreen.java:174)
03-05 10:01:47.313: E/AndroidRuntime(12216):    at android.view.View.performClick(View.java:3549)
03-05 10:01:47.313: E/AndroidRuntime(12216):    at android.view.View$PerformClick.run(View.java:14400)
03-05 10:01:47.313: E/AndroidRuntime(12216):    at android.os.Handler.handleCallback(Handler.java:605)
03-05 10:01:47.313: E/AndroidRuntime(12216):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-05 10:01:47.313: E/AndroidRuntime(12216):    at android.os.Looper.loop(Looper.java:154)
03-05 10:01:47.313: E/AndroidRuntime(12216):    at android.app.ActivityThread.main(ActivityThread.java:4945)
03-05 10:01:47.313: E/AndroidRuntime(12216):    at java.lang.reflect.Method.invokeNative(Native Method)
03-05 10:01:47.313: E/AndroidRuntime(12216):    at java.lang.reflect.Method.invoke(Method.java:511)
03-05 10:01:47.313: E/AndroidRuntime(12216):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-05 10:01:47.313: E/AndroidRuntime(12216):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-05 10:01:47.313: E/AndroidRuntime(12216):    at dalvik.system.NativeStart.main(Native Method)
03-05 10:01:49.064: D/Process(12216): killProcess, pid=12216
03-05 10:01:49.064: D/Process(12216): dalvik.system.VMStack.getThreadStackTrace(Native Method)
03-05 10:01:49.064: D/Process(12216): java.lang.Thread.getStackTrace(Thread.java:599)
03-05 10:01:49.064: D/Process(12216): android.os.Process.killProcess(Process.java:788)
03-05 10:01:49.064: D/Process(12216): com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:104)
03-05 10:01:49.064: D/Process(12216): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
03-05 10:01:49.064: D/Process(12216): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
03-05 10:01:49.064: D/Process(12216): dalvik.system.NativeStart.main(Native Method)
03-05 10:01:49.064: I/Process(12216): Sending signal. PID: 12216 SIG: 9
03-05 10:01:49.545: D/libEGL(12233): loaded /system/lib/egl/libGLES_android.so
03-05 10:01:49.555: D/libEGL(12233): loaded /system/lib/egl/libEGL_adreno200.so
03-05 10:01:49.555: D/libEGL(12233): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
03-05 10:01:49.555: D/libEGL(12233): loaded /system/lib/egl/libGLESv2_adreno200.so
03-05 10:01:49.575: I/Adreno200-EGLSUB(12233): <ConfigWindowMatch:2078>: Format RGBA_8888.
03-05 10:01:49.585: D/memalloc(12233): ashmem: Mapped buffer base:0x51fb9000 size:1536000 fd:62
03-05 10:01:49.585: D/OpenGLRenderer(12233): Enabling debug mode 0
03-05 10:01:49.645: D/OpenGLRenderer(12233): has fontRender patch
03-05 10:01:49.665: D/OpenGLRenderer(12233): has fontRender patch
03-05 10:01:49.685: D/OpenGLRenderer(12233): has fontRender patch
03-05 10:01:49.755: D/memalloc(12233): ashmem: Mapped buffer base:0x52466000 size:1536000 fd:65
03-05 10:01:50.135: D/memalloc(12233): ashmem: Mapped buffer base:0x526dd000 size:1536000 fd:68
03-05 10:01:51.347: D/OpenGLRenderer(12233): Flushing caches (mode 0)
03-05 10:01:51.447: W/IInputConnectionWrapper(12233): showStatusIcon on inactive InputConnection
03-05 10:01:51.447: W/IInputConnectionWrapper(12233): InputConnection = com.android.internal.widget.EditableInputConnection@40dd02e8, active client = false
03-05 10:01:51.647: D/OpenGLRenderer(12233): Flushing caches (mode 1)
03-05 10:02:00.155: D/OpenGLRenderer(12233): Flushing caches (mode 1)
03-05 10:02:08.814: D/OpenGLRenderer(12233): Flushing caches (mode 1)
03-05 10:02:08.994: D/OpenGLRenderer(12233): Flushing caches (mode 2)
03-05 10:02:32.427: W/ActivityThread(12468): Application com.vladimir.expert_suise is waiting for the debugger on port 8100...
03-05 10:02:32.447: I/System.out(12468): Sending WAIT chunk
03-05 10:02:32.447: I/dalvikvm(12468): Debugger is active
03-05 10:02:32.647: I/System.out(12468): Debugger has connected
03-05 10:02:32.647: I/System.out(12468): waiting for debugger to settle...
03-05 10:02:32.847: I/System.out(12468): waiting for debugger to settle...
03-05 10:02:33.047: I/System.out(12468): waiting for debugger to settle...
03-05 10:02:33.247: I/System.out(12468): waiting for debugger to settle...
03-05 10:02:33.448: I/System.out(12468): waiting for debugger to settle...
03-05 10:02:33.648: I/System.out(12468): waiting for debugger to settle...
03-05 10:02:33.848: I/System.out(12468): waiting for debugger to settle...
03-05 10:02:34.048: I/System.out(12468): waiting for debugger to settle...
03-05 10:02:34.248: I/System.out(12468): waiting for debugger to settle...
03-05 10:02:34.449: I/System.out(12468): waiting for debugger to settle...
03-05 10:02:34.649: I/System.out(12468): waiting for debugger to settle...
03-05 10:02:34.859: I/System.out(12468): waiting for debugger to settle...
03-05 10:02:35.049: I/System.out(12468): waiting for debugger to settle...
03-05 10:02:35.249: I/System.out(12468): waiting for debugger to settle...
03-05 10:02:35.450: I/System.out(12468): waiting for debugger to settle...
03-05 10:02:35.650: I/System.out(12468): waiting for debugger to settle...
03-05 10:02:35.850: I/System.out(12468): waiting for debugger to settle...
03-05 10:02:36.050: I/System.out(12468): waiting for debugger to settle...
03-05 10:02:36.250: I/System.out(12468): debugger has settled (1356)
03-05 10:02:37.502: D/libEGL(12468): loaded /system/lib/egl/libGLES_android.so
03-05 10:02:37.532: D/libEGL(12468): loaded /system/lib/egl/libEGL_adreno200.so
03-05 10:02:37.582: D/libEGL(12468): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
03-05 10:02:37.582: D/libEGL(12468): loaded /system/lib/egl/libGLESv2_adreno200.so
03-05 10:02:37.652: I/Adreno200-EGLSUB(12468): <ConfigWindowMatch:2078>: Format RGBA_8888.
03-05 10:02:37.692: D/memalloc(12468): ashmem: Mapped buffer base:0x520a0000 size:1536000 fd:61
03-05 10:02:37.702: D/OpenGLRenderer(12468): Enabling debug mode 0
03-05 10:02:37.902: D/OpenGLRenderer(12468): has fontRender patch
03-05 10:02:37.922: D/OpenGLRenderer(12468): has fontRender patch
03-05 10:02:37.952: D/OpenGLRenderer(12468): has fontRender patch
03-05 10:02:38.002: D/memalloc(12468): ashmem: Mapped buffer base:0x5251a000 size:1536000 fd:64
03-05 10:02:38.312: D/memalloc(12468): ashmem: Mapped buffer base:0x52691000 size:1536000 fd:67
03-05 10:03:07.731: D/dalvikvm(12468): Debugger has detached; object registry had 776 entries
03-05 10:03:07.731: D/AndroidRuntime(12468): Shutting down VM
03-05 10:03:07.731: W/dalvikvm(12468): threadid=1: thread exiting with uncaught exception (group=0x40ad9228)
03-05 10:03:07.731: E/AndroidRuntime(12468): FATAL EXCEPTION: main
03-05 10:03:07.731: E/AndroidRuntime(12468): java.lang.NullPointerException
03-05 10:03:07.731: E/AndroidRuntime(12468):    at com.vladimir.expert_suise.FirstScreen.onClick(FirstScreen.java:174)
03-05 10:03:07.731: E/AndroidRuntime(12468):    at android.view.View.performClick(View.java:3549)
03-05 10:03:07.731: E/AndroidRuntime(12468):    at android.view.View$PerformClick.run(View.java:14400)
03-05 10:03:07.731: E/AndroidRuntime(12468):    at android.os.Handler.handleCallback(Handler.java:605)
03-05 10:03:07.731: E/AndroidRuntime(12468):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-05 10:03:07.731: E/AndroidRuntime(12468):    at android.os.Looper.loop(Looper.java:154)
03-05 10:03:07.731: E/AndroidRuntime(12468):    at android.app.ActivityThread.main(ActivityThread.java:4945)
03-05 10:03:07.731: E/AndroidRuntime(12468):    at java.lang.reflect.Method.invokeNative(Native Method)
03-05 10:03:07.731: E/AndroidRuntime(12468):    at java.lang.reflect.Method.invoke(Method.java:511)
03-05 10:03:07.731: E/AndroidRuntime(12468):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-05 10:03:07.731: E/AndroidRuntime(12468):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-05 10:03:07.731: E/AndroidRuntime(12468):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

尝试删除Context,如下所示:

TelephonyManager tManager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String IMEI = tManager.getDeviceId();

取自Using Telephony Manager in android to find IMEI number

答案 1 :(得分:0)

您是否已获得清单文件的许可?

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