Hi,
Ich versuche mich gerade an einer ListView mit Icons.
Aktuell sieht es folgendermaßen aus:
Main Activity:
Java
public void klickmich(View view){
setContentView(R.layout.layoutexample);
ArrayList<ItemDetails> image_details = GetSearchResults();
final ListView lv1 = (ListView) findViewById(R.id.listV_main);
lv1.setAdapter(new ItemListBaseAdapter(this, image_details));
private ArrayList<ItemDetails> GetSearchResults() {
ArrayList<ItemDetails> results = new ArrayList<ItemDetails>();
ItemDetails item_details = new ItemDetails();
item_details.setName("List 1");
item_details.setItemDescription("Example");
item_details.setImageNumber(1);
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("List 2");
item_details.setItemDescription("Example");
item_details.setImageNumber(2);
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("List 3");
item_details.setItemDescription("Example");
item_details.setImageNumber(3);
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("List 4");
item_details.setItemDescription("Example");
item_details.setImageNumber(4);
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("List 5");
item_details.setItemDescription("Example");
item_details.setImageNumber(5);
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("List 6");
item_details.setItemDescription("Example");
item_details.setImageNumber(6);
results.add(item_details);
return results;
}
}
Alles anzeigen
ItemDetails.java
Java
public class ItemDetails {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getItemDescription() {
return itemDescription;
}
public void setItemDescription(String itemDescription) {
this.itemDescription = itemDescription;
}
public int getImageNumber() {
return imageNumber;
}
public void setImageNumber(int imageNumber) {
this.imageNumber = imageNumber;
}
private String name ;
private String itemDescription;
private int imageNumber;
}
Alles anzeigen
ItemListAdapter.java
Java
public class ItemListBaseAdapter extends BaseAdapter {
private static ArrayList<ItemDetails> itemDetailsrrayList;
private Integer[] imgid = {
R.drawable.flag,
R.drawable.processing,
R.drawable.agent,
R.drawable.network,
R.drawable.email,
R.drawable.mobile2
};
private LayoutInflater l_Inflater;
public ItemListBaseAdapter(Context context, ArrayList<ItemDetails> results) {
itemDetailsrrayList = results;
l_Inflater = LayoutInflater.from(context);
}
public int getCount() {
return itemDetailsrrayList.size();
}
public Object getItem(int position) {
return itemDetailsrrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = l_Inflater.inflate(R.layout.item_details_view, null);
holder = new ViewHolder();
holder.txt_itemName = (TextView) convertView.findViewById(R.id.name);
holder.txt_itemDescription = (TextView) convertView.findViewById(R.id.itemDescription);
holder.itemImage = (ImageView) convertView.findViewById(R.id.photo);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txt_itemName.setText(itemDetailsrrayList.get(position).getName());
holder.txt_itemDescription.setText(itemDetailsrrayList.get(position).getItemDescription());
holder.itemImage.setImageResource(imgid[itemDetailsrrayList.get(position).getImageNumber() - 1]);
return convertView;
}
static class ViewHolder {
TextView txt_itemName;
TextView txt_itemDescription;
ImageView itemImage;
}
}
Alles anzeigen
Dies funktioniert jedoch nicht:
Java
07-23 12:23:44.373 7837-7837/? E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.developerelite.asr/de.developerelite.asr.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
at android.app.ActivityThread.access$600(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5227)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at de.developerelite.asr.MainActivity.onCreate(MainActivity.java:62)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2262)
... 11 more
Alles anzeigen
mfg ternes3