尝试调用虚方法' int java.lang.String.hashCode()'

时间:2015-10-13 11:35:08

标签: android gridview android-volley android-logcat networkimageview

我尝试使用Volley Library将图片从URL加载到girdview中。为此,请关注this教程。当我运行我的项目时,它从服务器数据库收集所有URL,然后存储在arraylist中。但是没有显示图像。我收到java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference错误。它甚至没有指向我遇到此错误的任何代码行数。请参阅下面的Logcat输出:

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference
        at com.android.volley.Request.<init>(Request.java:136)
        at com.android.volley.toolbox.ImageRequest.<init>(ImageRequest.java:71)
        at com.android.volley.toolbox.ImageLoader.get(ImageLoader.java:219)
        at com.android.volley.toolbox.ImageLoader.get(ImageLoader.java:171)
        at com.android.volley.toolbox.NetworkImageView.loadImageIfNecessary(NetworkImageView.java:141)
        at com.android.volley.toolbox.NetworkImageView.onLayout(NetworkImageView.java:190)
        at android.view.View.layout(View.java:15596)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
        at android.view.View.layout(View.java:15596)
        at android.view.ViewGroup.layout(ViewGroup.java:4966)
        at android.widget.GridView.setupChild(GridView.java:1542)
        at android.widget.GridView.makeAndAddView(GridView.java:1436)
        at android.widget.GridView.makeRow(GridView.java:361)
        at android.widget.GridView.fillDown(GridView.java:302)
        at android.widget.GridView.fillFromTop(GridView.java:437)
        at android.widget.GridView.layoutChildren(GridView.java:1276)
        at android.widget.AbsListView.onLayout(AbsListView.java:2148)
        at android.view.View.layout(View.java:15596)
        at android.view.ViewGroup.layout(ViewGroup.java:4966)
        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:15596)
        at android.view.ViewGroup.layout(ViewGroup.java:4966)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
        at android.widget.ScrollView.onLayout(ScrollView.java:1502)
        at android.view.View.layout(View.java:15596)
        at android.view.ViewGroup.layout(ViewGroup.java:4966)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
        at android.view.View.layout(View.java:15596)
        at android.view.ViewGroup.layout(ViewGroup.java:4966)
        at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:494)
        at android.view.View.layout(View.java:15596)
        at android.view.ViewGroup.layout(ViewGroup.java:4966)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
        at android.view.View.layout(View.java:15596)
        at android.view.ViewGroup.layout(ViewGroup.java:4966)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2072)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1829)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
        at android.view.Choreographer.doCallbacks(Choreographer.java:580)
        at android.view.Choreographer.doFrame(Choreographer.java:550)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
        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:5221)
        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:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

有人可以帮我解决确切的问题吗?我该如何解决?

7 个答案:

答案 0 :(得分:5)

您应该检查从服务器获取数据的图像的空值。

答案 1 :(得分:1)

将字符串值设为null,对于此实例显示String.hashcode()错误

答案 2 :(得分:0)

我有同样的问题。刚找到我认为的解决方法。在要尝试与之通信的服务器的链接中,指定通信协议,即“ http://”或“ https://”,然后再添加域和所有域。这为我解决了。希望对您有帮助

答案 3 :(得分:0)

我在另一个内容中遇到此错误,并通过在Android Studio中更新Android SDK进行了修复。 也许有帮助。

答案 4 :(得分:0)

我的服务器是https,所以收到了另一个错误。我删除了https并仅保留了域名,但该域名无法正常工作,因此我最终放置了https:并联系我的主机以简化域上的安全规则或为特定域创建特殊规定。检查网站网址的实现位置,并根据您的网站在浏览器中的外观添加http或https。

答案 5 :(得分:0)

使用适当的@serializedName 注释更改 api 响应(数据类)。

答案 6 :(得分:-1)

检查清单。确保使用权限Internet在此处写。