0

I really don't know what to do anymore...

I got this code (tried many before):

final LoaderImageView image = (LoaderImageView) findViewById(R.id.loaderImageView);
        image.setImageDrawable(getAnImageUrl());  *<--- is line 119, where logcat say the NullPointer is*emphasized text*

private String getAnImageUrl() {
        return "http://i.imgur.com/n6jBV.jpg";
    }

This should show an image, I also tried many other ways I found on the internet, But it always says:

08-15 12:37:57.770: E/AndroidRuntime(19455): FATAL EXCEPTION: main
08-15 12:37:57.770: E/AndroidRuntime(19455): java.lang.RuntimeException: Unable to start activity 

ComponentInfo{com.dlv.listactivity/com.dlv.listactivity.MainActivity}: java.lang.NullPointerException
08-15 12:37:57.770: E/AndroidRuntime(19455):    at android.app.ActivityThread.performLaunchActivity

(ActivityThread.java:1651)
08-15 12:37:57.770: E/AndroidRuntime(19455):    at android.app.ActivityThread.handleLaunchActivity

(ActivityThread.java:1667)
08-15 12:37:57.770: E/AndroidRuntime(19455):    at android.app.ActivityThread.access$1500

(ActivityThread.java:117)
08-15 12:37:57.770: E/AndroidRuntime(19455):    at android.app.ActivityThread$H.handleMessage

(ActivityThread.java:935)
08-15 12:37:57.770: E/AndroidRuntime(19455):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-15 12:37:57.770: E/AndroidRuntime(19455):    at android.os.Looper.loop(Looper.java:123)
08-15 12:37:57.770: E/AndroidRuntime(19455):    at android.app.ActivityThread.main

(ActivityThread.java:3691)
08-15 12:37:57.770: E/AndroidRuntime(19455):    at java.lang.reflect.Method.invokeNative(Native 

Method)
08-15 12:37:57.770: E/AndroidRuntime(19455):    at java.lang.reflect.Method.invoke(Method.java:507)
08-15 12:37:57.770: E/AndroidRuntime(19455):    at com.android.internal.os.ZygoteInit

$MethodAndArgsCaller.run(ZygoteInit.java:847)
08-15 12:37:57.770: E/AndroidRuntime(19455):    at com.android.internal.os.ZygoteInit.main

(ZygoteInit.java:605)
08-15 12:37:57.770: E/AndroidRuntime(19455):    at dalvik.system.NativeStart.main(Native Method)
08-15 12:37:57.770: E/AndroidRuntime(19455): Caused by: java.lang.NullPointerException
08-15 12:37:57.770: E/AndroidRuntime(19455):    at com.dlv.listactivity.MainActivity.onCreate

(MainActivity.java:119)
08-15 12:37:57.770: E/AndroidRuntime(19455):    at android.app.Instrumentation.callActivityOnCreate

(Instrumentation.java:1047)
08-15 12:37:57.770: E/AndroidRuntime(19455):    at android.app.ActivityThread.performLaunchActivity

(ActivityThread.java:1615)

About that he gets a NullPointer.
I also already give permission to the internet (Tried every single one of them I found on the internet by now):

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.DELETE_CACHE_FILES">

And this is my list.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">
  <com.dlv.listactivity.LoaderImageView
        android:id="@+id/loaderImageView"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"/>
      <!--  image="http://developer.android.com/images/dialog_buttons.png" -->

  <ImageView
      android:id="@+id/img"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:src="@drawable/ic_launcher" />

  <TextView
      android:id="@+id/title"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_weight="1" android:layout_gravity="left|center_vertical" android:textSize="20dip" android:layout_marginLeft="10dip"/>
</LinearLayout>

I use a listActivity (instead of the normal Activity), and I got 2 xml's that I combine with inflate.

I even got other projects where I can download/show picture from the internet... But this one doesn't and I can't find the problem...

Can the inflate be a problem? or something else?
Please give me some suggestions.

Thanks Already, Bigflow

Bigflow
  • 3,616
  • 5
  • 29
  • 52
  • The stack trace says it's a NullPointerException at MainActivity.java line 119. What does that line say? – Petter Aug 15 '12 at 10:57
  • @Petter `image.setImageDrawable(getAnImageUrl());` – Bigflow Aug 15 '12 at 10:58
  • Check this - there is a good sample to download and figure out how to download images, orginize list etc http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview – dilix Aug 15 '12 at 10:59
  • @Bigflow Ok, then `image` is `null`. Which means findViewById() can't find a view with that id. – Petter Aug 15 '12 at 11:04
  • @Petter So then it has to do something with the `inflate` thing I think that is the reason why it can't "find" it. Will look further. – Bigflow Aug 15 '12 at 11:06
  • @Petter I finally got it, because I used inflate on my list.xml, that will make him "disappear" and that is because he couldn't find it. I now got that straight. I was getting really frustrating (this problem was going on for several days now). Can eat and sleep in peace now :). Thanks for the help!! – Bigflow Aug 15 '12 at 11:27

1 Answers1

1

Your layout doesn't have a LoaderImageView with the ID R.id.loaderImageView. That's why it's Null.

I believe you're using this: http://blog.blundell-apps.com/imageview-with-loading-spinner/ but forgot to declare it on the layout:

 <com.blundell.tut.LoaderImageView
   android:id="@+id/loaderImageView"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content" />
Budius
  • 39,391
  • 16
  • 102
  • 144