android AbsListView NullPointerException

时间:2014-02-20 10:27:32

标签: java android android-listview nullpointerexception

我在AbsListView

中遇到NullPointerException问题
  java.lang.NullPointerException
        at android.widget.AbsListView.obtainView(AbsListView.java:2161)
        at android.widget.ListView.makeAndAddView(ListView.java:1831)
        at android.widget.ListView.fillDown(ListView.java:674)
        at android.widget.ListView.fillFromTop(ListView.java:735)
        at android.widget.ListView.layoutChildren(ListView.java:1638)
        at android.widget.AbsListView.onLayout(AbsListView.java:1994)
        at android.view.View.layout(View.java:14008)
        at android.view.ViewGroup.layout(ViewGroup.java:4373)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
        at android.view.View.layout(View.java:14008)
        at android.view.ViewGroup.layout(ViewGroup.java:4373)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
        at android.view.View.layout(View.java:14008)
        at android.view.ViewGroup.layout(ViewGroup.java:4373)
        at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1585)
        at android.view.View.layout(View.java:14008)
        at android.view.ViewGroup.layout(ViewGroup.java:4373)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
        at android.view.View.layout(View.java:14008)
        at android.view.ViewGroup.layout(ViewGroup.java:4373)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
        at android.view.View.layout(View.java:14008)
        at android.view.ViewGroup.layout(ViewGroup.java:4373)
        at android.support.v4.widget.SlidingPaneLayout.onLayout(SlidingPaneLayout.java:651)
        at android.view.View.layout(View.java:14008)
        at android.view.ViewGroup.layout(ViewGroup.java:4373)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
        at android.view.View.layout(View.java:14008)
        at android.view.ViewGroup.layout(ViewGroup.java:4373)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
        at android.view.View.layout(View.java:14008)
        at android.view.ViewGroup.layout(ViewGroup.java:4373)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
        at android.view.View.layout(View.java:14008)
        at android.view.ViewGroup.layout(ViewGroup.java:4373)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
        at android.view.Choreographer.doCallbacks(Choreographer.java:562)
        at android.view.Choreographer.doFrame(Choreographer.java:532)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5041)
        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:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        at dalvik.system.NativeStart.main(Native Method)

这是AbsListView.java的代码

2159    child = mAdapter.getView(position, null, this);
2160
2161    if (child.getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
2162        child.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
2163    }

因此getView返回null,但是没有来自我的应用程序的代码所在位置的跟踪。有什么帮助吗?

1 个答案:

答案 0 :(得分:4)

我有完全相同的错误行并找到了你的问题。正如评论中所建议的那样,您可能会从此null

返回mAdapter.getView(position, null, this);

您可以按照以下方式构建getView方法:

 @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyCustomView = null;
         // The view here is not built yet
        if (convertView == null) {
            v = new MyCustomView(ctx); // build some view here.
        } // the view can be retrieved
        else {
            v = (MyCustomView) convertView;
        }

        return v;
    }

希望有所帮助