URL getContent中的NullPointerException

时间:2011-11-27 21:54:51

标签: android url nullpointerexception

我的app中出现了崩溃错误。错误是

Exception class java.lang.NullPointerException
Source method URLConnection.guessContentTypeFromStream()

java.lang.NullPointerException
at java.net.URLConnection.guessContentTypeFromStream(URLConnection.java:675)
at java.net.URLConnection.getContent(URLConnection.java:162)
at java.net.URL.getContent(URL.java:621)
at kostas.menu.olympiakos.nea.onListItemClick(nea.java:160)
at android.app.ListActivity$2.onItemClick(ListActivity.java:321)
at android.widget.AdapterView.performItemClick(AdapterView.java:284)
at android.widget.ListView.performItemClick(ListView.java:3382)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
at dalvik.system.NativeStart.main(Native Method)

名为"nea"的类正在加载一些带有图像的RSS,而崩溃行是

 try {

 (nea.java:160) Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imagee).getContent());
                image1.setImageBitmap(bitmap); 
              } catch (MalformedURLException e) {
               //log exception here
              } catch (IOException e) {
                //log exception here
              }  

我的应用程序崩溃了,因为没有要加载的图像,或问题出在其他地方?谢谢

编辑: 请记住:

List<String> imgl = new ArrayList<String>();

   String imagee=imgl.get(position).toString();

@aster:

我取代了这个:

 Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imagee).getContent());
                image1.setImageBitmap(bitmap); 

与此:

URL url = new URL(imagee);
                InputStream is = (InputStream) url.getContent();
                Bitmap bitmap = BitmapFactory.decodeStream(is);
                image1.setImageBitmap(bitmap); 

1 个答案:

答案 0 :(得分:0)

您可以轻松测试。

只需声明变量来存储中间结果,如下所示:

Url url = new Url(imagee);
InputStream is = (InputStream) url.getContent();
Bitmap bitmap = BitmapFactory.decodeStream(is);

只有一个简单的调用就会让你的线路崩溃,你很容易知道要检查或纠正的对象。