サーバー上のMySQLデータベースから作物のリストを取得し、このリストをListViewに入力しようとしています。以下は私のコードです:
これは AsyncTask クラスです。
class GetCropsList extends AsyncTask<String, Integer, ArrayList<String>> {
private final ProgressDialog dialog = new ProgressDialog(CropsListActivity.this);
@Override
protected void onPreExecute() {
dialog.setMessage("Please Wait.");
dialog.setCancelable(true);
dialog.show();
}
@Override
protected ArrayList<String> doInBackground(String... params) {
ArrayList<String> list = null;
String Lang=params[0];
Log.i("lang",Lang);
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/Farm_O_Pedia/GetCropsList.php");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("LanguageName",Lang));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse res= httpclient.execute(httppost);
Log.i("HTTP ok", res.toString());
HttpEntity entity = res.getEntity();
input=entity.getContent();
}
catch (Exception e) {
Log.i("HTTP Failed", e.toString());
}
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(input,"UTF-8"));
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line="0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
input.close();
result=sb.toString();
Log.i("JsonObj",result);
}
catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
try {
jArray=new JSONArray(result);
JSONObject obj=null;
for(int i=0;i<jArray.length();i++)
{
obj=jArray.getJSONObject(i);
list.add(obj.getString("crop_name"));
}
} catch (JSONException e) {
e.printStackTrace();
}
return list;
}
protected void onPostExecute(final ArrayList<String> p_result) {
super.onPostExecute(p_result);
if (dialog.isShowing())
{
dialog.dismiss();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(CropsListActivity.this,
R.layout.crops_listview,p_result ) {
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
((TextView) v).setTypeface(tf);
((TextView) v).setText(p_result.get(position));
((TextView) v).setTextColor(Color.WHITE);
return v;
}
};
CropsList.setAdapter(adapter);
}
}
また、doBackground で UI に触れないように注意しました。私のlogcatは次のとおりです。
02-18 11:20:44.301: E/AndroidRuntime(597): FATAL EXCEPTION: AsyncTask #2
02-18 11:20:44.301: E/AndroidRuntime(597): java.lang.RuntimeException: An error occured while executing doInBackground()
02-18 11:20:44.301: E/AndroidRuntime(597): at android.os.AsyncTask$3.done(AsyncTask.java:200)
02-18 11:20:44.301: E/AndroidRuntime(597): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
02-18 11:20:44.301: E/AndroidRuntime(597): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
02-18 11:20:44.301: E/AndroidRuntime(597): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
02-18 11:20:44.301: E/AndroidRuntime(597): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
02-18 11:20:44.301: E/AndroidRuntime(597): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
02-18 11:20:44.301: E/AndroidRuntime(597): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
02-18 11:20:44.301: E/AndroidRuntime(597): at java.lang.Thread.run(Thread.java:1019)
02-18 11:20:44.301: E/AndroidRuntime(597): Caused by: java.lang.NullPointerException
02-18 11:20:44.301: E/AndroidRuntime(597): at com.example.farm_o_pedia.CropsListActivity$GetCropsList.doInBackground(CropsListActivity.java:170)
02-18 11:20:44.301: E/AndroidRuntime(597): at com.example.farm_o_pedia.CropsListActivity$GetCropsList.doInBackground(CropsListActivity.java:1)
02-18 11:20:44.301: E/AndroidRuntime(597): at android.os.AsyncTask$2.call(AsyncTask.java:185)
02-18 11:20:44.301: E/AndroidRuntime(597): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
02-18 11:20:44.301: E/AndroidRuntime(597): ... 4 more
02-18 11:20:45.660: E/WindowManager(597): Activity com.example.farm_o_pedia.CropsListActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40529ed8 that was originally added here
02-18 11:20:45.660: E/WindowManager(597): android.view.WindowLeaked: Activity com.example.farm_o_pedia.CropsListActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40529ed8 that was originally added here
02-18 11:20:45.660: E/WindowManager(597): at android.view.ViewRoot.<init>(ViewRoot.java:258)
02-18 11:20:45.660: E/WindowManager(597): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
02-18 11:20:45.660: E/WindowManager(597): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
02-18 11:20:45.660: E/WindowManager(597): at android.view.Window$LocalWindowManager.addView(Window.java:424)
02-18 11:20:45.660: E/WindowManager(597): at android.app.Dialog.show(Dialog.java:241)
02-18 11:20:45.660: E/WindowManager(597): at com.example.farm_o_pedia.CropsListActivity$GetCropsList.onPreExecute(CropsListActivity.java:116)
02-18 11:20:45.660: E/WindowManager(597): at android.os.AsyncTask.execute(AsyncTask.java:391)
02-18 11:20:45.660: E/WindowManager(597): at com.example.farm_o_pedia.CropsListActivity.onCreate(CropsListActivity.java:80)
02-18 11:20:45.660: E/WindowManager(597): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-18 11:20:45.660: E/WindowManager(597): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-18 11:20:45.660: E/WindowManager(597): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-18 11:20:45.660: E/WindowManager(597): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-18 11:20:45.660: E/WindowManager(597): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-18 11:20:45.660: E/WindowManager(597): at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 11:20:45.660: E/WindowManager(597): at android.os.Looper.loop(Looper.java:123)
02-18 11:20:45.660: E/WindowManager(597): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-18 11:20:45.660: E/WindowManager(597): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 11:20:45.660: E/WindowManager(597): at java.lang.reflect.Method.invoke(Method.java:507)
02-18 11:20:45.660: E/WindowManager(597): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-18 11:20:45.660: E/WindowManager(597): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-18 11:20:45.660: E/WindowManager(597): at dalvik.system.NativeStart.main(Native Method)
どんな助けもかなりのものです。