ArrayAdapterがNullPointException


NullPointExceptionという例外でアプリが終了するのはよくあるバグです。ログを見ればどこで例外を起こしているか分かるのですが、今回は次のようにArrayAdapterの中で例外が起きていて困りました。

このNullPointExceptionの原因は、ArrayAdapterを作る時に渡したList<>の中にnullが入っていたことでした。

FATAL EXCEPTION: main
Process: jp.nosuz.secretkeyring, PID: 11911
java.lang.NullPointerException
    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
    at android.widget.AbsListView.obtainView(AbsListView.java:2263)
    at android.widget.ListView.makeAndAddView(ListView.java:1790)
    at android.widget.ListView.fillDown(ListView.java:691)
    at android.widget.ListView.fillFromTop(ListView.java:752)
    at android.widget.ListView.layoutChildren(ListView.java:1630)
    at android.widget.AbsListView.onLayout(AbsListView.java:2091)
    at android.view.View.layout(View.java:14948)
    at android.view.ViewGroup.layout(ViewGroup.java:4631)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
    at android.view.View.layout(View.java:14948)
    at android.view.ViewGroup.layout(ViewGroup.java:4631)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
    at android.view.View.layout(View.java:14948)
    at android.view.ViewGroup.layout(ViewGroup.java:4631)
    at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:433)
    at android.view.View.layout(View.java:14948)
    at android.view.ViewGroup.layout(ViewGroup.java:4631)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
    at android.view.View.layout(View.java:14948)
    at android.view.ViewGroup.layout(ViewGroup.java:4631)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
    at android.view.View.layout(View.java:14948)
    at android.view.ViewGroup.layout(ViewGroup.java:4631)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
    at android.view.View.layout(View.java:14948)
    at android.view.ViewGroup.layout(ViewGroup.java:4631)
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1991)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1748)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5692)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
    at android.view.Choreographer.doCallbacks(Choreographer.java:574)
    at android.view.Choreographer.doFrame(Choreographer.java:544)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:212)
    at android.app.ActivityThread.main(ActivityThread.java:5151)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
    at dalvik.system.NativeStart.main(Native Method)

参照

Spinner: NullPointerException at ArrayAdapter.createViewFromResource –stackoverflow


コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です