1

我正在尝试用 ImageViews 填充数组

更新 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();

公共类 MenueAdapter 扩展 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; 
}

}

imageDownloader.dowload() 返回一个 ImageView。问题似乎是,我的 ImageView img 总是为空。

我已经尝试过一些类似的东西: Null ImageView Reference

但不幸的是,我通过在 xml 文件中添加 id-tag 得到一条错误消息...:/

这是我的 XML 文件:

    <?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> 

我希望这是处理 strictMode 问题的正确方法,如果你们中的任何人知道得更好,请告诉我。

日志:

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)

但原来的问题依然存在。

4

1 回答 1

4

如在日志中:

android.view.ViewRootImpl$CalledFromWrongThreadException: 只有创建视图层次结构的原始线程才能触摸

因为您正在尝试从TimertaskThread 访问 ImageView。使用Activity.runOnUiThread从其他线程更新 Ui

于 2013-03-11T13:51:39.530 回答