当前位置: 代码迷 >> 综合 >> NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParam
  详细解决方案

NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParam

热度:10   发布时间:2024-01-21 03:01:22.0

最近在做和图片相关显示的出现了一个问题,整理一下思路,分享出来给大家参考一下:

下面是一个空指针的异常,是在GalleryAdapter中出现的。

// Short Msg: java.lang.NullPointerException

// Long Msg: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.view.View.getLayoutParams()' on a null object reference
// Build Label: Lenovo/passion/passion:5.1.1/LMY47V/VIBEUI_V2.8_1532_5.268.1_ST_P1c72:user/release-keys
// Build Changelist: VIBEUI_V2.8_1532_5.268.1_ST_P1c72
// Build Time: 1438611592000
// java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.view.View.getLayoutParams()' on a null object reference
// at android.widget.Gallery.setUpChild(Gallery.java:893)
// at android.widget.Gallery.makeAndAddView(Gallery.java:872)
// at android.widget.Gallery.layout(Gallery.java:669)
// at android.widget.Gallery.onLayout(Gallery.java:361)
// at android.view.View.layout(View.java:15725)
// at android.view.ViewGroup.layout(ViewGroup.java:5041)
// at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
// at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
// at android.view.View.layout(View.java:15725)
// at android.view.ViewGroup.layout(ViewGroup.java:5041)
// at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
// at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
// at android.view.View.layout(View.java:15725)
// at android.view.ViewGroup.layout(ViewGroup.java:5041)
// at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
// at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
// at android.view.View.layout(View.java:15725)
// at android.view.ViewGroup.layout(ViewGroup.java:5041)
// at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
// at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1692)
// at android.widget.LinearLayout.onLayout(LinearLayout.java:1468)
// at android.view.View.layout(View.java:15725)
// at android.view.ViewGroup.layout(ViewGroup.java:5041)
// at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
// at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
// at android.view.View.layout(View.java:15725)
// at android.view.ViewGroup.layout(ViewGroup.java:5041)
// at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
// at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
// at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
// at android.view.View.layout(View.java:15725)
// at android.view.ViewGroup.layout(ViewGroup.java:5041)
// at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
// at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
// at android.view.View.layout(View.java:15725)
// at android.view.ViewGroup.layout(ViewGroup.java:5041)
// at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2105)
// at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1862)
// at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1080)
// at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5933)
// at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
// at android.view.Choreographer.doCallbacks(Choreographer.java:604)
// at android.view.Choreographer.doFrame(Choreographer.java:574)
// at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
// at android.os.Handler.handleCallback(Handler.java:739)
// at android.os.Handler.dispatchMessage(Handler.java:95)
// at android.os.Looper.loop(Looper.java:135)
// at android.app.ActivityThread.main(ActivityThread.java:5380)
// at java.lang.reflect.Method.invoke(Native Method)
// at java.lang.reflect.Method.invoke(Method.java:372)
// at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:939)

主要跟踪的方向是:

//  at android.widget.Gallery.setUpChild(Gallery.java:893)
//  at android.widget.Gallery.makeAndAddView(Gallery.java:872)

//  at android.widget.Gallery.layout(Gallery.java:669)

在这Galley类中出现了异常:

主要是在setUpChild的方法下 子view变量child是空的导致的:


说明在makeAndAddView的view穿进去的时候为空,不在贴图了,最后的出的结论是:

是由于GalleryAdapter的getview方法返回为空的时候导致了出现空指针异常

其实本人在写的时候出现了数组越界异常,所以,在getview之前就做了判断

public View getView(int position, View convertView, ViewGroup parent) {if (position < 0 || mItems == null || position >= mItems.size()) {return <span style="font-family: Arial;">convertView;</span>
}
又因为,Android底层是C++编写的,很多默认是为null,所以导致出现上面异常。

解决方案:

1、可以设置一个默认view传递给convertView:

2、防止数组越界,就不会进入这个判断,按正常的流程走,不会出现此异常(个人是因为集合直接赋值,

集合直接赋值会报原集合的物理地址传给新的集合,建议用addALL()方法,这样不会因为其他操作,导致数据越界)



以上是个人在开发中出现的问题,拿出来和大家分享,欢迎参考和知道。



  相关解决方案