出错通过JSON一个ListView加载图像 [英] Error while loading images in a ListView through JSON
问题描述
我想在ListView中,但得到错误加载数据和图像。
FoodView.java
公共类FoodView扩展ListActivity
{
私人ProgressDialog pDialog;
JSONParser jParser =新JSONParser();
ArrayList的<&HashMap的LT;字符串,字符串>>产品列表; 私人静态字符串url_all_products =http://ambrogroup.com/app/index/file/food; 私有静态最后弦乐TAG_SUCCESS =成功;
私有静态最后弦乐TAG_PRODUCTS =产品;
私有静态最后弦乐TAG_PID =PID;
私有静态最后弦乐TAG_NAME =名;
私有静态最后弦乐TAG_IMG =img目录;
私有静态最后弦乐TAG_PRICE =价格;
私有静态最后弦乐TAG_DESTINATION =目的地; JSONArray产品= NULL;
LV的ListView;
LazyImageLoadAdapter适配器; 私有String []海峡; @覆盖
公共无效的onCreate(捆绑savedInstanceState)
{
super.onCreate(savedInstanceState); 产品列表=新的ArrayList<&HashMap的LT;字符串,字符串>>();
新LoadAllProducts()执行();
LV = getListView(); lv.setOnItemClickListener(新OnItemClickListener(){
@覆盖
公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的ID)
{
/ *
字符串的pid =((的TextView)view.findViewById(R.id.pid))的getText()的toString()。
字符串PNAME =((的TextView)view.findViewById(R.id.name))的getText()的toString()。
在意向=新意图(getApplicationContext(),EditProductActivity.class);
in.putExtra(TAG_PID,PID);
startActivityForResult(在100);
* /
}
});
}
@覆盖
保护无效的onActivityResult(INT申请code,INT结果code,意图数据)
{
super.onActivityResult(要求code,结果code,数据);
如果(结果code == 100)
{
意向意图= getIntent();
完();
startActivity(意向);
} } 类LoadAllProducts扩展的AsyncTask<字符串,字符串,字符串>
{ @覆盖
在preExecute保护无效()
{
super.on preExecute();
pDialog =新ProgressDialog(FoodView.this);
pDialog.setMessage(载入列表请稍候...);
pDialog.setIndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
} 保护字符串doInBackground(字符串参数... args){
清单<&的NameValuePair GT; PARAMS =新的ArrayList<&的NameValuePair GT;();
JSONObject的JSON = jParser.makeHtt prequest(url_all_products,GET,则params);
Log.d(Gellting所有列表:,json.toString()); 尝试{
INT成功= json.getInt(TAG_SUCCESS);
如果(成功== 1)
{
产品= json.getJSONArray(TAG_PRODUCTS);
海峡=新的String [products.length()];
的for(int i = 0; I< products.length();我++)
{
JSONObject的C = products.getJSONObject(I)
字符串ID = c.getString(TAG_PID);
字符串名称= c.getString(TAG_NAME);
串价格= c.getString(TAG_PRICE);
串目的地= c.getString(TAG_DESTINATION);
字符串IMG = c.getString(TAG_IMG); 海峡[I] = IMG; HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>(); map.put(TAG_PID,身份证);
map.put(TAG_NAME,名);
map.put(TAG_PRICE,价格);
map.put(TAG_DESTINATION,目的地);
map.put(TAG_IMG,IMG); productsList.add(地图);
}
}
其他
{ / *意向书I =新意图(getApplicationContext(),NewProductActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(ⅰ); * /
}
}赶上(JSONException E){
e.printStackTrace();
} 返回null;
} 保护无效onPostExecute(字符串FILE_URL)
{
pDialog.dismiss();
runOnUiThread(新的Runnable()
{
公共无效的run()
{
ListView控件列表;
LazyImageLoadAdapter适配器;
名单=(ListView控件)findViewById(R.id.list1);
适配器=新LazyImageLoadAdapter(FoodView.this,STR,产品列表);
list.setAdapter(适配器);
}
}); } }
}
LazyImageLoaderAdapter.java
包com.ambrogroup;进口的java.util.ArrayList;
进口的java.util.HashMap;进口android.app.Activity;
进口android.content.Context;
进口android.util.Log;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.view.View.OnClickListener;
进口android.widget.BaseAdapter;
进口android.widget.ImageView;
进口android.widget.ListAdapter;
进口android.widget.ListView;
进口android.widget.SimpleAdapter;
进口android.widget.TextView;//适配器类BaseAdapter延伸并与OnClickListener实现
公共类LazyImageLoadAdapter扩展BaseAdapter实现OnClickListener { 私人活动活动;
私有String [] IIMAGE,IID,INAME,iPrice,iDestination;
私有静态LayoutInflater吹气= NULL;
公共ImageLoader的ImageLoader的;
ArrayList的<&HashMap的LT;字符串,字符串>> plist中;
私有静态最后弦乐TAG_PID =PID,TAG_NAME =名,TAG_DESTINATION =目的地,TAG_PRICE =价格; 公共LazyImageLoadAdapter(活动一,的String [] sImage,ArrayList的<&HashMap的LT;字符串,字符串>>产品列表){
活性=一个;
IIMAGE = sImage;
吹气=(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ImageLoader的=新ImageLoader的(activity.getApplicationContext());
PLIST =产品列表; } 公众诠释的getCount(){
返回iImage.length;
} 公共对象的getItem(INT位置){
返回的位置;
} 众长getItemId(INT位置){
返回的位置;
} / *********创建一个holder类包含虚增XML文件中的元素********* /
公共静态类ViewHolder
{
公共ImageView的VIMAGE;
公众的TextView TNAME;
公众的TextView tPrice;
公众的TextView tDestination;
} 公共查看getView(INT位置,查看convertView,父母的ViewGroup){ 查看VI = convertView;
ViewHolder持有人; 如果(convertView == NULL)
{
VI = inflater.inflate(R.layout.list_view,NULL); 持有人=新ViewHolder();
holder.tName =(TextView中)vi.findViewById(R.id.name);
holder.tPrice =(TextView中)vi.findViewById(R.id.price);
holder.tDestination =(TextView中)vi.findViewById(R.id.destination);
holder.vImage =(ImageView的)vi.findViewById(R.id.img); vi.setTag(保持器);
}
其他
支架=(ViewHolder)vi.getTag();
holder.tName.setText(「本公司」+位置);
holder.tPrice.setText(公司介绍+位置);
ImageView的图像= holder.vImage; imageLoader.DisplayImage(IIMAGE [位置],图像); vi.setOnClickListener(新OnItemClickListener(位置));
返回VI;
} @覆盖
公共无效的onClick(查看为arg0){
// TODO自动生成方法存根 }
当项目在ListView中单击名为/ ********* ************ /
私有类OnItemClickListener实现OnClickListener {
私人诠释mPosition; OnItemClickListener(INT位置){
mPosition =位置;
} @覆盖
公共无效的onClick(查看为arg0){
// FoodView SCT =(FoodView)活性;
//sct.onItemClick(mPosition);
}
}
}
跟随误差荫面临
logcat的错误
十月4日至25日:28:28.498:E / AndroidRuntime(4814):致命异常:主要
10月4日至25日:28:28.498:E / AndroidRuntime(4814):工艺:com.ambrogroup,PID:4814
10月4日至25日:28:28.498:E / AndroidRuntime(4814):显示java.lang.NullPointerException
10月4日至25日:28:28.498:E / AndroidRuntime(4814):在com.ambrogroup.FoodView $ LoadAllProducts $ 1.run(FoodView.java:160)
10月4日至25日:28:28.498:E / AndroidRuntime(4814):在android.app.Activity.runOnUiThread(Activity.java:4713)
10月4日至25日:28:28.498:E / AndroidRuntime(4814):在com.ambrogroup.FoodView $ LoadAllProducts.onPostExecute(FoodView.java:152)
10月4日至25日:28:28.498:E / AndroidRuntime(4814):在com.ambrogroup.FoodView $ LoadAllProducts.onPostExecute(FoodView.java:1)
10月4日至25日:28:28.498:E / AndroidRuntime(4814):在android.os.AsyncTask.finish(AsyncTask.java:632)
10月4日至25日:28:28.498:E / AndroidRuntime(4814):在android.os.AsyncTask.access $ 600(AsyncTask.java:177)
10月4日至25日:28:28.498:E / AndroidRuntime(4814):在android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:645)
10月4日至25日:28:28.498:E / AndroidRuntime(4814):在android.os.Handler.dispatchMessage(Handler.java:102)
10月4日至25日:28:28.498:E / AndroidRuntime(4814):在android.os.Looper.loop(Looper.java:136)
10月4日至25日:28:28.498:E / AndroidRuntime(4814):在android.app.ActivityThread.main(ActivityThread.java:5017)
10月4日至25日:28:28.498:E / AndroidRuntime(4814):在java.lang.reflect.Method.invokeNative(本机方法)
10月4日至25日:28:28.498:E / AndroidRuntime(4814):在java.lang.reflect.Method.invoke(Method.java:515)
10月4日至25日:28:28.498:E / AndroidRuntime(4814):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779)
10月4日至25日:28:28.498:E / AndroidRuntime(4814):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10月4日至25日:28:28.498:E / AndroidRuntime(4814):在dalvik.system.NativeStart.main(本机方法)
ListActivity 已的ListView
捆绑它,所以这一块code在你的的Runnable
不意义:
列表=(ListView控件)findViewById(R.id.list1);
您获得 NPE
,因为该字符串后列表
是空
- 没有被发现。我建议你使用以下code:
LazyImageLoadAdapter适配器=新LazyImageLoadAdapter(FoodView.this,STR,产品列表);
getListView.setAdapter(适配器);
还有 onPostExecute
总是在UI线程调用,没有必要使用 runOnUiThread
I want to load both data and images in ListView but getting errors.
FoodView.java
public class FoodView extends ListActivity
{
private ProgressDialog pDialog;
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> productsList;
private static String url_all_products = "http://ambrogroup.com/app/index/file/food";
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "products";
private static final String TAG_PID = "pid";
private static final String TAG_NAME = "name";
private static final String TAG_IMG = "img";
private static final String TAG_PRICE = "price";
private static final String TAG_DESTINATION = "destination";
JSONArray products = null;
ListView lv;
LazyImageLoadAdapter adapter;
private String[] str;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
productsList = new ArrayList<HashMap<String, String>>();
new LoadAllProducts().execute();
lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
/*
String pid = ((TextView) view.findViewById(R.id.pid)).getText().toString();
String pname = ((TextView) view.findViewById(R.id.name)).getText().toString();
Intent in = new Intent(getApplicationContext(),EditProductActivity.class);
in.putExtra(TAG_PID, pid);
startActivityForResult(in, 100);
*/
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == 100)
{
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
class LoadAllProducts extends AsyncTask<String, String, String>
{
@Override
protected void onPreExecute()
{
super.onPreExecute();
pDialog = new ProgressDialog(FoodView.this);
pDialog.setMessage("Loading list. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
Log.d("Gellting all the list : ", json.toString());
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1)
{
products = json.getJSONArray(TAG_PRODUCTS);
str = new String[products.length()];
for (int i = 0; i < products.length(); i++)
{
JSONObject c = products.getJSONObject(i);
String id = c.getString(TAG_PID);
String name = c.getString(TAG_NAME);
String price = c.getString(TAG_PRICE);
String destination = c.getString(TAG_DESTINATION);
String img = c.getString(TAG_IMG);
str[i] = img;
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_PID, id);
map.put(TAG_NAME, name);
map.put(TAG_PRICE, price);
map.put(TAG_DESTINATION, destination);
map.put(TAG_IMG, img);
productsList.add(map);
}
}
else
{
/* Intent i = new Intent(getApplicationContext(), NewProductActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i); */
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url)
{
pDialog.dismiss();
runOnUiThread(new Runnable()
{
public void run()
{
ListView list;
LazyImageLoadAdapter adapter;
list=(ListView)findViewById(R.id.list1);
adapter=new LazyImageLoadAdapter(FoodView.this, str,productsList);
list.setAdapter(adapter);
}
});
}
}
}
LazyImageLoaderAdapter.java
package com.ambrogroup;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
//Adapter class extends with BaseAdapter and implements with OnClickListener
public class LazyImageLoadAdapter extends BaseAdapter implements OnClickListener{
private Activity activity;
private String[] iImage,iID,iName,iPrice,iDestination;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
ArrayList<HashMap<String, String>> pList;
private static final String TAG_PID="pid",TAG_NAME="name",TAG_DESTINATION="destination",TAG_PRICE="price";
public LazyImageLoadAdapter(Activity a, String[] sImage, ArrayList<HashMap<String, String>> productsList) {
activity = a;
iImage=sImage;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(activity.getApplicationContext());
pList = productsList;
}
public int getCount() {
return iImage.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
/********* Create a holder Class to contain inflated xml file elements *********/
public static class ViewHolder
{
public ImageView vImage;
public TextView tName;
public TextView tPrice;
public TextView tDestination;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if(convertView==null)
{
vi = inflater.inflate(R.layout.list_view, null);
holder = new ViewHolder();
holder.tName = (TextView) vi.findViewById(R.id.name);
holder.tPrice=(TextView)vi.findViewById(R.id.price);
holder.tDestination=(TextView)vi.findViewById(R.id.destination);
holder.vImage=(ImageView)vi.findViewById(R.id.img);
vi.setTag(holder);
}
else
holder=(ViewHolder)vi.getTag();
holder.tName.setText("Company"+position);
holder.tPrice.setText("company description "+position);
ImageView image = holder.vImage;
imageLoader.DisplayImage(iImage[position], image);
vi.setOnClickListener(new OnItemClickListener(position));
return vi;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
/********* Called when Item click in ListView ************/
private class OnItemClickListener implements OnClickListener{
private int mPosition;
OnItemClickListener(int position){
mPosition = position;
}
@Override
public void onClick(View arg0) {
//FoodView sct = (FoodView)activity;
//sct.onItemClick(mPosition);
}
}
}
Following error Iam facing
Logcat Error
04-25 10:28:28.498: E/AndroidRuntime(4814): FATAL EXCEPTION: main
04-25 10:28:28.498: E/AndroidRuntime(4814): Process: com.ambrogroup, PID: 4814
04-25 10:28:28.498: E/AndroidRuntime(4814): java.lang.NullPointerException
04-25 10:28:28.498: E/AndroidRuntime(4814): at com.ambrogroup.FoodView$LoadAllProducts$1.run(FoodView.java:160)
04-25 10:28:28.498: E/AndroidRuntime(4814): at android.app.Activity.runOnUiThread(Activity.java:4713)
04-25 10:28:28.498: E/AndroidRuntime(4814): at com.ambrogroup.FoodView$LoadAllProducts.onPostExecute(FoodView.java:152)
04-25 10:28:28.498: E/AndroidRuntime(4814): at com.ambrogroup.FoodView$LoadAllProducts.onPostExecute(FoodView.java:1)
04-25 10:28:28.498: E/AndroidRuntime(4814): at android.os.AsyncTask.finish(AsyncTask.java:632)
04-25 10:28:28.498: E/AndroidRuntime(4814): at android.os.AsyncTask.access$600(AsyncTask.java:177)
04-25 10:28:28.498: E/AndroidRuntime(4814): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
04-25 10:28:28.498: E/AndroidRuntime(4814): at android.os.Handler.dispatchMessage(Handler.java:102)
04-25 10:28:28.498: E/AndroidRuntime(4814): at android.os.Looper.loop(Looper.java:136)
04-25 10:28:28.498: E/AndroidRuntime(4814): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-25 10:28:28.498: E/AndroidRuntime(4814): at java.lang.reflect.Method.invokeNative(Native Method)
04-25 10:28:28.498: E/AndroidRuntime(4814): at java.lang.reflect.Method.invoke(Method.java:515)
04-25 10:28:28.498: E/AndroidRuntime(4814): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-25 10:28:28.498: E/AndroidRuntime(4814): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-25 10:28:28.498: E/AndroidRuntime(4814): at dalvik.system.NativeStart.main(Native Method)
ListActivity already have ListView
bundled with it, so this piece of code in your Runnable
doesn't make sense:
list=(ListView)findViewById(R.id.list1);
You get NPE
because after that string list
is null
- nothing is found. I suggest you to use the following code:
LazyImageLoadAdapter adapter = new LazyImageLoadAdapter(FoodView.this, str,productsList);
getListView.setAdapter(adapter);
Also onPostExecute
is always invoked on the UI thread, there is no need to use runOnUiThread
这篇关于出错通过JSON一个ListView加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!