2011-03-09 55 views
2

的代碼如下Listview.setadapter提高空指針異常

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    Intent i = getIntent(); 

     ArrayList<String> imageUrl = i.getStringArrayListExtra("ImageUrl"); 
     String[] companyImageUrl = imageUrl.toArray(new String[0]); 
      ListView companyList = null; 
      LazyAdapter companyListViewAdapter; 

      companyList = (ListView)findViewById(R.id.companylist); 

      companyListViewAdapter = new LazyAdapter(companyListView.this , companyImageUrl); 

      try { 
       companyList.setAdapter(companyListViewAdapter);//exception here 
      } catch (Exception e) { 
       System.out.println(e); 
      } 
      setContentView(R.layout.company_list); 
} 

編輯:Lazyadapter代碼添加

public class LazyAdapter extends BaseAdapter { 

private Activity activity; 
private String[] data; 
private static LayoutInflater inflater=null; 
public ImageLoader imageLoader; 

public LazyAdapter(Activity a, String[] d) { 
    activity = a; 
    data=d; 
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    imageLoader= new ImageLoader(activity.getApplicationContext()); 
} 

public int getCount() { 
    return data.length; 
} 

public Object getItem(int position) { 
    return position; 
} 

public long getItemId(int position) { 
    return position; 
} 

public static class ViewHolder{ 
    public TextView text; 
    public ImageView image; 
} 

public View getView(int position, View convertView, ViewGroup parent) { 
    View vi=convertView; 
    ViewHolder holder; 
    UrlList urlList; 

    urlList = XMLParser.urlList; 

    if(convertView==null){ 
     vi = inflater.inflate(R.layout.item, null); 
     holder=new ViewHolder(); 
     holder.text=(TextView)vi.findViewById(R.id.text);; 
     holder.image=(ImageView)vi.findViewById(R.id.image); 
     vi.setTag(holder); 
    } 
    else 
     holder=(ViewHolder)vi.getTag(); 

    String[] categoryNameStrings = urlList.getCategoryName().toArray(new String[0]); 

    holder.text.setText(categoryNameStrings[position]); 
    holder.image.setTag(data[position]); 
    imageLoader.DisplayImage(data[position], activity, holder.image); 
    return vi; 
} 

}

+0

我做一個標籤欄的應用程序。第二個選項卡有一個列表視圖。此列表視圖導致另一個列表視圖。我創建了一個新的活動fi =或第二個listview,並從之前的活動中獲得一個Arraylist。這個listview是當我在前面的listview中選擇一個單元格的時候。 – pradeep 2011-03-09 11:03:28

+0

你可以發佈錯誤日誌嗎? – longhairedsi 2011-03-09 11:09:35

回答

2

,就把這行的super.onCreate(savedInstanceState);

setContentView(R.layout.company_list); 
5

使用setContentView(R.layout.company_list);super.onCreate(savedInstanceState);

+0

是的,它解決了,但真正的問題是,列表視圖顯示空,我有趣的lazyAdapter加載圖像視圖和文本視圖到列表視圖,但它不工作。 – pradeep 2011-03-09 11:17:31

+0

你能顯示LazyAdapter代碼嗎? – 2011-03-09 11:21:23

+0

我已經發布了它 – pradeep 2011-03-09 11:25:58

0

這是什麼?

UrlList urlList; 
urlList = XMLParser.urlList; 

它在這裏做什麼?

String[] categoryNameStrings = urlList.getCategoryName().toArray(new String[0]); 

您可以發佈錯誤日誌嗎?

1

得到錯誤,當這樣首先需要確保給予正確的XML文件作爲

setContentView(R.layout.main)

listview id (i.e) findViewById(R.id.listview1);