我想在ListView中加载数据和图像但是会出错。
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);
}
}
}
以下错误Iam面临
Logcat错误
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)
答案 0 :(得分:1)
为什么不在FoodView类中创建listview和适配器并将其设置为listview onCreate?
答案 1 :(得分:0)
ListActivity已经捆绑了ListView
,因此Runnable
中的这段代码没有意义:
list=(ListView)findViewById(R.id.list1);
您获得NPE
因为在该字符串list
之后null
- 找不到任何内容。我建议你使用以下代码:
LazyImageLoadAdapter adapter = new LazyImageLoadAdapter(FoodView.this, str,productsList);
getListView.setAdapter(adapter);
总是在UI线程上调用onPostExecute
,不需要使用runOnUiThread