我正在尝试使用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。
我已经尝试过以下方法: 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)
但最初的问题仍然存在。
答案 0 :(得分:4)
在日志中:
android.view.ViewRootImpl $ CalledFromWrongThreadException:只有 创建视图层次结构的原始线程可以触摸
因为您尝试从Timertask
线程访问ImageView。使用Activity.runOnUiThread从其他线程更新Ui