1

I'm trying to fill an Array with ImageViews

UPDATE 16:32:

    runOnUiThread(new Runnable() {
        public void run() {             
            try{                                    
                View inflatedView = getLayoutInflater().inflate(R.layout.mainview_menue_item_row, null);

                imageList = new ImageView[URLS.length];
                System.out.println(URLS.length);

                for(int x = 0; x < URLS.length; x++)
                {
                    ImageView img = (ImageView) inflatedView.findViewById(R.id.menueicon);

                    StrictMode.ThreadPolicy old = StrictMode.getThreadPolicy();
                    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder(old)
                        .permitDiskWrites()
                        .permitNetwork()
                        .build());
                    imageDownloader.download(URLS[x], img);
                    StrictMode.setThreadPolicy(old);
                    //imageDownloader.download(URLS[x], img);
                    imageList[x] = img;     



                }                   
               } catch (Exception e) {
                   e.printStackTrace(); 
               }            
            }
    }); 



    MenueAdapter adapter = new MenueAdapter(MainActivity.this, 
            R.layout.mainview_menue_item_row, menue_data, imageList);
    listView1.setAdapter(adapter);

    HandleInput();

public class MenueAdapter extends ArrayAdapter{

Context context; 

int layoutResourceId;
Menue data[] = null;
ImageView imageList[] = null; 

int destWidth;   
int destHeight; 



public MenueAdapter(Context context, 
        int layoutResourceId, 
        Menue[] data, 
        ImageView[] imageList) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId; 
    this.context= context; 
    this.data = data;
    this.imageList = imageList; 
    this.destWidth = 0; 
    this.destHeight = 0; 
}

@Override
public View getView(int position, View convertView,
        final ViewGroup parent) {       

    View row = convertView; 
    MenueHolder holder = null; 

    if (row == null) {
        LayoutInflater inflater = 
                ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false); 

        holder = new MenueHolder(); 

        holder.imgIcon = (ImageView)row.findViewById(R.id.menueicon);
        holder.txtHolder = (TextView)row.findViewById(R.id.textInRow); 
        holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle); 

        row.setTag(holder); 
    }
    else {
        holder = (MenueHolder)row.getTag(); 
    }

    Menue menue = data[position];
    holder.imgIcon = imageList[position]; 

    holder.txtHolder.setText(menue.txtHolder); 
    holder.txtTitle.setText(menue.title); 

    return row;

}

static class MenueHolder {  
    ImageView imgIcon; 
    TextView txtHolder; 
    TextView txtTitle; 
}

}

The imageDownloader.dowload() returns a ImageView. The issue seems to be that, my ImageView img always is null.

I already tried some things like: Null ImageView Reference

But unfortunately i get a error message by adding the id-tag in the xml file... :/

Here my Xml File:

    <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="left|center"
    android:paddingBottom="5px"
    android:paddingLeft="5px"
    android:paddingTop="5px" >
   <ImageView
       android:id="@+id/menueicon"
       android:adjustViewBounds="true"
       android:layout_width="wrap_content"      
       android:maxWidth ="200dp"       
       android:layout_height="fill_parent"      
       android:layout_marginRight="6dip"
       android:paddingLeft="2dp"      
       android:scaleType="centerCrop"/>"
    <LinearLayout
       android:orientation="vertical"
       android:layout_width="0dip"
       android:layout_weight="1"
       android:layout_height="fill_parent">       
    <TextView android:id="@+id/txtTitle"
    android:layout_width="wrap_content"
    android:textColor="#FF8600"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10px"    
    android:gravity="center"/>
    <TextView android:id="@+id/textInRow"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginLeft="10px"
       android:textColor="#0099CC"/>   
    </LinearLayout>
    </LinearLayout> 

I hope thats the proper way to handle strictMode issues, if any of u know any better, pls let me know.

LOG:

03-11 13:57:53.749: W/System.err(9562): java.lang.NullPointerException
03-11 13:57:53.759: W/System.err(9562):     at com.example.listview.MainActivity$1.run(MainActivity.java:150)
03-11 13:57:53.759: W/System.err(9562):     at android.app.Activity.runOnUiThread(Activity.java:4644)
03-11 13:57:53.759: W/System.err(9562):     at com.example.listview.MainActivity.onCreate(MainActivity.java:125)
03-11 13:57:53.759: W/System.err(9562):     at android.app.Activity.performCreate(Activity.java:5104)
03-11 13:57:53.759: W/System.err(9562):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-11 13:57:53.769: W/System.err(9562):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-11 13:57:53.769: W/System.err(9562):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-11 13:57:53.769: W/System.err(9562):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-11 13:57:53.769: W/System.err(9562):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-11 13:57:53.769: W/System.err(9562):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-11 13:57:53.769: W/System.err(9562):     at android.os.Looper.loop(Looper.java:137)
03-11 13:57:53.769: W/System.err(9562):     at android.app.ActivityThread.main(ActivityThread.java:5041)
03-11 13:57:53.769: W/System.err(9562):     at java.lang.reflect.Method.invokeNative(Native Method)
03-11 13:57:53.769: W/System.err(9562):     at java.lang.reflect.Method.invoke(Method.java:511)
03-11 13:57:53.769: W/System.err(9562):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-11 13:57:53.769: W/System.err(9562):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-11 13:57:53.781: W/System.err(9562):     at dalvik.system.NativeStart.main(Native Method)

But the original problem still exists.

1
  • please post your error log.
    – s_bei
    Mar 11, 2013 at 13:43

1 Answer 1

4

as in Log :

android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch

because you are trying to access ImageView from Timertask Thread. use Activity.runOnUiThread for updating Ui from other Thread

5
  • Hey thx for the quick answer, but that seems not to be the solution.
    – luQ
    Mar 11, 2013 at 13:59
  • The imageDownloader is based on this example android-developers.blogspot.de/2010/07/… and worked just fine at a ealier state
    – luQ
    Mar 11, 2013 at 14:03
  • I did like u say, but no i run into another issue, would u please be so kind and take a look, i updated my question. Thx
    – luQ
    Mar 11, 2013 at 14:49
  • @user2139007 : where u are setting bitmap to image View? Mar 11, 2013 at 15:21
  • @user2139007 : try pastebin.com/Q4wXZz6m code and let me known it's working or not just change you MenueAdapter code and comment timertask execution Mar 11, 2013 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.