我想旋轉我的腦袋周圍的Asynctasks和線程之類的,我想我有一個很好的解決方案,我做了我所有的Asynctask類的計算,發送它們使用onCallComplete返回到我的活動,將所有變量添加到單獨的數組中,然後將該數組發送到列表視圖適配器。然而,由於某種原因,我似乎無法理解,Android正在拋出我一個java.lang.NullPointerException: Attempt to read from null array
。我確信這很明顯,但我不知道如何解決,所以任何建議將不勝感激,謝謝!Android - 不知道爲什麼我的數組爲空
活動的onCallComplete(發生錯誤)
public void onCallComplete(int listSize, String[] titles, String[] prices, Bitmap[] images, String[] productURLs){
ItemList[]itemArray= new ItemList[listSize];
int i =0;
while(i<listSize){
itemArray[i] = new ItemList();
itemArray[i].setTitle(titles[i]); //error occurs here
itemArray[i].setPrice(prices[i]);
itemArray[i].setImage(images[i]);
itemArray[i].setProductURL(productURLs[i]);
i++;
}
ListAdapter adapter = new CustomAdapter(this, itemArray);
ListView productListView = (ListView)findViewById(R.id.productListView);
productListView.setAdapter(adapter);
}
ITEMLIST類:
public class ItemList {
private String title;
private String price;
private Bitmap image;
private String productURL;
public ItemList(){
this.title=null;
this.price=null;
this.image=null;
this.productURL=null;
}
void setTitle(String title){
this.title = title;
}
void setImage(Bitmap image){
this.image = image;
}
void setPrice(String price){
this.price = price;
}
void setProductURL(String productURL){
this.productURL = productURL;
}
String getTitle(){
return title;
}
String getPrice(){
return price;
}
Bitmap getImage(){
return image;
}
String getProductURL(){
return productURL;
}
}
編輯:所以經過一些試驗和拉金德倫指針我得出的結論,這是我的AsyncTask計算返回null(去圖,Asynctask總是對我來說是一個問題)。這裏是AsyncTask的代碼。
public class GetProductAttributes extends AsyncTask<Object, Object, Void> {
OnCallCompleteCallBack callback;
String url;
int listSize;
int index;
String[] productURLs;
String[] prices;
String[] titles;
String imageSRC;
Bitmap[] images;
ImageView productView;
int listSize;
int result;
public GetProductAttributes(String url, int index, OnCallCompleteCallBack callback) {
this.url = url;
this.index = index;
this.callback = callback;
}
protected Void doInBackground(Object... voids) {
//Create JSoup connection
try {
Document doc = Jsoup.connect(url).get();
String link = doc.select("h2#s-result-count").first().text();
System.out.println(link);
System.out.println(link.substring(1));
if (link.substring(1, 2).equals("-")) {
System.out.println("run1");
listSize = Integer.parseInt(link.substring(2, 3));
System.out.println(listSize);
try {
listSize = Integer.parseInt(link.substring(2, 4));
System.out.println(listSize);
} catch (Exception e) {
}
} else {
System.out.println("run2");
listSize = Integer.parseInt(link.substring(0, 1));
System.out.println(listSize);
try {
listSize = Integer.parseInt(link.substring(0, 2));
System.out.println(listSize);
} catch (Exception e) {
}
}
titles = new String[listSize];
prices = new String[listSize];
productURLs = new String[listSize];
images = new Bitmap[listSize];
int i = 0;
while (i < listSize) {
Elements basicLink = doc.select("div.showRightCol")
.select("div.leftCol")
.select("div.a-row.s-result-list-parent-container")
.select("ul.s-result-list.s-col-1.s-col-ws-1.s-result-list-hgrid.s-height-equalized.s-list-view.s-text-condensed")
.select("li[id=result_" + i + "]")
.select("div.s-item-container")
.select("div.a-fixed-left-grid")
.select("div.a-fixed-left-grid-inner");//start here to get to everything
Element element = basicLink.select("a.a-link-normal.s-access-detail-page.s-color-twister-title-link.a-text-normal").first();
String title = element.attr("title");
//System.out.println("Title is: " + title);
titles[i] = title;
// Gets product URL and image source
Elements longLink = basicLink.select("div.a-fixed-left-grid-col.a-col-left")
.select("div.a-row")
.select("div.a-column.a-span12.a-text-center")
.select("a.a-link-normal.a-text-normal");
String productURL = longLink.attr("href");
//System.out.println(productURL);
productURLs[i] = productURL;
imageSRC = longLink.select("img.s-access-image.cfMarker").attr("src");
//System.out.println(imageSRC);
images[i] = getImage(imageSRC);
//Gets price
String price = basicLink.select("div.a-row")
.select("div.a-column.a-span7")
.select("div.a-row.a-spacing-mini")
.select("div.a-row.a-spacing-none")
.select("a.a-size-small.a-link-normal.a-text-normal")
.select("span.a-size-base.a-color-base").first().text();
System.out.println(price);
prices[i]=price;
i++;
}
//Gets title
} catch (Exception e) {
}
return null;
}
可能是您的**標題[] **爲空,請添加您的完整代碼 –
[什麼是NullPointerException,以及如何解決它?](https://stackoverflow.com/questions/218384)/what-is-a-nullpointerexception-and-how-do-i-fix-it) – khelwood
一秒鐘,添加其餘的代碼。 – coolyfrost