我正在运行以下代码,通过JSON从网站获取数据和图像到ListView。
现在它显示错误。
你能帮忙找到问题吗?
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_IMAGE = "img";
private static final String TAG_DESTINATION = "destination";
private static final String TAG_PRICE = "price";
JSONArray products = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.food_view);
// productsList = new ArrayList<HashMap<String, String>>(); // Hashmap for ListView
new LoadAllProducts().execute(); // Loading products in Background Thread by explicit function
ListView listview = getListView(); // Get listview
listview.setOnItemClickListener(new OnItemClickListener() { // on seleting single product // launching Edit Product Screen
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
//getting values from selected ListItem
String pid = ((TextView) view.findViewById(R.id.pid)).getText().toString();
String pname = ((TextView) view.findViewById(R.id.name)).getText().toString();
String pimg = ((TextView) view.findViewById(R.id.img)).getText().toString();
String pdestination = ((TextView) view.findViewById(R.id.destination)).getText().toString();
String pprice = ((TextView) view.findViewById(R.id.price)).getText().toString();
}
});
}
class LoadAllProducts extends AsyncTask<String, String, String>
{
ListView listView;
ArrayList<ItemList> listArray = new ArrayList<ItemList>();
CustomListViewAdapter customGridAdapter;
@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();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
// Check your log cat for JSON reponse
Log.d("Getting all the list : ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1)
{
products = json.getJSONArray(TAG_PRODUCTS);
String[] mStrings= 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 img = c.getString(TAG_IMAGE);
String destination = c.getString(TAG_DESTINATION);
String price = c.getString(TAG_PRICE);
mStrings[i] = img;
String urldisplay = mStrings[i];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
listArray.add(new ItemList(mIcon11,id,name,destination,price));
listView = (ListView) findViewById(R.id.list_item);
customGridAdapter = new CustomListViewAdapter(FoodView.this, R.layout.row_grid, listArray);
listView.setAdapter(customGridAdapter);
}
}
else
{
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
}
logcat的
04-24 05:17:35.508: W/System.err(1072): at android.os.AsyncTask$2.call(AsyncTask.java:288)
04-24 05:17:35.508: W/System.err(1072): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-24 05:17:35.558: I/Choreographer(1072): Skipped 36 frames! The application may be doing too much work on its main thread.
04-24 05:17:36.038: W/System.err(1072): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
04-24 05:17:36.038: W/System.err(1072): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-24 05:17:36.038: W/System.err(1072): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-24 05:17:36.068: W/System.err(1072): at java.lang.Thread.run(Thread.java:841)
04-24 05:17:36.068: W/dalvikvm(1072): threadid=11: thread exiting with uncaught exception (group=0xb4afcba8)
04-24 05:17:36.138: I/Choreographer(1072): Skipped 50 frames! The application may be doing too much work on its main thread.
04-24 05:17:36.688: E/AndroidRuntime(1072): FATAL EXCEPTION: AsyncTask #1
04-24 05:17:36.688: E/AndroidRuntime(1072): Process: com.ambrogroup, PID: 1072
04-24 05:17:36.688: E/AndroidRuntime(1072): java.lang.RuntimeException: An error occured while executing doInBackground()
04-24 05:17:36.688: E/AndroidRuntime(1072): at android.os.AsyncTask$3.done(AsyncTask.java:300)
04-24 05:17:36.688: E/AndroidRuntime(1072): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
04-24 05:17:36.688: E/AndroidRuntime(1072): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
04-24 05:17:36.688: E/AndroidRuntime(1072): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
04-24 05:17:36.688: E/AndroidRuntime(1072): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
04-24 05:17:36.688: E/AndroidRuntime(1072): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-24 05:17:36.688: E/AndroidRuntime(1072): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-24 05:17:36.688: E/AndroidRuntime(1072): at java.lang.Thread.run(Thread.java:841)
04-24 05:17:36.688: E/AndroidRuntime(1072): Caused by: java.lang.NullPointerException
04-24 05:17:36.688: E/AndroidRuntime(1072): at com.ambrogroup.FoodView$LoadAllProducts.doInBackground(FoodView.java:136)
04-24 05:17:36.688: E/AndroidRuntime(1072): at com.ambrogroup.FoodView$LoadAllProducts.doInBackground(FoodView.java:1)
04-24 05:17:36.688: E/AndroidRuntime(1072): at android.os.AsyncTask$2.call(AsyncTask.java:288)
04-24 05:17:36.688: E/AndroidRuntime(1072): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-24 05:17:36.688: E/AndroidRuntime(1072): ... 4 more
答案 0 :(得分:0)
您不应该在doInBackGround()方法中将adapter设置为listview。你可以在asyncTask中调用这个postExecute()
protected String postExecute()
{
listView = (ListView) findViewById(R.id.list_item);
customGridAdapter = new CustomListViewAdapter(FoodView.this, R.layout.row_grid, listArray);
listView.setAdapter(customGridAdapter);
}
答案 1 :(得分:0)
删除类LoadAllProducts中的ListView。在new LoadAllProducts().execute();
之前删除FoodView类的列表视图