ImageView で配列を埋めようとしています
更新 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 は 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 リファレンス
しかし、残念ながら、xml ファイルに id タグを追加すると、エラー メッセージが表示されます... :/
ここに私の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)
しかし、元の問題はまだ存在します。