简体   繁体   中英

Weird exception in calling a method in Android

I'm getting an exception with this trace:

java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2247)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)
    at android.app.ActivityThread.access$700(ActivityThread.java:152)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5328)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
    at com.something.activity.SomeActivity.onCreate(SomeActivity.java:53)
    at android.app.Activity.performCreate(Activity.java:5250)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
    ... 11 more

And in line 53 of the given activity, I'm just calling a method of SomeActivity, which is something like this:

50 @Override
51 protected void onCreate(Bundle savedInstanceState){
52     ...
53     populateItems();
54     ...
55 }

How can I get a NullPointerException on calling line of a method? Is my log reporter drunk or what? BTW I'm pretty sure about the line of the code and whether the version who got the exception and the given code is exactly the same.

You can't, a method is not a field. Most likely it seems like you compiled your code, ran it and then afterwards changed some code. Then the stacktracke will of cause lead you wrong.

put your full onCreate method

check these:
-call setContentView(layout) before populateItems()
-in findViewById(item) , item must be in it's container layout

确保setContentView(R.layout.main_layout)中使用的main_layout是正确的布局,其中应包含populateItems()正在使用的所有项目/视图,然后可以通过findViewById(R.id.item_id) populateItems()获得populateItems()的每个项目。 findViewById(R.id.item_id) ,否则您将获得NullPointerException

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM