0

How to identify exactly what causing NullPointerException?

08-14 20:25:56.703: E/AndroidRuntime(32491): FATAL EXCEPTION: main
08-14 20:25:56.703: E/AndroidRuntime(32491): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.joericharduz.akita/com.joericharduz.akita.MainActivity}: java.lang.NullPointerException
08-14 20:25:56.703: E/AndroidRuntime(32491):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
08-14 20:25:56.703: E/AndroidRuntime(32491):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
08-14 20:25:56.703: E/AndroidRuntime(32491):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-14 20:25:56.703: E/AndroidRuntime(32491):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
08-14 20:25:56.703: E/AndroidRuntime(32491):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-14 20:25:56.703: E/AndroidRuntime(32491):    at android.os.Looper.loop(Looper.java:130)
08-14 20:25:56.703: E/AndroidRuntime(32491):    at android.app.ActivityThread.main(ActivityThread.java:3687)
08-14 20:25:56.703: E/AndroidRuntime(32491):    at java.lang.reflect.Method.invokeNative(Native Method)
08-14 20:25:56.703: E/AndroidRuntime(32491):    at java.lang.reflect.Method.invoke(Method.java:507)
08-14 20:25:56.703: E/AndroidRuntime(32491):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
08-14 20:25:56.703: E/AndroidRuntime(32491):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
08-14 20:25:56.703: E/AndroidRuntime(32491):    at dalvik.system.NativeStart.main(Native Method)
08-14 20:25:56.703: E/AndroidRuntime(32491): Caused by: java.lang.NullPointerException
08-14 20:25:56.703: E/AndroidRuntime(32491):    at com.joericharduz.akita.MainActivity.onCreate(MainActivity.java:56)
08-14 20:25:56.703: E/AndroidRuntime(32491):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-14 20:25:56.703: E/AndroidRuntime(32491):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
08-14 20:25:56.703: E/AndroidRuntime(32491):    ... 11 more

Please give your suggestions how to find NullPointerException.

Line 56 code:

etSearch.findViewById(R.id.etSearch);
4

2 回答 2

3

You haven't initialized etSearch. The proper way to do that is like so:

etSearch = (EditText) findViewById(R.id.etSearch);

NOT this: etSearch.findViewById(R.id.etSearch);

Make sure you've called setContentView(...) first.

于 2013-08-14T15:40:21.547 回答
0
Public class MyActivity extends Activity{

EditText et;
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et = (EditText)findViewById(R.id.yourId);
}
}

This is the proper way to instantiate the activity element . You are not typecasting the EditText element.

于 2013-08-14T15:55:09.197 回答