简体   繁体   English

在Android中调用方法的怪异异常

[英]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: 在给定活动的第53行中,我只是调用SomeActivity的方法,如下所示:

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? 如何在方法的调用行上获取NullPointerException 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. 然后stacktracke的原因将导致您出错。

put your full onCreate method 把你完整的onCreate方法

check these: 检查这些:
-call setContentView(layout) before populateItems() 在populateItems()之前调用setContentView(layout)
-in findViewById(item) , item must be in it's container layout -在findViewById(item)中,项目必须在其容器布局中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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