私を助けることができるTUTORIELを見つけました..これはMySQLデータベースを使用してデータを取得し、ListViewコントロールに表示します..問題は、AsyncTaskを処理するときにエラーが発生することです..これは私の個人的なコードとエラーレポートです
package pre.commande;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class MainActivity extends ListActivity {
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> produitList;
// url to get all products list
private static String url_all_products = "http://10.0.2.2/precommande/ts_produit.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUIT = "produit";
private static final String TAG_ID = "id";
private static final String TAG_DES = "des";
// products JSONArray
JSONArray produit = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Hashmap for ListView
produitList = new ArrayList<HashMap<String, String>>();
// Loading products in Background Thread
new LoadAllProducts().execute();
// Get listview
ListView lv = getListView();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading products. 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("All Products: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
produit = json.getJSONArray(TAG_PRODUIT);
// looping through All Products
for (int i = 0; i < produit.length(); i++) {
JSONObject c = produit.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String des = c.getString(TAG_DES);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_DES, des);
// adding HashList to ArrayList
produitList.add(map);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, produitList,
R.layout.list_item, new String[] { TAG_ID,
TAG_DES},
new int[] { R.id.pid, R.id.name });
// updating listview
setListAdapter(adapter);
}
});
}
}
}
エラーレポート
GPU エミュレーションなしのエミュレータが検出されました。08-06 11:07:06.318: E/WindowManager(1347): アクティビティ pre.commande.MainActivity が、最初にここに追加されたウィンドウ com.android.internal.policy.impl.PhoneWindow$DecorView@b6c6afc0 をリークしました 08-06 11: 07:06.318: E/WindowManager(1347): android.view.WindowLeaked: アクティビティ pre.commande.MainActivity がウィンドウ com.android.internal.policy.impl.PhoneWindow$DecorView@b6c6afc0 をリークしました 08-06 11 :07:06.318: E/WindowManager(1347): android.view.ViewRoot.(ViewRoot.java:258) 08-06 11:07:06.318: E/WindowManager(1347): android.view.WindowManagerImpl.addView で(WindowManagerImpl.java:148) 08-06 11:07:06.318: E/WindowManager(1347): android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) 08-06 11:07:06.318: E/WindowManager (1347): android.view で。